diff options
| -rw-r--r-- | src/locales/guided_translation.py | 59 |
1 files changed, 47 insertions, 12 deletions
diff --git a/src/locales/guided_translation.py b/src/locales/guided_translation.py index e5f94cfe..65f58db2 100644 --- a/src/locales/guided_translation.py +++ b/src/locales/guided_translation.py @@ -1,8 +1,10 @@ #!/usr/bin/python -import argparse, json, logging +import argparse, json, logging, sys +from pathlib import Path DEFAULT_FROM_LOCALE = 'en' +LOCALE_DATA_FILEPATH = '../lib/translations/lang.json' def nested_dict_keys(d, _keyname_prefix=''): entry_list = [] @@ -14,6 +16,18 @@ def nested_dict_keys(d, _keyname_prefix=''): entry_list += [_keyname_prefix + key] return entry_list +def create_locale(locale_code): + logging.debug("Creating locale %s...", locale_code) + with open(LOCALE_DATA_FILEPATH, 'r', encoding='utf-8') as lang_file: + lang_data = json.load(lang_file) + + lang_data[locale_code] = input("What is the name of the language " + locale_code + "? ") + + with open(LOCALE_DATA_FILEPATH, 'w') as lang_file: + json.dump(lang_data, lang_file, ensure_ascii=False, indent='\t') + lang_file.write('\n') + logging.debug("Created locale %s.", locale_code) + def localize_string(string_id, from_data, to_data): logging.debug("Translating string with id %s", string_id) id_parts = string_id.split('.') @@ -28,6 +42,7 @@ def localize_string(string_id, from_data, to_data): print(to_translate) translated = input("Type the translated text then press ENTER: ") + print(translated) # find/create place to put translated string translation_location = to_data # the dictionary or list that should contain the translated string @@ -42,22 +57,35 @@ if __name__ == "__main__": # setup and parse commandline arguments args_parser = argparse.ArgumentParser(description="guided translation of CoinOS") args_parser.add_argument('from_locale', nargs='?', type=str, - help='which locale to translate from') + help="which locale to translate from") args_parser.add_argument('to_locale', nargs='?', type=str, - help='which locale to translate to') + help="which locale to translate to") + args_parser.add_argument('-v', '--verbose', action='count', default=0, + help="show more information") + args_parser.add_argument('-V', '--version', action='store_true', + help="show version info and exit") args = args_parser.parse_args() + if args.version: + print("Guided Translation Script version 0.2.0") + sys.exit(0) + from_locale = args.from_locale or input("Which locale do you want to translate from (default: %s)? " % DEFAULT_FROM_LOCALE) or DEFAULT_FROM_LOCALE to_locale = args.to_locale or input("Which locale do you want to translate to? ") if not to_locale: raise ValueError("You need to specify a language to translate to.") - logging.basicConfig(format='%(levelname)s: %(message)s') + if args.verbose >= 2: + logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG) + elif args.verbose >= 1: + logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) + else: + logging.basicConfig(format='%(levelname)s: %(message)s') # load locale files logging.debug("Reading file to convert from (%s.json)", from_locale) - with open(from_locale + ".json", 'r') as from_file: + with open(from_locale + ".json", 'r', encoding='utf-8') as from_file: from_json_text = from_file.read() logging.debug("Converting from data from JSON to object") from_data = json.loads(from_json_text) @@ -67,10 +95,16 @@ if __name__ == "__main__": logging.debug("List of strings: %s", from_id_list) logging.debug("Reading file to convert to (%s.json)", to_locale) - with open(to_locale + ".json", 'r') as to_file: - to_json_text = to_file.read() - logging.debug("Converting to data from JSON to object") - to_data = json.loads(to_json_text) + to_filepath = "./" + to_locale + ".json" + if Path(to_filepath).exists(): + with open(to_locale + ".json", 'r', encoding='utf-8') as to_file: + to_json_text = to_file.read() + logging.debug("Converting to data from JSON to object") + to_data = json.loads(to_json_text) + else: + logging.info("No file found. Creating new locale.") + create_locale(to_locale) + to_data = {} to_id_list = nested_dict_keys(to_data) logging.info("Successfully obtained %d strings from locale %s", len(to_id_list), to_locale) @@ -83,9 +117,10 @@ if __name__ == "__main__": localize_string(string_id, from_data, to_data) to_id_list.append(string_id) except KeyboardInterrupt: - print("Program interrupted. Saving strings and closing program...") + print("\nProgram interrupted. Saving strings...") # save localized data - with open(to_locale + ".json", 'w') as to_file: - json.dump(to_data, to_file, ensure_ascii=False, indent=2) + with open(to_locale + ".json", 'w', encoding='utf-8') as to_file: + json.dump(to_data, to_file, ensure_ascii=False, indent='\t') + to_file.write('\n') logging.info("Saved %d localized strings to %s.json", len(to_id_list), to_locale) |
