diff --git a/.gitignore b/.gitignore index a13d4757..5a28b1ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ -src/kolibri +# A generated source file +src/strings.py tmpenv tar python-for-android/build/ diff --git a/scripts/create_strings.py b/scripts/create_strings.py index 7b9f21ce..c4d48d69 100644 --- a/scripts/create_strings.py +++ b/scripts/create_strings.py @@ -101,7 +101,13 @@ def _find_string(lang, string): return None -def create_resource_files(output_dir): +# Strings that we only access from Python, so we don't need to include them in the strings.xml file +PYTHON_ONLY_STRINGS = [ + "Learner", +] + + +def create_resource_files(output_dir): # noqa: C901 """ Read each language directory and create resource files in the corresponding Android values folder. """ @@ -114,7 +120,9 @@ def create_resource_files(output_dir): en_strings_root = en_strings_tree.getroot() - for lang_dir in os.listdir(output_dir): + all_langs = list(os.listdir(output_dir)) + + for lang_dir in all_langs: if lang_dir == DEFAULT_LANGUAGE: dir_name = "values" else: @@ -166,6 +174,21 @@ def create_resource_files(output_dir): xml_declaration=True, ) + # Create the Python strings file + output = "# This file is auto-generated by the create_strings.py script. Do not edit it directly." + output += "\ni18n_strings = {" + for python_string in PYTHON_ONLY_STRINGS: + output += "\n " + f"'{python_string}': " + "{" + for lang_dir in all_langs: + value = _find_string(lang_dir, python_string) + if value is None: + continue + output += f"\n '{lang_dir}': '{value}', " + output += "\n }," + output += "\n}\n" + with open(os.path.join(os.path.dirname(__file__), "../src/strings.py"), "w") as f: + f.write(output) + def main(): """ diff --git a/src/android_utils.py b/src/android_utils.py index 59501a44..db8a99f6 100644 --- a/src/android_utils.py +++ b/src/android_utils.py @@ -5,9 +5,9 @@ from cryptography import x509 from cryptography.hazmat.backends import default_backend +from i18n import get_string from jnius import autoclass from jnius import cast -from le_utils.proquint import generate def is_service_context(): @@ -229,7 +229,9 @@ def get_dummy_user_name(): cache_key = "DUMMY_USER_NAME" value = value_cache.get(cache_key) if value is None: - value = generate() + Locale = autoclass("java.util.Locale") + currentLocale = Locale.getDefault().toLanguageTag() + value = get_string("Learner", currentLocale) value_cache.set(cache_key, value) return value diff --git a/src/i18n.py b/src/i18n.py new file mode 100644 index 00000000..dc8fe449 --- /dev/null +++ b/src/i18n.py @@ -0,0 +1,11 @@ +try: + from strings import i18n_strings +except ImportError: + i18n_strings = {} + + +def get_string(name, language): + try: + return i18n_strings[language][name] + except KeyError: + return name