def rebuild_pot(potfile, package_dir, i18n_domain):
    if not os.path.exists(package_dir):
        return

    translatable_key_expr = re.compile(r'^[^:]*:translate\(%s\)$' %
                                       re.escape(i18n_domain))

    content_creation_files = []
    for dirpath, dirnames, filenames in os.walk(package_dir):
        if not dirpath.rstrip('/').endswith('/content_creation'):
            continue

        content_creation_files.extend(
            map(lambda name: os.path.join(dirpath, name),
                filter(lambda name: name.endswith('.json'), filenames)))

    msgids = set()
    for jsonpath in content_creation_files:
        data = load(open(jsonpath))
        msgids.update(get_translated_values(data, translatable_key_expr))

    if not msgids:
        return

    catalog = MessageCatalog(domain=i18n_domain)
    for msgid in msgids:
        catalog.add(msgid)

    potfile_dir = os.path.dirname(potfile)
    if not os.path.exists(potfile_dir):
        os.makedirs(potfile_dir)

    with open(potfile, 'w') as potfile:
        writer = POWriter(potfile, catalog)
        writer.write(msgstrToComment=True)
def rebuild_pot(potfile, package_dir, i18n_domain):
    if not os.path.exists(package_dir):
        return

    translatable_key_expr = re.compile(
        r'^[^:]*:translate\(%s\)$' % re.escape(i18n_domain))

    content_creation_files = []
    for dirpath, dirnames, filenames in os.walk(package_dir):
        if not dirpath.rstrip('/').endswith('/content_creation'):
            continue

        content_creation_files.extend(
            map(lambda name: os.path.join(dirpath, name),
                filter(lambda name: name.endswith('.json'), filenames)))

    msgids = set()
    for jsonpath in content_creation_files:
        data = load(open(jsonpath))
        msgids.update(get_translated_values(data, translatable_key_expr))

    if not msgids:
        return

    catalog = MessageCatalog(domain=i18n_domain)
    for msgid in msgids:
        catalog.add(msgid)

    potfile_dir = os.path.dirname(potfile)
    if not os.path.exists(potfile_dir):
        os.makedirs(potfile_dir)

    with open(potfile, 'w') as potfile:
        writer = POWriter(potfile, catalog)
        writer.write(msgstrToComment=True)
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
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
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
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
Exemple #7
0
    def find_pofile_for(self, message, language):
        key = (message.package, message.domain, language)
        if key not in self.catalogs:
            path = self.find_pofile_path_for(message, language)
            self.catalogs[key] = MessageCatalog(path)

        return self.catalogs[key]
def catalog_of_files(paths):
    """Returns a merged message catalog of all provided paths.
    """
    if not paths:
        raise ValueError()

    catalog = None

    for path in paths:
        try:
            if not catalog:
                catalog = MessageCatalog(filename=path)
            else:
                catalog.merge(MessageCatalog(filename=path))

        except AttributeError:
            # Skip file - not valid
            pass

    return catalog
    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)
Exemple #10
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)
def create_new_pofile(path, domain):
    catalog = MessageCatalog(domain=domain)
    if not os.path.exists(os.path.dirname(path)):
        os.makedirs(os.path.dirname(path))
    with open(path, 'w+') as file_:
        POWriter(file_, catalog).write(msgstrToComment=False, sync=True)