from os import system, makedirs, remove, listdir
from os.path import exists, isfile, join
from shutil import move, copy
+from json import loads
+from polib import pofile, POEntry
from ya2.build.build import find_file_names, FindFileNamesInfo
class LanguageBuilder:
'''Tools for building files for l10n.'''
- def __init__(self, app_name, po_path, mo_path):
+ def __init__(self, app_name, po_path, json_path, mo_path):
self.__app_name = app_name
self.__po_path = po_path
+ self.__json_path = json_path
self.__mo_path = mo_path
def build(self):
cmd_tmpl = 'xgettext -ci18n -d {appname} -L python ' + \
'-o {pot_path}{appname}.pot '
system(cmd_tmpl.format(appname=self.__app_name, pot_path=self.__po_path) + src_files)
+ self.__add_from_json_to_pot()
+
+ def __add_from_json_to_pot(self):
+ json_files = [name for name in listdir(self.__json_path)
+ if isfile(join(self.__json_path, name)) and
+ name.endswith('.json')]
+ json_strings = []
+ for json_file in json_files:
+ with open(f'{self.__json_path}{json_file}') as f:
+ json = loads(f.read())
+ json_strings += [json['name']]
+ for instruction_line in json['instructions']:
+ json_strings += [instruction_line]
+ def process_json_escape(string):
+ return bytes(string, 'utf-8').decode('unicode-escape')
+ json_strings = [process_json_escape(s) for s in json_strings]
+ json_strings = list(set(json_strings))
+ pot = pofile(f'{self.__po_path}{self.__app_name}.pot')
+ pot_ids = [entry.msgid for entry in pot]
+ for json_string in json_strings:
+ if json_string not in pot_ids:
+ entry = POEntry(msgid=json_string)
+ pot.append(entry)
+ pot.save(f'{self.__po_path}{self.__app_name}.pot')
def __process_language(self, language_code):
self.__merge(language_code)