USe the system locale if the parameter default_language is not set while instanciating Ki18n

This commit is contained in:
2026-11-22 09:46:04 +01:00
parent 0e8875e878
commit f7825e6789
8 changed files with 420 additions and 37 deletions
+27 -1
View File
@@ -6,7 +6,7 @@ import logging
import pytest
from ki18n.ki18n import Ki18n
from ki18n.ki18n import Ki18n, _parse_system_locale
from support import LOGGER, Env
@@ -18,6 +18,32 @@ def _reset_singleton():
Ki18n._instance = None
@pytest.fixture(autouse=True)
def _no_system_locale(monkeypatch):
"""Pretend that the system locale cannot be determined.
Without an explicit default language ``Ki18n`` uses the system locale, so the
tests must not depend on the machine they run on. Tests that need a locale
set one with the ``system_locale`` fixture.
"""
monkeypatch.setattr(Ki18n, "get_locale", staticmethod(lambda: None))
@pytest.fixture
def system_locale(monkeypatch):
"""Return a function that makes ``Ki18n.get_locale()`` report a given locale.
``system_locale("de_AT")`` builds a ``SystemLocale`` from the name;
``system_locale(None)`` simulates an undeterminable locale.
"""
def apply(name: str | None) -> None:
locale = None if name is None else _parse_system_locale(name, "LANG")
monkeypatch.setattr(Ki18n, "get_locale", staticmethod(lambda: locale))
return apply
@pytest.fixture
def env(tmp_path, monkeypatch) -> Env:
"""Isolated application and user directories.