From efa663febeca390864312edea6123dab90090d8c Mon Sep 17 00:00:00 2001 From: bridge Date: Sat, 31 Jan 2026 13:36:14 +0800 Subject: [PATCH] add: i18n msgid test --- src/i18n/__init__.py | 7 +++++++ tests/test_i18n_lint.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 tests/test_i18n_lint.py diff --git a/src/i18n/__init__.py b/src/i18n/__init__.py index 64b447c..70d6f96 100644 --- a/src/i18n/__init__.py +++ b/src/i18n/__init__.py @@ -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) diff --git a/tests/test_i18n_lint.py b/tests/test_i18n_lint.py new file mode 100644 index 0000000..fcbacfc --- /dev/null +++ b/tests/test_i18n_lint.py @@ -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}"