Skip to content

Commit

Permalink
Remove autogenerated proquint username in favour of translated Learne…
Browse files Browse the repository at this point in the history
…r label.
  • Loading branch information
rtibbles committed Jan 10, 2024
1 parent f536093 commit 45bf636
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
src/kolibri
# A generated source file
src/strings.py
tmpenv
tar
python-for-android/build/
Expand Down
27 changes: 25 additions & 2 deletions scripts/create_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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:
Expand Down Expand Up @@ -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():
"""
Expand Down
6 changes: 4 additions & 2 deletions src/android_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions src/i18n.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 45bf636

Please sign in to comment.