def extract_module_messages(directory: pathlib.Path): module_files = ModuleFiles.load_from_dirpath(directory) # raise ValueError source_catalog = _build_source_catalog( ModuleSpec.load_from_path(module_files.spec), pathlib.Path(module_files.code), directory, ) po_path = _po_path(directory, default_locale) try: old_source_catalog = read_po_catalog(po_path) except FileNotFoundError: old_source_catalog = Catalog(default_locale) if not catalogs_are_same(source_catalog, old_source_catalog): write_po_catalog(po_path, source_catalog) fuzzy = find_fuzzy_messages(old_catalog=old_source_catalog, new_catalog=source_catalog) for locale_id in supported_locales: if locale_id != default_locale: po_path = _po_path(directory, locale_id) try: old_catalog = read_po_catalog(po_path) except FileNotFoundError: old_catalog = Catalog(locale_id) catalog = _merge_nonsource_catalog(locale_id, old_catalog, source_catalog, fuzzy) if not catalogs_are_same(catalog, old_catalog): write_po_catalog(po_path, catalog)
def merge(): """ Merge the messages found in the template catalog of the default locale into the catalogs of all locales. The template catalog is searched for special comments that indicate default messages. These messages are added to the catalog for the default locale. We compare the old and new default messages. If they are (non-empty and) different, we will mark them as fuzzy in message catalogs (of non-default locales). """ # First, merge the js and python catalogs for the default locale and find fuzzy messages print("Merging catalog for %s at %s" % (default_locale, catalog_path(default_locale, CATALOG_FILENAME))) old_source_catalog = read_po_catalog( catalog_path(default_locale, MID_EXTRACT_CATALOG_FILENAME)) js_catalog = read_po_catalog(catalog_path(default_locale, CATALOG_FILENAME)) python_catalog = read_po_catalog( catalog_path(default_locale, TEMPLATE_CATALOG_FILENAME)) (catalog, python_source_catalog, fuzzy) = _merge_source_catalog(js_catalog, python_catalog, old_source_catalog) write_po_catalog(catalog_path(default_locale, CATALOG_FILENAME), catalog, ignore_obsolete=True) print("Found %s new fuzzy messages" % len(fuzzy)) # Then, add the new python messages to the catalogs for the other locales and mark fuzzy messages for locale in supported_locales: if locale != default_locale: target_catalog_path = catalog_path(locale, CATALOG_FILENAME) print("Merging catalog for %s at %s" % (locale, target_catalog_path)) js_catalog = read_po_catalog(target_catalog_path) old = read_po_catalog( catalog_path(locale, MID_EXTRACT_CATALOG_FILENAME)) catalog = _merge_catalogs([js_catalog, python_source_catalog], old, fuzzy) write_po_catalog(target_catalog_path, catalog, ignore_obsolete=True)
def clean(): """Remove backup catalogs created by `prepare` and convert the catalog of the default locale to a template catalog The strings of messages in the catalog of the default locale are added to the template catalog as special comments. """ print("Cleaning up") # Remove temp files for locale in supported_locales: remove(catalog_path(locale, MID_EXTRACT_CATALOG_FILENAME)) # Update template file for default locale catalog = read_po_catalog(catalog_path(default_locale, CATALOG_FILENAME)) move_strings_to_comments(catalog, comment_tag=COMMENT_TAG_FOR_DEFAULT_MESSAGE) write_po_catalog( catalog_path(default_locale, TEMPLATE_CATALOG_FILENAME), catalog, ignore_obsolete=True, width=10000000, # we set a huge value for width, so that special comments do not wrap omit_header=True, )
def extract_module_messages(directory: pathlib.Path): with directory_loaded_as_zipfile_path(directory) as zip_path: module_zipfile = ModuleZipfile(zip_path) # may be invalid source_catalog = _build_source_catalog(module_zipfile) po_path = _po_path(directory, default_locale) try: old_source_catalog = read_po_catalog(po_path) except FileNotFoundError: old_source_catalog = Catalog(default_locale) # Update file for default locale if not catalogs_are_same(source_catalog, old_source_catalog): write_po_catalog(po_path, source_catalog) # Update template catalog # We will have no specific locale in the template catalog template_catalog = copy_catalog(source_catalog, locale=None) move_strings_to_comments(template_catalog, comment_tag="default-message") pot_path = _pot_path(directory) try: old_template_catalog = read_po_catalog(pot_path) except FileNotFoundError: old_template_catalog = Catalog() if not catalogs_are_same(template_catalog, old_template_catalog): write_po_catalog( pot_path, template_catalog, ignore_obsolete=True, width= 10000000, # we set a huge value for width, so that special comments do not wrap omit_header= True, # removes locale and other info from the output file ) fuzzy = find_fuzzy_messages(old_catalog=old_source_catalog, new_catalog=source_catalog) for locale_id in supported_locales: if locale_id != default_locale: po_path = _po_path(directory, locale_id) try: old_catalog = read_po_catalog(po_path) except FileNotFoundError: old_catalog = Catalog(locale_id) catalog = _merge_nonsource_catalog(locale_id, old_catalog, source_catalog, fuzzy) if not catalogs_are_same(catalog, old_catalog): write_po_catalog(po_path, catalog)