Some checks are pending
Tests & Quality Checks / Test on Python 3.11 (push) Waiting to run
Tests & Quality Checks / Test on Python 3.12 (push) Waiting to run
Tests & Quality Checks / Test on Python 3.11-1 (push) Waiting to run
Tests & Quality Checks / Test on Python 3.12-1 (push) Waiting to run
Tests & Quality Checks / Test on Python 3.10 (push) Waiting to run
Tests & Quality Checks / Test on Python 3.11-2 (push) Waiting to run
Tests & Quality Checks / Test on Python 3.12-2 (push) Waiting to run
Tests & Quality Checks / Build Artifacts (push) Blocked by required conditions
Tests & Quality Checks / Build Artifacts-1 (push) Blocked by required conditions
87 lines
3 KiB
Python
87 lines
3 KiB
Python
"""Unit tests for i18n translation helper."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from webdrop_bridge.utils import i18n
|
|
|
|
|
|
class TestI18n:
|
|
"""Tests for translation lookup and fallback behavior."""
|
|
|
|
def test_tr_lazy_initialization_uses_english_defaults(self):
|
|
"""Translator should lazily initialize and resolve known keys."""
|
|
# Force a fresh singleton state for this test.
|
|
i18n._translator = i18n.Translator() # type: ignore[attr-defined]
|
|
|
|
assert i18n.tr("settings.title") == "Settings"
|
|
|
|
def test_initialize_with_language_falls_back_to_english(self, tmp_path: Path):
|
|
"""Missing keys in selected language should fall back to English."""
|
|
translations = tmp_path / "translations"
|
|
translations.mkdir(parents=True, exist_ok=True)
|
|
|
|
(translations / "en.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"greeting": "Hello {name}",
|
|
"settings.title": "Settings",
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
(translations / "de.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"settings.title": "Einstellungen",
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
i18n._translator = i18n.Translator() # type: ignore[attr-defined]
|
|
i18n.initialize("de", translations)
|
|
|
|
assert i18n.tr("settings.title") == "Einstellungen"
|
|
assert i18n.tr("greeting", name="Alex") == "Hello Alex"
|
|
|
|
def test_get_available_languages_reads_translation_files(self, tmp_path: Path):
|
|
"""Available languages should be discovered from JSON files."""
|
|
translations = tmp_path / "translations"
|
|
translations.mkdir(parents=True, exist_ok=True)
|
|
(translations / "en.json").write_text("{}", encoding="utf-8")
|
|
(translations / "fr.json").write_text("{}", encoding="utf-8")
|
|
|
|
i18n._translator = i18n.Translator() # type: ignore[attr-defined]
|
|
i18n.initialize("en", translations)
|
|
|
|
available = i18n.get_available_languages()
|
|
assert "en" in available
|
|
assert "fr" in available
|
|
|
|
def test_renamed_settings_keys_fall_back_to_legacy_aliases(self, tmp_path: Path):
|
|
"""Renamed keys should resolve via alias mapping to existing translations."""
|
|
translations = tmp_path / "translations"
|
|
translations.mkdir(parents=True, exist_ok=True)
|
|
|
|
(translations / "en.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"settings.log_file.browse_btn": "Browse...",
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
(translations / "de.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"settings.log_file.browse_btn": "Durchsuchen...",
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
i18n._translator = i18n.Translator() # type: ignore[attr-defined]
|
|
i18n.initialize("de", translations)
|
|
|
|
assert i18n.tr("settings.logging.browse_btn") == "Durchsuchen..."
|