add: i18n msgid test

This commit is contained in:
bridge
2026-01-31 13:36:14 +08:00
parent 3a4c1a5dcb
commit efa663febe
2 changed files with 43 additions and 0 deletions

View File

@@ -8,12 +8,15 @@ Usage:
"""
import gettext
import logging
from pathlib import Path
from typing import Optional
# Cache for loaded translations.
_translations: dict[str, Optional[gettext.GNUTranslations]] = {}
logger = logging.getLogger(__name__)
def _get_locale_dir() -> Path:
"""Get the locales directory path."""
@@ -99,6 +102,10 @@ def t(message: str, **kwargs) -> str:
else:
translated = message
# Check for missing translation if not in English
if _get_current_lang() != "en-US" and translated == message and message.strip():
logger.warning(f"[i18n] Missing translation for msgid: '{message}'")
if kwargs:
try:
return translated.format(**kwargs)

36
tests/test_i18n_lint.py Normal file
View File

@@ -0,0 +1,36 @@
import re
import pytest
from pathlib import Path
# Define Chinese character range
ZH_PATTERN = re.compile(r'[\u4e00-\u9fff]')
# Match msgid "content"
MSGID_PATTERN = re.compile(r'^msgid\s+"(.*)"')
def get_po_files():
"""Get all .po files in src/i18n/locales"""
root_dir = Path(__file__).parent.parent / "src" / "i18n" / "locales"
return list(root_dir.rglob("*.po"))
@pytest.mark.parametrize("po_file", get_po_files())
def test_msgid_should_not_contain_chinese(po_file):
"""
Ensure msgid in .po files does not contain Chinese characters.
Convention: msgid should be English source text, msgstr is the translation.
"""
errors = []
try:
with open(po_file, 'r', encoding='utf-8') as f:
for line_num, line in enumerate(f, 1):
line = line.strip()
match = MSGID_PATTERN.match(line)
if match:
content = match.group(1)
if ZH_PATTERN.search(content):
errors.append(f"Line {line_num}: {content}")
except FileNotFoundError:
pytest.skip(f"File not found: {po_file}")
error_msg = "\n".join(errors)
assert not errors, f"Found Chinese characters in msgid in {po_file}:\n{error_msg}"