Provide a function to reload translations for derivepassphrase
Marco Ricci

Marco Ricci commited on 2024-12-31 18:40:01
Zeige 1 geänderte Dateien mit 60 Einfügungen und 4 Löschungen.

... ...
@@ -6,10 +6,13 @@
6 6
 
7 7
 from __future__ import annotations
8 8
 
9
+import contextlib
9 10
 import datetime
10 11
 import enum
11 12
 import gettext
12 13
 import inspect
14
+import os
15
+import sys
13 16
 import textwrap
14 17
 import types
15 18
 from typing import TYPE_CHECKING, NamedTuple, TextIO, cast
... ...
@@ -17,7 +20,7 @@ from typing import TYPE_CHECKING, NamedTuple, TextIO, cast
17 20
 import derivepassphrase as dpp
18 21
 
19 22
 if TYPE_CHECKING:
20
-    from collections.abc import Iterable, Mapping
23
+    from collections.abc import Iterable, Mapping, Sequence
21 24
 
22 25
     from typing_extensions import Any, Self
23 26
 
... ...
@@ -27,7 +30,62 @@ __version__ = dpp.__version__
27 30
 __all__ = ('PROG_NAME',)
28 31
 
29 32
 PROG_NAME = 'derivepassphrase'
30
-translation = gettext.translation(PROG_NAME, fallback=True)
33
+
34
+
35
+def load_translations(
36
+    localedirs: list[str] | None = None,
37
+    languages: Sequence[str] | None = None,
38
+    class_: type[gettext.NullTranslations] | None = None,
39
+) -> gettext.NullTranslations:
40
+    """Load a translation catalog for derivepassphrase.
41
+
42
+    Runs [`gettext.translation`][] under the hood for multiple locale
43
+    directories.  `fallback=True` is implied.
44
+
45
+    Args:
46
+        localedirs:
47
+            A list of directories to run [`gettext.translation`][]
48
+            against.  Defaults to `$XDG_DATA_HOME/locale` (usually
49
+            `~/.local/share/locale`), `{sys.prefix}/share/locale` and
50
+            `{sys.base_prefix}/share/locale` if not given.
51
+        languages:
52
+            Passed directly to [`gettext.translation`][].
53
+        class_:
54
+            Passed directly to [`gettext.translation`][].
55
+
56
+    Returns:
57
+        A (potentially dummy) translation catalog.
58
+
59
+    """
60
+    if localedirs is None:
61
+        if sys.platform.startswith('win'):
62
+            xdg_data_home = os.environ.get(
63
+                'APPDATA',
64
+                os.path.expanduser('~'),
65
+            )
66
+        elif os.environ.get('XDG_DATA_HOME'):
67
+            xdg_data_home = os.environ['XDG_DATA_HOME']
68
+        else:
69
+            xdg_data_home = os.path.join(
70
+                os.path.expanduser('~'), '.local', 'share'
71
+            )
72
+        localedirs = [
73
+            os.path.join(xdg_data_home, 'locale'),
74
+            os.path.join(sys.prefix, 'share', 'locale'),
75
+            os.path.join(sys.base_prefix, 'share', 'locale'),
76
+        ]
77
+    for localedir in localedirs:
78
+        with contextlib.suppress(OSError):
79
+            return gettext.translation(
80
+                PROG_NAME,
81
+                localedir=localedir,
82
+                languages=languages,
83
+                class_=class_,
84
+            )
85
+    return gettext.NullTranslations()
86
+
87
+
88
+translation = load_translations()
31 89
 
32 90
 
33 91
 class TranslatableString(NamedTuple):
... ...
@@ -1172,6 +1230,4 @@ def _cstr(s: str) -> str:
1172 1230
 
1173 1231
 
1174 1232
 if __name__ == '__main__':
1175
-    import sys
1176
-
1177 1233
     write_pot_file(sys.stdout)
1178 1234