"""Unit tests for the i18n helper.""" import importlib import pytest @pytest.fixture def reload_i18n(monkeypatch): """Reload the module after tweaking env vars so LANG is recomputed.""" def _reload(env: dict[str, str]): for k, v in env.items(): monkeypatch.setenv(k, v) import i18n return importlib.reload(i18n) return _reload def test_default_language_is_english(reload_i18n): mod = reload_i18n({"HY_LANG": ""}) assert mod.LANG == "en" assert mod.t("warn.empty_msg") == "Please enter a message" def test_explicit_zh(reload_i18n): mod = reload_i18n({"HY_LANG": "zh-CN.UTF-8"}) assert mod.LANG == "zh" assert mod.t("warn.empty_msg") == "请输入消息内容" def test_falls_back_to_default_lang_for_unknown(reload_i18n): mod = reload_i18n({"HY_LANG": "fr"}) assert mod.LANG == "en" def test_format_args(reload_i18n): mod = reload_i18n({"HY_LANG": "en"}) assert mod.t("tool.call_header", i=2, n=5) == "Function call (2/5)" def test_falls_back_to_english_when_zh_missing(reload_i18n): mod = reload_i18n({"HY_LANG": "zh"}) mod.TRANSLATIONS["zh"].pop("warn.empty_msg", None) assert mod.t("warn.empty_msg") == "Please enter a message" def test_unknown_key_returns_key(reload_i18n): mod = reload_i18n({"HY_LANG": "en"}) assert mod.t("does.not.exist") == "does.not.exist"