129 lines
3.6 KiB
Python
129 lines
3.6 KiB
Python
|
|
from collections import Counter
|
||
|
|
from pathlib import Path
|
||
|
|
import re
|
||
|
|
|
||
|
|
|
||
|
|
REPO = Path(__file__).resolve().parent.parent
|
||
|
|
|
||
|
|
|
||
|
|
def read(path: Path) -> str:
|
||
|
|
return path.read_text(encoding="utf-8")
|
||
|
|
|
||
|
|
|
||
|
|
def extract_locale_block(src: str, locale_key: str) -> str:
|
||
|
|
start_match = re.search(rf"\b{re.escape(locale_key)}\s*:\s*\{{", src)
|
||
|
|
assert start_match, f"{locale_key} locale block not found"
|
||
|
|
|
||
|
|
start = start_match.end() - 1
|
||
|
|
depth = 0
|
||
|
|
in_single = False
|
||
|
|
in_double = False
|
||
|
|
in_backtick = False
|
||
|
|
escape = False
|
||
|
|
|
||
|
|
for i in range(start, len(src)):
|
||
|
|
ch = src[i]
|
||
|
|
|
||
|
|
if escape:
|
||
|
|
escape = False
|
||
|
|
continue
|
||
|
|
|
||
|
|
if in_single:
|
||
|
|
if ch == "\\":
|
||
|
|
escape = True
|
||
|
|
elif ch == "'":
|
||
|
|
in_single = False
|
||
|
|
continue
|
||
|
|
|
||
|
|
if in_double:
|
||
|
|
if ch == "\\":
|
||
|
|
escape = True
|
||
|
|
elif ch == '"':
|
||
|
|
in_double = False
|
||
|
|
continue
|
||
|
|
|
||
|
|
if in_backtick:
|
||
|
|
if ch == "\\":
|
||
|
|
escape = True
|
||
|
|
elif ch == "`":
|
||
|
|
in_backtick = False
|
||
|
|
continue
|
||
|
|
|
||
|
|
if ch == "'":
|
||
|
|
in_single = True
|
||
|
|
continue
|
||
|
|
if ch == '"':
|
||
|
|
in_double = True
|
||
|
|
continue
|
||
|
|
if ch == "`":
|
||
|
|
in_backtick = True
|
||
|
|
continue
|
||
|
|
|
||
|
|
if ch == "{":
|
||
|
|
depth += 1
|
||
|
|
continue
|
||
|
|
if ch == "}":
|
||
|
|
depth -= 1
|
||
|
|
if depth == 0:
|
||
|
|
return src[start + 1 : i]
|
||
|
|
|
||
|
|
raise AssertionError(f"{locale_key} locale block braces are not balanced")
|
||
|
|
|
||
|
|
|
||
|
|
def locale_keys(src: str, locale_key: str) -> list[str]:
|
||
|
|
# Locale objects are flat. Match every key regardless of indentation so
|
||
|
|
# accidental 2-space lines cannot hide from duplicate-key checks.
|
||
|
|
key_pattern = re.compile(r"^\s*([a-zA-Z0-9_]+)\s*:", re.MULTILINE)
|
||
|
|
return key_pattern.findall(extract_locale_block(src, locale_key))
|
||
|
|
|
||
|
|
|
||
|
|
def test_korean_locale_block_exists():
|
||
|
|
src = read(REPO / "static" / "i18n.js")
|
||
|
|
assert "\n ko: {" in src
|
||
|
|
assert "_lang: 'ko'" in src
|
||
|
|
assert "_label: 'Korean (한국어)'" in src
|
||
|
|
assert "_speech: 'ko-KR'" in src
|
||
|
|
|
||
|
|
|
||
|
|
def test_korean_locale_includes_representative_translations():
|
||
|
|
src = read(REPO / "static" / "i18n.js")
|
||
|
|
expected = [
|
||
|
|
"settings_title: '설정'",
|
||
|
|
"settings_label_language: '언어'",
|
||
|
|
"login_title: '로그인'",
|
||
|
|
"approval_heading: '승인 필요'",
|
||
|
|
"tab_chat: '채팅'",
|
||
|
|
"tab_tasks: '작업'",
|
||
|
|
"tab_profiles: 'Agent 프로필'",
|
||
|
|
"empty_title: '무엇을 도와드릴까요?'",
|
||
|
|
"onboarding_title: 'Hermes Web UI에 오신 것을 환영합니다'",
|
||
|
|
]
|
||
|
|
for entry in expected:
|
||
|
|
assert entry in src
|
||
|
|
|
||
|
|
|
||
|
|
def test_korean_locale_matches_english_key_coverage():
|
||
|
|
src = read(REPO / "static" / "i18n.js")
|
||
|
|
en_keys = set(locale_keys(src, "en"))
|
||
|
|
ko_keys = set(locale_keys(src, "ko"))
|
||
|
|
assert sorted(en_keys - ko_keys) == []
|
||
|
|
assert sorted(ko_keys - en_keys) == []
|
||
|
|
|
||
|
|
|
||
|
|
def test_korean_locale_has_no_duplicate_keys():
|
||
|
|
src = read(REPO / "static" / "i18n.js")
|
||
|
|
keys = locale_keys(src, "ko")
|
||
|
|
duplicates = sorted(k for k, count in Counter(keys).items() if count > 1)
|
||
|
|
assert not duplicates, f"Korean locale has duplicate keys: {duplicates}"
|
||
|
|
|
||
|
|
|
||
|
|
def test_korean_locale_keys_use_standard_indentation():
|
||
|
|
src = read(REPO / "static" / "i18n.js")
|
||
|
|
ko_block = extract_locale_block(src, "ko")
|
||
|
|
badly_indented = [
|
||
|
|
line.strip()
|
||
|
|
for line in ko_block.splitlines()
|
||
|
|
if re.match(r"^\s{1,3}[a-zA-Z0-9_]+\s*:", line)
|
||
|
|
]
|
||
|
|
assert badly_indented == []
|