def merge(self): if not self.input_dirs: return logger.info('Processing catalog "%s"', self.name) if not self.pot_file.exists(): logger.warning( "POT template file %s doesn't exist", str(self.pot_file), ) return for lang, po_file in self.po_files.items(): if po_file.isreadonly(): raise ValueError('File is readonly', str(po_file)) with self.pot_file.open() as pot_fp: template = read_po(pot_fp, locale=lang) if po_file.exists(): logger.info('Merging template to PO file %s', po_file) with po_file.open('U') as po_fp: catalog = read_po(po_fp, locale=lang) catalog.update(template) else: logger.info('Creating new PO file %s', po_file) catalog = Catalog() catalog = template catalog.locale = Locale.parse(lang) catalog.fuzzy = False tmp_file = str(po_file) + '.new' po_file.makedirs() with open(tmp_file, 'wb') as tmp_fp: write_po(tmp_fp, catalog, omit_header=True) os.rename(tmp_file, str(po_file))
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)