Exemplo n.º 1
0
def get_translations_from_file(pofile):
    file_catalog = MessageCatalog(pofile)
    for message in file_catalog.values():
        default = message.getDefault()
        if default:
            default = default.decode('utf-8')

        yield message.msgid, message.msgstr, default
Exemplo n.º 2
0
def message_references(*pathparts):
    path = fshelpers.resolve_to_path(pathparts)
    catalog = MessageCatalog(path)

    messages = {}
    for msg in catalog.values():
        messages[msg.msgid] = msg.references
    return messages
Exemplo n.º 3
0
def get_translations_from_file(pofile):
    file_catalog = MessageCatalog(pofile)
    for message in file_catalog.values():
        default = message.getDefault()
        if default:
            default = default.decode('utf-8')

        yield message.msgid, message.msgstr, default
Exemplo n.º 4
0
def messages(*pathparts):
    """Returns a dict of messages (msgid => msgstr) of the po file of which
    the path is passed as argument list.
    """
    path = fshelpers.resolve_to_path(pathparts)
    catalog = MessageCatalog(path)

    messages = {}
    for msg in catalog.values():
        messages[msg.msgid] = msg.msgstr
    return messages
Exemplo n.º 5
0
    def _update_message_catalog(self, catalog_path, is_pot=False):
        if not os.path.exists(os.path.dirname(catalog_path)):
            os.makedirs(os.path.dirname(catalog_path))

        if not os.path.exists(catalog_path):
            open(catalog_path, 'w+').close()

        generator = getUtility(IWorkflowGenerator)
        translations = generator.get_translations(
            self.workflow_id, self.specification)

        catalog = MessageCatalog(filename=catalog_path)

        def is_delete_candidate(msg):
            # When our spec-path is in the references of the message,
            # we treat the message as delete candiate.
            if self.relative_specification_path in msg.references:
                # Remove the reference. It will be added again if we
                # still need the message.
                msg.references.remove(self.relative_specification_path)
                return True

            return False

        delete_candidates = map(attrgetter('msgid'),
                                filter(is_delete_candidate,
                                       catalog.values()))

        for msgid, msgstr in translations.items():
            msgid = msgid.decode('utf-8').replace('"', '\\"')
            msgstr = msgstr.decode('utf-8').replace('"', '\\"')

            if msgid in delete_candidates:
                delete_candidates.remove(msgid)

            if msgid in catalog:
                msg = catalog[msgid]
                msg.msgstr = msgstr
                if self.relative_specification_path not in msg.references:
                    msg.references.append(self.relative_specification_path)

                msg.automatic_comments = filter(
                    lambda text: not text.startswith('Default: '),
                    msg.automatic_comments)
                msg.automatic_comments.append(u'Default: "{0}"'.format(msgstr))

            else:
                catalog.add(msgid, msgstr=msgstr,
                            references=[self.relative_specification_path],
                            automatic_comments=[
                                u'Default: "{0}"'.format(msgstr)])

            if is_pot:
                catalog[msgid].msgstr = u''

        for msgid in delete_candidates:
            if not catalog[msgid].references:
                del catalog[msgid]

        with open(catalog_path, 'w+') as catalog_file:
            writer = POWriter(catalog_file, catalog)
            writer.write(msgstrToComment=False, sync=True)

        cleanup_pofile(catalog_path)
Exemplo n.º 6
0
    def _update_message_catalog(self, catalog_path, is_pot=False):
        if not os.path.exists(os.path.dirname(catalog_path)):
            os.makedirs(os.path.dirname(catalog_path))

        if not os.path.exists(catalog_path):
            open(catalog_path, 'w+').close()

        generator = getUtility(IWorkflowGenerator)
        translations = generator.get_translations(self.workflow_id,
                                                  self.specification)

        catalog = MessageCatalog(filename=catalog_path)

        def is_delete_candidate(msg):
            # When our spec-path is in the references of the message,
            # we treat the message as delete candiate.
            if self.relative_specification_path in msg.references:
                # Remove the reference. It will be added again if we
                # still need the message.
                msg.references.remove(self.relative_specification_path)
                return True

            return False

        delete_candidates = map(attrgetter('msgid'),
                                filter(is_delete_candidate, catalog.values()))

        for msgid, msgstr in translations.items():
            msgid = msgid.decode('utf-8').replace('"', '\\"')
            msgstr = msgstr.decode('utf-8').replace('"', '\\"')

            if msgid in delete_candidates:
                delete_candidates.remove(msgid)

            if msgid in catalog:
                msg = catalog[msgid]
                msg.msgstr = msgstr
                if self.relative_specification_path not in msg.references:
                    msg.references.append(self.relative_specification_path)

                msg.automatic_comments = filter(
                    lambda text: not text.startswith('Default: '),
                    msg.automatic_comments)
                msg.automatic_comments.append(u'Default: "{0}"'.format(msgstr))

            else:
                catalog.add(
                    msgid,
                    msgstr=msgstr,
                    references=[self.relative_specification_path],
                    automatic_comments=[u'Default: "{0}"'.format(msgstr)])

            if is_pot:
                catalog[msgid].msgstr = u''

        for msgid in delete_candidates:
            if not catalog[msgid].references:
                del catalog[msgid]

        with open(catalog_path, 'w+') as catalog_file:
            writer = POWriter(catalog_file, catalog)
            writer.write(msgstrToComment=False, sync=True)

        cleanup_pofile(catalog_path)