def cmd_compile(args): def _compile_file(path, component, locale): with io.open(str(path), 'r') as po: catalog = read_po(po, locale=locale, domain=component) logger.info("Compiling component [%s] locale [%s] (%d messages)...", component, locale, len(catalog)) with io.open(str(path.with_suffix('.mo')), 'wb') as mo: write_mo(mo, catalog) with io.open(str(path.with_suffix('.jed')), 'w') as jed: write_jed(jed, catalog) components = list(load_components(args)) modern_file_layout = list() for comp_id, comd_mod in components: locale_path = Path(import_module(comd_mod).__path__[0]) / 'locale' if not locale_path.is_dir() or len(list( locale_path.glob('*.po'))) == 0: continue modern_file_layout.append(comp_id) for po_path in locale_path.glob('*.po'): locale = po_path.with_suffix('').name _compile_file(po_path, comp_id, locale) locpath = resource_filename(args.package, 'locale') pofiles = [] for root, dirnames, filenames in os.walk(locpath): for filename in fnmatch.filter(filenames, '*.po'): pofiles.append(os.path.join(root, filename)[len(locpath) + 1:]) comp_ids = [cid for cid, _ in components] for pofile in pofiles: locale = pofile.split(os.sep, 1)[0] component = os.path.split(pofile)[1][:-3] if component not in comp_ids: continue if component in modern_file_layout: logger.warning( "Component [%s] was already compliled from new style " "locale file layout! Skipping it.", component) continue _compile_file(Path(os.path.join(locpath, pofile)), component, locale)
def cmd_update(args): def _update_file(po_path, pot_path, component, locale): logger.info("Updating component [%s] locale [%s]...", component, locale) with io.open(str(po_path), 'r') as po_fd, io.open(str(pot_path), 'r') as pot_fd: po = read_po(po_fd, locale=locale) pot = read_po(pot_fd) po.update(pot, True) with io.open(str(po_path), 'wb') as fd: write_po(fd, po) components = list(load_components(args)) modern_file_layout = list() for comp_id, comp_mod in components: locale_path = Path(import_module(comp_mod).__path__[0]) / 'locale' if not locale_path.is_dir() or len(list( locale_path.glob('*.po'))) == 0: continue modern_file_layout.append(comp_id) pot_path = locale_path / '.pot' if not pot_path.is_file(): logger.error("POT-file for component [%s] not found in [%s]", comp_id, str(pot_path)) continue for po_path in locale_path.glob('*.po'): locale = po_path.with_suffix('').name _update_file(po_path, pot_path, comp_id, locale) root = resource_filename(args.package, 'locale') pofiles = [] for dirname, dirnames, filenames in os.walk(root): for filename in fnmatch.filter(filenames, '*.po'): relative = os.path.relpath(os.path.join(dirname, filename), root) pofiles.append(relative) comp_ids = [cid for cid, _ in components] for pofile in pofiles: locale = pofile.split(os.sep)[0] component = os.path.split(pofile)[1].split('.', 1)[0] if component not in comp_ids: continue if component in modern_file_layout: logger.warning( "Component [%s] was already updated from new style " "locale file layout! Skipping it.", component) continue logger.info("Updating component '%s' locale '%s'...", component, locale) # NOQA with open(os.path.join(root, pofile), 'r') as fd: catalog = read_po(fd, locale=locale, charset='utf-8') potfile = os.path.join(root, '%s.pot' % component) if not os.path.isfile(potfile): logger.warn("Template for %s:%s doesn't exists! Skipping.", locale, component) # NOQA with codecs.open(potfile, 'r', 'utf-8') as fd: template = read_po(fd) catalog.update(template, True) with open(os.path.join(root, pofile), 'wb') as fd: write_po(fd, catalog)