Debian Codegraph @

tests/test_intl/test_locale.py

raw
1 """Test locale."""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING
6
7 import pytest
8
9 from sphinx import locale
10
11 if TYPE_CHECKING:
12 from collections.abc import Callable, Iterator
13 from pathlib import Path
14
15 from sphinx.testing.util import SphinxTestApp
16
17
18 @pytest.fixture(autouse=True)
19 def _cleanup_translations() -> Iterator[None]:
20 yield
21 locale.translators.clear()
22
23
24 def test_init(rootdir: Path) -> None:
25 # not initialized yet
26 _ = locale.get_translation('myext')
27 assert _('Hello world') == 'Hello world'
28 assert _('Hello sphinx') == 'Hello sphinx'
29 assert _('Hello reST') == 'Hello reST'
30
31 # load locale1
32 locale.init([rootdir / 'test-locale' / 'locale1'], 'en', 'myext')
33 _ = locale.get_translation('myext')
34 assert _('Hello world') == 'HELLO WORLD'
35 assert _('Hello sphinx') == 'Hello sphinx'
36 assert _('Hello reST') == 'Hello reST'
37
38 # load a catalog to unrelated namespace
39 locale.init([rootdir / 'test-locale' / 'locale2'], 'en', 'myext', 'mynamespace')
40 _ = locale.get_translation('myext')
41 assert _('Hello world') == 'HELLO WORLD'
42 assert _('Hello sphinx') == 'Hello sphinx' # nothing changed here
43 assert _('Hello reST') == 'Hello reST'
44
45 # load locale2 in addition
46 locale.init([rootdir / 'test-locale' / 'locale2'], 'en', 'myext')
47 _ = locale.get_translation('myext')
48 assert _('Hello world') == 'HELLO WORLD'
49 assert _('Hello sphinx') == 'HELLO SPHINX'
50 assert _('Hello reST') == 'Hello reST'
51
52
53 def test_init_with_unknown_language(rootdir: Path) -> None:
54 locale.init([rootdir / 'test-locale' / 'locale1'], 'unknown', 'myext')
55 _ = locale.get_translation('myext')
56 assert _('Hello world') == 'Hello world'
57 assert _('Hello sphinx') == 'Hello sphinx'
58 assert _('Hello reST') == 'Hello reST'
59
60
61 @pytest.mark.sphinx('html', testroot='root')
62 def test_add_message_catalog(app: SphinxTestApp, rootdir: Path) -> None:
63 app.config.language = 'en'
64 app.add_message_catalog('myext', rootdir / 'test-locale' / 'locale1')
65 _ = locale.get_translation('myext')
66 assert _('Hello world') == 'HELLO WORLD'
67 assert _('Hello sphinx') == 'Hello sphinx'
68 assert _('Hello reST') == 'Hello reST'
69
70
71 def _empty_language_translation(rootdir: Path) -> Callable[[str], str]:
72 locale_dirs, catalog = [rootdir / 'test-locale' / 'locale1'], 'myext'
73 locale.translators.clear()
74 locale.init(locale_dirs, language=None, catalog=catalog)
75 return locale.get_translation(catalog)
76
77
78 def test_init_environment_language(
79 rootdir: Path, monkeypatch: pytest.MonkeyPatch
80 ) -> None:
81 with monkeypatch.context() as m:
82 m.setenv('LANGUAGE', 'en_US:en')
83 _ = _empty_language_translation(rootdir)
84 assert _('Hello world') == 'HELLO WORLD'
85
86 with monkeypatch.context() as m:
87 m.setenv('LANGUAGE', 'et_EE:et')
88 _ = _empty_language_translation(rootdir)
89 assert _('Hello world') == 'Tere maailm'