Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
fix: add the missing status.pending label in all seven locales and test that every referenced translation key resolves
Murtaza Nasird1fe7f1a8e7a
diff --git a/static/locales/de.json b/static/locales/de.jsonindex 0aa9bca3..e76d9ccd 100644--- a/static/locales/de.json+++ b/static/locales/de.json@@ -1526,6 +1526,7 @@"status": {"completed": "Abgeschlossen","failed": "Fehlgeschlagen",+ "pending": "Ausstehend","processing": "Verarbeitung","queued": "In Warteschlange","stuck": "Blockierte Verarbeitung zurücksetzen",diff --git a/static/locales/en.json b/static/locales/en.jsonindex e11a246b..079e6344 100644--- a/static/locales/en.json+++ b/static/locales/en.json@@ -1546,6 +1546,7 @@"status": {"completed": "Completed","failed": "Failed",+ "pending": "Pending","processing": "Processing","queued": "Queued","stuck": "Reset stuck processing",diff --git a/static/locales/es.json b/static/locales/es.jsonindex 6a0cf693..0b040b10 100644--- a/static/locales/es.json+++ b/static/locales/es.json@@ -1526,6 +1526,7 @@"status": {"completed": "Completado","failed": "Falló",+ "pending": "Pendiente","processing": "Procesando","queued": "En cola","stuck": "Restablecer procesamiento bloqueado",diff --git a/static/locales/fr.json b/static/locales/fr.jsonindex 8cd15d16..d8ae2a52 100644--- a/static/locales/fr.json+++ b/static/locales/fr.json@@ -1526,6 +1526,7 @@"status": {"completed": "Terminé","failed": "Échec",+ "pending": "En attente","processing": "Traitement","queued": "En file d'attente","stuck": "Réinitialiser le traitement bloqué",diff --git a/static/locales/pt-BR.json b/static/locales/pt-BR.jsonindex d1bda3d0..f67ae74a 100644--- a/static/locales/pt-BR.json+++ b/static/locales/pt-BR.json@@ -1526,6 +1526,7 @@"status": {"completed": "Concluído","failed": "Falha",+ "pending": "Pendente","processing": "Processando","queued": "Enfileirado","stuck": "Redefinir processamento travado",diff --git a/static/locales/ru.json b/static/locales/ru.jsonindex 1516db17..0f15e986 100644--- a/static/locales/ru.json+++ b/static/locales/ru.json@@ -1526,6 +1526,7 @@"status": {"completed": "Завершено","failed": "Не удалось",+ "pending": "Ожидает","processing": "Обработка","queued": "В очереди","stuck": "Сбросить зависшую обработку",diff --git a/static/locales/zh.json b/static/locales/zh.jsonindex 888d0369..986f5c80 100644--- a/static/locales/zh.json+++ b/static/locales/zh.json@@ -1526,6 +1526,7 @@"status": {"completed": "已完成","failed": "失败",+ "pending": "待处理","processing": "处理中","queued": "排队中","stuck": "重置卡住的处理",diff --git a/tests/test_localization.py b/tests/test_localization.pynew file mode 100644index 00000000..f6635148--- /dev/null+++ b/tests/test_localization.py@@ -0,0 +1,159 @@+"""Every localized string a user can see resolves in every language.++A missing key is not an exception: `i18n.t()` returns the key itself, so the+interface renders the literal string `status.pending` where a word should be.+Nothing fails, no test breaks, and it survives until somebody notices in a+screenshot. That is exactly how `status.pending` shipped: it is the default+status of every new recording, referenced in app.modular.js, and present in+none of the seven locale files.++So this checks the two properties that make that impossible rather than+checking any particular key:++ 1. every key the interface REFERENCES exists in en.json+ 2. every locale carries exactly the same keys as en.json++Both run without a browser, which is the point. The existing browser checks+only cover pages somebody thought to open, in the language they happened to be+using.+"""++import json+import os+import re++import pytest+++REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+LOCALE_DIR = os.path.join(REPO, 'static', 'locales')++# data-i18n="a.b", plus the -placeholder / -title / -aria-label variants.+TEMPLATE_PATTERN = re.compile(r'data-i18n(?:-[a-z-]+)?="([^"]+)"')+# t('a.b') / $t("a.b") / i18n.t('a.b'). Requires a dot so bare t('x') helpers+# and unrelated single-argument calls are not mistaken for keys.+JS_PATTERN = re.compile(r"""(?:\$t|\bt|i18n\.t)\(\s*['"]([a-zA-Z][\w.]*\.[\w.]+)['"]""")+++def _flatten(d, prefix=''):+ keys = set()+ for k, v in d.items():+ key = f'{prefix}{k}'+ if isinstance(v, dict):+ keys |= _flatten(v, key + '.')+ else:+ keys.add(key)+ return keys+++def _load(lang):+ with open(os.path.join(LOCALE_DIR, f'{lang}.json'), encoding='utf-8') as f:+ return json.load(f)+++def _languages():+ return sorted(f[:-5] for f in os.listdir(LOCALE_DIR) if f.endswith('.json'))+++def _walk(root, suffix, skip=()):+ for base, dirs, files in os.walk(os.path.join(REPO, root)):+ dirs[:] = [d for d in dirs if d not in ('node_modules', '__pycache__', 'vendor')]+ for name in files:+ if name.endswith(suffix) and not any(s in name for s in skip):+ yield os.path.join(base, name)+++def _referenced_keys():+ """Every i18n key the templates and frontend actually ask for."""+ found = {}+ for path in _walk('templates', '.html'):+ text = open(path, encoding='utf-8').read()+ for key in TEMPLATE_PATTERN.findall(text):+ found.setdefault(key, set()).add(os.path.relpath(path, REPO))+ for path in _walk('static/js', '.js', skip=('.test.js',)):+ text = open(path, encoding='utf-8').read()+ for key in JS_PATTERN.findall(text):+ found.setdefault(key, set()).add(os.path.relpath(path, REPO))+ return found+++def test_there_are_locale_files_to_check():+ """Guards the tests below from passing vacuously if the glob breaks."""+ langs = _languages()+ assert 'en' in langs+ assert len(langs) >= 7, f'expected the full set of locales, found {langs}'+++def test_the_scanner_actually_finds_keys():+ """Same guard for the reference scan: a broken regex would make the+ coverage test below pass while checking nothing."""+ referenced = _referenced_keys()+ assert len(referenced) > 200, (+ f'only {len(referenced)} keys found across templates and JS; the scanner is broken')+++def test_every_referenced_key_exists_in_english():+ """A key with no entry renders as the literal key. This is the check that+ `status.pending` failed."""+ english = _flatten(_load('en'))+ referenced = _referenced_keys()++ missing = {k: v for k, v in referenced.items() if k not in english}+ assert not missing, 'referenced but absent from en.json:\n' + '\n'.join(+ f' {k} <- {", ".join(sorted(paths))}' for k, paths in sorted(missing.items()))++[email protected]('lang', [l for l in _languages() if l != 'en'])+def test_each_locale_covers_every_english_key(lang):+ """A key present in English and missing elsewhere shows English speakers+ nothing wrong while everyone else sees a raw key."""+ english = _flatten(_load('en'))+ other = _flatten(_load(lang))++ missing = sorted(english - other)+ assert not missing, f'{lang}.json is missing {len(missing)} key(s): {missing[:15]}'++[email protected]('lang', [l for l in _languages() if l != 'en'])+def test_no_locale_carries_keys_english_does_not(lang):+ """Extra keys are dead weight, and usually mean a key was renamed in+ English and left behind here."""+ english = _flatten(_load('en'))+ other = _flatten(_load(lang))++ extra = sorted(other - english)+ assert not extra, f'{lang}.json has {len(extra)} key(s) not in en.json: {extra[:15]}'++[email protected]('lang', _languages())+def test_no_value_is_empty(lang):+ """An empty string renders as nothing at all, which is worse than the+ English fallback would have been."""+ def empties(d, prefix=''):+ out = []+ for k, v in d.items():+ key = f'{prefix}{k}'+ if isinstance(v, dict):+ out += empties(v, key + '.')+ elif isinstance(v, str) and not v.strip():+ out.append(key)+ return out++ blank = empties(_load(lang))+ assert not blank, f'{lang}.json has empty values: {blank[:15]}'+++def test_every_recording_status_the_backend_can_set_has_a_label():+ """The specific gap that motivated this file. PENDING is the column+ default, so every recording passes through it, and it had no label."""+ english = _load('en')['status']+ from src.models import Recording++ column_default = Recording.__table__.columns['status'].default+ assert column_default is not None, 'status lost its default; update this test'+ assert str(column_default.arg).lower() in english, (+ f'the default status {column_default.arg!r} has no label in status.*')++ # The set the backend actually assigns, from the model's own comment.+ for status in ('PENDING', 'PROCESSING', 'SUMMARIZING', 'COMPLETED', 'FAILED'):+ assert status.lower() in english, f'status.{status.lower()} is missing'