aboutsummaryrefslogtreecommitdiffstats
path: root/lib/locales/guided_translation.py
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-01-31 14:35:52 -0800
committerAdam Soltys <[email protected]>2024-01-31 14:35:52 -0800
commit25d57b726f158f7eb142b109c3a51e2c84bad210 (patch)
tree5a1e18122f5322f2cd17475b2ba564a91735fe75 /lib/locales/guided_translation.py
parent0c223a4835711a287f17835af959b47f72ea68c9 (diff)
downloadcoinos-server-25d57b726f158f7eb142b109c3a51e2c84bad210.tar.gz
coinos-server-25d57b726f158f7eb142b109c3a51e2c84bad210.zip
withdrawal emails
Diffstat (limited to 'lib/locales/guided_translation.py')
-rw-r--r--lib/locales/guided_translation.py169
1 files changed, 169 insertions, 0 deletions
diff --git a/lib/locales/guided_translation.py b/lib/locales/guided_translation.py
new file mode 100644
index 00000000..27342314
--- /dev/null
+++ b/lib/locales/guided_translation.py
@@ -0,0 +1,169 @@
+#!/usr/bin/python
+
+import argparse, json, logging, sys, requests, os
+from pathlib import Path
+
+DEFAULT_FROM_LOCALE = 'en'
+LOCALE_DATA_FILEPATH = '../lib/translations/lang.json'
+
+def nested_dict_keys(d, _keyname_prefix=''):
+ entry_list = []
+ for key in d:
+ value = d[key]
+ if isinstance(value, dict):
+ entry_list += nested_dict_keys(value, _keyname_prefix + key + '.')
+ else:
+ 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, to_locale):
+ api_key = os.environ.get('GOOGLE_API_KEY')
+ logging.debug("Translating string with id %s", string_id)
+ id_parts = string_id.split('.')
+
+ # get original string
+ to_translate = from_data
+ for id_part in id_parts:
+ to_translate = to_translate[id_part]
+
+ # Google Translate API endpoint
+ url = "https://translation.googleapis.com/language/translate/v2"
+
+ # Prepare data for POST request
+ data = {
+ 'q': to_translate,
+ 'source': 'en', # assuming the source language is English
+ 'target': to_locale,
+ 'format': 'text'
+ }
+
+ # Headers for the request
+ headers = {"Content-Type": "application/json"}
+
+ # Make the request
+ response = requests.post(url, headers=headers, params={'key': api_key}, json=data)
+
+ # Check if the request was successful
+ if response.status_code == 200:
+ # Parse the response and extract translation
+ translated_text = response.json()['data']['translations'][0]['translatedText']
+ print("Translated text:", translated_text)
+ else:
+ print("Error while translating:", response.json())
+ translated_text = None
+
+ # Proceed only if translation was successful
+ if translated_text:
+ # find/create place to put translated string
+ translation_location = to_data # the dictionary or list that should contain the translated string
+ for id_part in id_parts[:-1]:
+ if id_part not in translation_location:
+ translation_location[id_part] = {}
+ translation_location = translation_location[id_part]
+
+ translation_location[id_parts[-1]] = translated_text
+
+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")
+ args_parser.add_argument('to_locale', nargs='?', type=str,
+ help="which locale to translate to")
+ args_parser.add_argument('-v', '--verbose', action='count', default=0,
+ help="show more information (use twice for even more)")
+ 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.3.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.")
+
+ 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', 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)
+ from_id_list = nested_dict_keys(from_data)
+ logging.info("Successfully obtained %d strings from locale %s",
+ len(from_id_list), from_locale)
+ logging.debug("List of strings: %s", from_id_list)
+
+ logging.debug("Reading file to convert to (%s.json)", to_locale)
+ 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)
+ logging.debug("List of strings: %s", to_id_list)
+
+ # offer to remove strings in to list that were removed from from list
+ removed_strings = []
+ for string_id in to_id_list:
+ if string_id not in from_id_list:
+ removed_strings.append(string_id)
+
+ if removed_strings:
+ print("""There are %d pieces of text that are in the translated locale (%s) but not the original locale (%s).
+The IDs of these pieces of text are: %s
+This could mean that this text was translated first, or it could mean this text was removed from the original locale but has yet to be removed from the translated locale. In the latter case, you may want to remove them from the translated locale file.
+""" % (len(removed_strings), to_locale, from_locale, removed_strings), file=sys.stderr)
+
+ untranslated_strings = []
+ for string_id in from_id_list:
+ if string_id not in to_id_list:
+ untranslated_strings.append(string_id)
+
+ if untranslated_strings:
+ print("There are %d pieces of text to translate: %s"
+ % (len(untranslated_strings), untranslated_strings))
+ # localize strings!
+ try:
+ for string_id in untranslated_strings:
+ localize_string(string_id, from_data, to_data, to_locale)
+ to_id_list.append(string_id)
+ except KeyboardInterrupt:
+ print("\nProgram interrupted. Saving strings...")
+ else:
+ print("Translations already completed.")
+
+ # save localized data
+ 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)