Merge branch 'main' into cram-dev

This commit is contained in:
bunnie 2024-12-16 18:48:00 +08:00 committed by GitHub
commit 370eb75ac1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 71 additions and 55 deletions

View file

@ -4,8 +4,8 @@ The `lib.rs` file in this crate is auto-generated by `xtask`. It must be manuall
calling `xtask generate-locales` every time changes are made to the localization strings. calling `xtask generate-locales` every time changes are made to the localization strings.
## Where are the Strings? ## Where are the Strings?
Locale files can be added to any server by making a `locales` directory within the server, and Locale files can be added to any app, service, etc by making a `locales` directory within the item's top level folder, and
then placing a `.json` file in there with a format like this: then placing a `i18n.json` file in there with a format like this:
```json ```json
{ {
@ -36,6 +36,8 @@ Note that "en-tts" is a locale for english/vision-impaired. `🔇` means that an
for that locale (can be used in any locale, not just speech-to-text locales). Omitting a locale for that locale (can be used in any locale, not just speech-to-text locales). Omitting a locale
entirely without using the 🔇 character will lead to a panic. entirely without using the 🔇 character will lead to a panic.
There are also locale files `i18n.json` and `manifest.json` in the `apps` folder itself for strings relevant to menu items etc. If you add an app you will want to add items here as well.
## How to Code with Locales ## How to Code with Locales
The JSON file is parsed by `xtask` into a lib.rs file which is referred to within a server by The JSON file is parsed by `xtask` into a lib.rs file which is referred to within a server by
the following idiom: the following idiom:
@ -163,8 +165,8 @@ is present it will be treated as the original version.
_NOTE:_ You may want to add the new lang to the other tools _NOTE:_ You may want to add the new lang to the other tools
that know about available languages: that know about available languages:
* https://github.com/betrusted-io/xous-core/blob/main/tools/updater/precursorupdater/precursorupdater.py#L154 * https://github.com/betrusted-io/xous-core/blob/main/tools/updater/precursorupdater/precursorupdater.py#L620
* https://github.com/betrusted-io/xous-core/blob/main/tools/backup.py#L420 * https://github.com/betrusted-io/xous-core/blob/main/tools/backup.py#L435
* https://github.com/betrusted-io/xous-core/blob/main/tools/restore.py#L541 * https://github.com/betrusted-io/xous-core/blob/main/tools/restore.py#L557
* https://github.com/betrusted-io/xous-core/blob/main/tools/legacy/update_ci.sh#L14 * https://github.com/betrusted-io/xous-core/blob/main/tools/legacy/update_ci.sh#L14
* https://github.com/betrusted-io/xous-core/blob/main/tools/legacy/factory_reset.sh#L14 * https://github.com/betrusted-io/xous-core/blob/main/tools/legacy/factory_reset.sh#L14

View file

@ -5,7 +5,6 @@ import json
import sys import sys
import os import os
import os.path import os.path
import pwd
import re import re
@ -71,9 +70,9 @@ class Main(object):
self.args.xousdir = os.path.normpath(os.path.dirname(self.args.pdir)) self.args.xousdir = os.path.normpath(os.path.dirname(self.args.pdir))
self.args.program = os.path.basename(self.args.program) self.args.program = os.path.basename(self.args.program)
self.errfile = sys.stderr self.errfile = sys.stderr
# if end of string matches ' *EN*' (or some other lang) # Regex to match strings ending with ' *EN*' or any other language code (e.g., ' *FR*')
self.temp_regex = re.compile("^.* \*[a-zA-Z]{2}\*$") self.temp_regex = re.compile(r"^.* \*[a-zA-Z]{2}\*$")
self.mt_regex = re.compile("^.* \*MT\*$") self.mt_regex = re.compile(r"^.* \*MT\*$")
def out(self, *objects): def out(self, *objects):
print(*objects) print(*objects)
@ -96,15 +95,16 @@ class Main(object):
return 1 return 1
# read keys in "rootkeys.backup_key": # read keys in "rootkeys.backup_key":
essential = "rootkeys.backup_key" essential = "rootkeys.backup_key"
f = open( try:
root_keys, with open(root_keys, encoding='utf-8') as f:
) obj = json.load(f)
obj = json.load(f) self.languages = []
self.languages = [] if essential in obj:
if essential in obj: for key in obj[essential].keys():
for key in obj[essential].keys(): self.languages.append(key)
self.languages.append(key) except Exception as e:
f.close() self.err(f"Error: {e}")
return 1
return 0 return 0
def list_languages(self): def list_languages(self):
@ -150,11 +150,13 @@ class Main(object):
# other TAG # other TAG
def show_missing(self, i18n, is_manifest): def show_missing(self, i18n, is_manifest):
i18n_path = os.path.join(self.args.xousdir, i18n) i18n_path = os.path.join(self.args.xousdir, i18n)
f = open( try:
i18n_path, with open(i18n_path, encoding='utf-8') as f:
) obj = json.load(f)
obj = json.load(f) except Exception as e:
f.close() self.err(f"Error: {e}")
return 1
jqpath = None jqpath = None
translation = None translation = None
for tag in obj.keys(): for tag in obj.keys():
@ -186,13 +188,17 @@ class Main(object):
def stub_missing(self, i18n, is_manifest): def stub_missing(self, i18n, is_manifest):
i18n_path = os.path.join(self.args.xousdir, i18n) i18n_path = os.path.join(self.args.xousdir, i18n)
f = open( try:
i18n_path, with open(i18n_path, encoding='utf-8') as f:
) obj = json.load(f)
obj = json.load(f) except Exception as e:
f.close() self.err(f"Error: {e}")
return 1
jqpath = None jqpath = None
translation = None translation = None
modified = False
for tag in obj.keys(): for tag in obj.keys():
if is_manifest: if is_manifest:
appmenu = "appmenu." + tag appmenu = "appmenu." + tag
@ -205,21 +211,21 @@ class Main(object):
lang_path = jqpath + "." + lang lang_path = jqpath + "." + lang
if not lang in translation: if not lang in translation:
if lang == "en-tts": if lang == "en-tts":
stub = translation['en']} stub = translation['en']
else: else:
stub = f"{translation['en']} *EN*" stub = f"{translation['en']} *EN*"
print(f"In {i18n}, at {lang_path} with en stub: {stub}") print(f"In {i18n}, at {lang_path} with en stub: {stub}")
translation[lang] = stub translation[lang] = stub
# blow 'em away. That's what git is for after all. modified = True
with open(i18n_path, "w") as f: if modified:
json.dump( # blow 'em away. That's what git is for after all.
obj, try:
f, with open(i18n_path, 'w', encoding='utf-8') as f:
ensure_ascii=False, json.dump(obj, f, ensure_ascii=False, check_circular=False, sort_keys=True, indent=4)
check_circular=False, except Exception as e:
sort_keys=True, self.err(f"Error: {e}")
indent=4, return 1
)
return 0 return 0
def fill_missing_with_en_stub(self): def fill_missing_with_en_stub(self):
@ -255,12 +261,14 @@ class Main(object):
if not os.path.exists(i18n_path_orig): if not os.path.exists(i18n_path_orig):
os.rename(i18n_path, i18n_path_orig) os.rename(i18n_path, i18n_path_orig)
self.verr('adding "%s" to %s' % (self.args.new_lang, i18n)) self.verr('adding "%s" to %s' % (self.args.new_lang, i18n))
orig_file = open(
i18n_path_orig, try:
) with open(i18n_path_orig, encoding='utf-8') as orig_file:
obj = json.load(orig_file) obj = json.load(orig_file)
orig_file.close() except Exception as e:
new_file = open(i18n_path, "w") self.err(f"Error: {e}")
return 1
translation = None translation = None
for tag in obj.keys(): for tag in obj.keys():
if is_manifest: if is_manifest:
@ -273,15 +281,21 @@ class Main(object):
if t_from != "🔇": if t_from != "🔇":
t_new += self.from_hint t_new += self.from_hint
translation[self.args.new_lang] = t_new translation[self.args.new_lang] = t_new
json.dump(
obj, try:
new_file, with open(i18n_path, 'w', encoding='utf-8') as new_file:
ensure_ascii=False, json.dump(
check_circular=False, obj,
sort_keys=True, new_file,
indent=4, ensure_ascii=False,
) check_circular=False,
new_file.close() sort_keys=True,
indent=4,
)
except Exception as e:
self.err(f"Error: {e}")
return 1
return 0 return 0
def new_lang(self): def new_lang(self):
@ -316,7 +330,7 @@ class Main(object):
rc = self.fill_missing_with_en_stub() rc = self.fill_missing_with_en_stub()
elif self.args.new_lang: elif self.args.new_lang:
if not self.args.from_lang: if not self.args.from_lang:
self.err("error: you must specify both --from-lang en --to-lang fr") self.err("error: you must specify both --from-lang en --new-lang fr")
rc = 1 rc = 1
else: else:
self.get_languages() self.get_languages()