Files
breeze/tests/locale_keys_test.py
T
waozi 82ed01d010 Translate account alias texts and the Radio app name
Six account alias strings still held an older English text in ten
languages, so the same-as-English check could not see them; the Radio
app kept its English name in the app list. Translate them all and add
their characters to the Chinese, Japanese and Korean fonts.
2026-10-29 11:51:37 -03:00

167 lines
7.2 KiB
Python

#!/usr/bin/env python3
"""Locale files and the code that uses them agree on the set of keys.
Every locale file has exactly the English keys, each with a value and a `---`
separator; the four practice labels exist; every LocaleText / FormatLocaleText
call with a literal key names an English key; and every English key is used
somewhere (or is one of the keys chosen at run time, listed below).
"""
from pathlib import Path
import re
import json
import sys
root = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(root / "scripts"))
from practice_media import media_path
failures = []
PRACTICE_LABELS = ["exercise_wim_hof", "exercise_patterns",
"exercise_meditation", "exercise_sun_salutation",
"exercise_tai_chi"]
# Keys picked by name at run time, so no literal use appears in the sources.
DYNAMIC_KEYS = """
exercise_wim_hof exercise_patterns exercise_meditation exercise_sun_salutation
meditation_music_download_button meditation_music_redownload_button
sync_alias_title sync_alias_change_title sync_alias_message
sync_alias_change_message sync_alias_register_button sync_alias_save_button
skip_button close_button sync_review_using_remote sync_review_keeping_local
habit_stats_no_rounds_month habit_stats_no_rounds_week habit_stats_day_singular
habit_stats_day_plural session_count_singular session_count_plural
practice_home_minutes practice_home_repetitions practice_home_wim_hof_metadata
habit_session_total_count habit_day_total_count_suffix deleted_sessions
sync_pull_ok sync_pull_conflict sync_push_ok sync_push_failed sync_sign_failed
sync_invalid_account sync_server_unreachable sync_server_error sync_connected
sync_auth_failed sync_failed sync_server_unavailable sync_status_connected
sync_status_disconnected tray_show_inner_breeze tray_hide_inner_breeze
tray_start_practice tray_quit_inner_breeze tray_break_settings
tray_mark_complete tray_wim_hof_paused tray_wim_hof_breath tray_wim_hof_hold
tray_wim_hof_breathe_in tray_wim_hof_next_round tray_wim_hof_starting_seconds
tray_meditation_paused tray_meditation_left tray_sun_salutation
tray_sun_salutation_paused tray_patterns_left tray_patterns_left_paused
tray_patterns_elapsed tray_patterns_elapsed_paused tray_next_break
theme_sky theme_ocean theme_forest theme_sunset theme_lavender theme_cherry
theme_dawn theme_sage theme_sepia theme_mono theme_mint theme_cobalt
tutorial_step_intro tutorial_step_method tutorial_step_breathe
tutorial_step_exhale_hold tutorial_step_inhale_hold
app_name_arcade app_name_chess app_name_photos app_name_library app_name_radio
""".split() + ["sun_salutation_step_%d" % i for i in range(1, 13)]
def tai_chi_course_keys():
"""The Tai Chi course's words: locale keys named in course.tch's strings."""
data = media_path("assets/practices/taichi/course.tch").read_bytes()
if data[:4] != b"TCH2":
failures.append("assets/practices/taichi/course.tch is not a course file")
return []
count = int.from_bytes(data[4:8], "little")
at = 8
keys = []
for _ in range(count):
length = int.from_bytes(data[at:at + 2], "little")
value = data[at + 2:at + 2 + length].decode("utf-8")
at += 2 + length
if value.startswith("tai_chi_text_"):
keys.append(value)
return keys
DYNAMIC_KEYS += tai_chi_course_keys()
# Achievement titles and descriptions are declarative app-owned locale keys.
for catalog in sorted((root / "apps").glob("*/achievements.json")):
for achievement in json.loads(catalog.read_text())["achievements"]:
DYNAMIC_KEYS += [achievement["title_key"], achievement["description_key"],
"achievement_group_" + achievement["group"]]
def load_keys(path):
"""The keys of a locale file, recording format problems as failures."""
keys = []
pending = None
needs_separator = False
for line in path.read_text(encoding="utf-8").split("\n"):
line = line.rstrip("\r")
if line == "---":
needs_separator = False
header = re.match(r"^\[([^\]]+)\]", line)
if header:
key = header.group(1)
if needs_separator:
failures.append(f"{path.name} missing --- before [{key}]")
needs_separator = True
if pending is not None:
failures.append(f"{path.name} key [{pending}] has no value")
if key not in keys:
keys.append(key)
pending = key
elif pending is not None and line.strip() not in ("", "---"):
pending = None
if pending is not None:
failures.append(f"{path.name} key [{pending}] has no value")
return keys
def check_practice_labels(keys, name):
for label in PRACTICE_LABELS:
if label not in keys:
failures.append(f"{name} missing practice label [{label}]")
english = load_keys(root / "locales/en.txt")
english_set = set(english)
check_practice_labels(english, "en.txt")
sources = []
for directory in (root / "src", root / "apps", root / "build/packages/kryon/src", root / "build/packages/entity/src"):
if directory.is_dir():
sources += sorted(p for p in directory.rglob("*") if p.suffix in (".zi", ".c", ".h"))
call = re.compile(r'\b(?:LocaleText|GetLocaleText|FormatLocaleText|[A-Za-z]+LocaleHost)\(\s*"([^"]*)"')
used = set(DYNAMIC_KEYS)
for source in sources:
text = source.read_text(encoding="utf-8", errors="replace")
for number, line in enumerate(text.split("\n"), 1):
for match in call.finditer(line):
if match.group(1) not in english_set:
failures.append(f"{source.relative_to(root)}:{number} locale call missing English key [{match.group(1)}]")
# Key literals also appear in data tables and SQL projections. SQL uses
# single quotes for the field labels resolved by LocaleText at runtime.
for literal in re.findall(r'''["']([^"'\n]{1,127})["']''', text):
if literal in english_set:
used.add(literal)
# English strings nothing uses yet. Listing them keeps new dead strings from
# slipping in; delete a key from the locale files and from this list together.
allowed_unused = set()
allowlist = root / "tests/locale_unused_keys.txt"
if allowlist.exists():
allowed_unused = {line.strip() for line in allowlist.read_text().split("\n") if line.strip()}
for key in english:
if key not in used and key not in allowed_unused:
failures.append(f"en.txt unused key [{key}]")
for key in sorted(allowed_unused):
if key not in english_set:
failures.append(f"tests/locale_unused_keys.txt lists [{key}], which is not in en.txt")
elif key in used:
failures.append(f"tests/locale_unused_keys.txt lists [{key}], which is now used")
for path in sorted((root / "locales").glob("*.txt")):
if path.name in ("en.txt", "index.txt"):
continue
keys = load_keys(path)
check_practice_labels(keys, path.name)
have = set(keys)
for key in english:
if key not in have:
failures.append(f"{path.name} missing [{key}]")
for key in keys:
if key not in english_set:
failures.append(f"{path.name} extra key [{key}] not in en.txt")
if failures:
print("\n".join(failures), file=sys.stderr)
print(f"{len(failures)} locale key test failure(s)", file=sys.stderr)
sys.exit(1)
print("locale key tests passed")