| 1 | import ast |
| 2 | import json |
| 3 | from pathlib import Path |
| 4 | import unittest |
| 5 | |
| 6 | |
| 7 | ROOT_DIR = Path(__file__).parent.parent.parent |
| 8 | WEBUI_MAIN = ROOT_DIR / "webui" / "Main.py" |
| 9 | I18N_DIR = ROOT_DIR / "webui" / "i18n" |
| 10 | |
| 11 | |
| 12 | class _TrKeyVisitor(ast.NodeVisitor): |
| 13 | def __init__(self): |
| 14 | self.keys = set() |
| 15 | |
| 16 | def visit_Call(self, node): |
| 17 | if ( |
| 18 | isinstance(node.func, ast.Name) |
| 19 | and node.func.id == "tr" |
| 20 | and node.args |
| 21 | and isinstance(node.args[0], ast.Constant) |
| 22 | and isinstance(node.args[0].value, str) |
| 23 | ): |
| 24 | self.keys.add(node.args[0].value) |
| 25 | self.generic_visit(node) |
| 26 | |
| 27 | |
| 28 | def _load_translation(locale): |
| 29 | data = json.loads((I18N_DIR / f"{locale}.json").read_text(encoding="utf-8")) |
| 30 | return data.get("Translation", {}) |
| 31 | |
| 32 | |
| 33 | class TestWebuiI18n(unittest.TestCase): |
| 34 | def test_english_locale_covers_static_webui_labels(self): |
| 35 | tree = ast.parse(WEBUI_MAIN.read_text(encoding="utf-8")) |
| 36 | visitor = _TrKeyVisitor() |
| 37 | visitor.visit(tree) |
| 38 | |
| 39 | en_keys = set(_load_translation("en")) |
| 40 | |
| 41 | self.assertEqual(sorted(visitor.keys - en_keys), []) |
| 42 | |
| 43 | def test_russian_locale_covers_english_locale(self): |
| 44 | en_keys = set(_load_translation("en")) |
| 45 | ru_keys = set(_load_translation("ru")) |
| 46 | |
| 47 | self.assertEqual(sorted(en_keys - ru_keys), []) |
| 48 | |
| 49 | def test_russian_locale_covers_static_webui_labels(self): |
| 50 | tree = ast.parse(WEBUI_MAIN.read_text(encoding="utf-8")) |
| 51 | visitor = _TrKeyVisitor() |
| 52 | visitor.visit(tree) |
| 53 | |
| 54 | ru_keys = set(_load_translation("ru")) |
| 55 | |
| 56 | self.assertEqual(sorted(visitor.keys - ru_keys), []) |
| 57 | |
| 58 | def test_script_language_options_include_russian(self): |
| 59 | tree = ast.parse(WEBUI_MAIN.read_text(encoding="utf-8")) |
| 60 | support_locales = None |
| 61 | |
| 62 | for node in tree.body: |
| 63 | if not isinstance(node, ast.Assign): |
| 64 | continue |
| 65 | if any( |
| 66 | isinstance(target, ast.Name) and target.id == "support_locales" |
| 67 | for target in node.targets |
| 68 | ): |
| 69 | support_locales = ast.literal_eval(node.value) |
| 70 | break |
| 71 | |
| 72 | self.assertIsNotNone(support_locales) |
| 73 | self.assertIn("ru-RU", support_locales) |
| 74 |