Esempio n. 1
0
def extract_and_update_locale_dir(dirname, mapping):
    locale_dir = os.path.join(dirname, 'locale')

    for domain in ('django', 'djangojs'):
        extracted_catalog = Catalog()
        extracted = extract_from_dir(
            dirname=dirname,
            method_map=mapping[domain]['method_map'],
            options_map=mapping[domain]['options_map']
        )
        for filename, lineno, message, comments, context in extracted:
            extracted_catalog.add(message, None, [(filename, lineno)], auto_comments=comments, context=context)

        for locale, language in settings.LANGUAGES:
            po_path = os.path.join(locale_dir, locale, 'LC_MESSAGES', '%s.po' % domain)

            if os.path.exists(po_path):
                with open(po_path, 'rb') as po_fileobj:
                    catalog = read_po(po_fileobj, locale=locale, domain=domain)
                if catalog._messages != extracted_catalog._messages:
                    catalog.update(extracted_catalog, no_fuzzy_matching=True, update_header_comment=False)
                    with open(po_path, 'wb') as po_fileobj:
                        write_po(po_fileobj, catalog, ignore_obsolete=True)

            elif extracted_catalog._messages:
                extracted_catalog.locale = Locale(locale)
                extracted_catalog.domain = domain
                extracted_catalog.project = 'Mesto.UA'
                extracted_catalog.copyright_holder = 'Mesto.UA'
                extracted_catalog.fuzzy = False
                touch_directory(po_path)
                with open(po_path, 'wb') as po_fileobj:
                    write_po(po_fileobj, extracted_catalog)