def save_po(self, path, json_root_key, lang): """ Save Gettext PO file into directory structure as "path/lang/json_root_key.po" If "path/lang/json_root_key.po" already exists, it will be updated accordingly to JSON dict :param path: Root directory where place to files :param json_root_key: JSON key from RcgJsonKeys class :param lang: Language to translate. Should be in RcgLanguages class :return: """ language = next(name for name in RcgLanguages if name.value["iso_code"] == lang) save_path = join(path, lang) save_file = join(save_path, json_root_key.value + ".po") if not exists(save_path): makedirs(save_path) if exists(save_file): # File already exists, let's try to update it logging.info("Updating '{}'...".format(save_file)) po = pofile(save_file) pot = self.generate_pot(json_root_key) po.merge(pot) po.save(save_file) else: # File does not exists, create it from JSON data logging.info("Creating '{}'...".format(save_file)) po = POFile(check_for_duplicates=True) po.metadata = METADATA_ENTRY for entry in self.json_content[json_root_key.value]: if entry[RcgLanguages.LANG_ENGLISH.value["key"]] != "": po_entry = POEntry( msgctxt=entry[LANG_KEY], msgid=entry[RcgLanguages.LANG_ENGLISH.value["key"]], ) if language.value["key"] in entry and entry[ language.value["key"]] is not None: po_entry.msgstr = entry[language.value["key"]] po_entry.flags.append("fuzzy") try: po.append(po_entry) except ValueError: logging.debug( "Entry {} already exists, skipping...".format( entry[LANG_KEY])) po.save(save_file) return