1
1
"""Locale utilities."""
2
2
3
- import gettext
4
3
import locale
5
- from collections import defaultdict
6
- from gettext import NullTranslations
4
+ from gettext import NullTranslations , translation
7
5
from os import path
8
- from typing import Any , Callable , Dict , Iterable , List , Optional , Tuple , Union
6
+
7
+ if False :
8
+ # NoQA
9
+ from collections .abc import Callable , Iterable
10
+ from typing import Any , Dict , List , Optional , Tuple , Union
9
11
10
12
11
13
class _TranslationProxy :
@@ -37,7 +39,7 @@ def __str__(self) -> str:
37
39
def __dir__ (self ) -> 'List[str]' :
38
40
return dir (str )
39
41
40
- def __getattr__ (self , name : str ) -> Any :
42
+ def __getattr__ (self , name : str ) -> ' Any' :
41
43
return getattr (self .__str__ (), name )
42
44
43
45
def __getstate__ (self ) -> 'Tuple[Callable, Tuple[str, ...]]' :
@@ -67,10 +69,10 @@ def __mod__(self, other: str) -> str:
67
69
def __rmod__ (self , other : str ) -> str :
68
70
return other % self .__str__ ()
69
71
70
- def __mul__ (self , other : Any ) -> str :
72
+ def __mul__ (self , other : ' Any' ) -> str :
71
73
return self .__str__ () * other
72
74
73
- def __rmul__ (self , other : Any ) -> str :
75
+ def __rmul__ (self , other : ' Any' ) -> str :
74
76
return other * self .__str__ ()
75
77
76
78
def __hash__ (self ):
@@ -92,11 +94,15 @@ def __getitem__(self, index):
92
94
return self .__str__ ()[index ]
93
95
94
96
95
- translators : Dict [Tuple [str , str ], NullTranslations ] = defaultdict ( NullTranslations )
97
+ translators : ' Dict[Tuple[str, str], NullTranslations]' = {}
96
98
97
99
98
- def init (locale_dirs : List [Optional [str ]], language : Optional [str ],
99
- catalog : str = 'sphinx' , namespace : str = 'general' ) -> Tuple [NullTranslations , bool ]:
100
+ def init (
101
+ locale_dirs : 'List[Optional[str]]' ,
102
+ language : 'Optional[str]' ,
103
+ catalog : str = 'sphinx' ,
104
+ namespace : str = 'general' ,
105
+ ) -> 'Tuple[NullTranslations, bool]' :
100
106
"""Look for message catalogs in `locale_dirs` and *ensure* that there is at
101
107
least a NullTranslations catalog set in `translators`. If called multiple
102
108
times or if several ``.mo`` files are found, their contents are merged
@@ -112,7 +118,7 @@ def init(locale_dirs: List[Optional[str]], language: Optional[str],
112
118
113
119
if language and '_' in language :
114
120
# for language having country code (like "de_AT")
115
- languages : Optional [List [str ]] = [language , language .split ('_' )[0 ]]
121
+ languages : ' Optional[List[str]]' = [language , language .split ('_' )[0 ]]
116
122
elif language :
117
123
languages = [language ]
118
124
else :
@@ -121,7 +127,7 @@ def init(locale_dirs: List[Optional[str]], language: Optional[str],
121
127
# loading
122
128
for dir_ in locale_dirs :
123
129
try :
124
- trans = gettext . translation (catalog , localedir = dir_ , languages = languages )
130
+ trans = translation (catalog , localedir = dir_ , languages = languages )
125
131
if translator is None :
126
132
translator = trans
127
133
else :
@@ -137,7 +143,7 @@ def init(locale_dirs: List[Optional[str]], language: Optional[str],
137
143
return translator , has_translation
138
144
139
145
140
- def setlocale (category : int , value : Union [str , Iterable [str ], None ] = None ) -> None :
146
+ def setlocale (category : int , value : ' Union[str, Iterable[str], None]' = None ) -> None :
141
147
"""Update locale settings.
142
148
143
149
This does not throw any exception even if update fails.
@@ -149,7 +155,7 @@ def setlocale(category: int, value: Union[str, Iterable[str], None] = None) -> N
149
155
* https://bugs.python.org/issue18378#msg215215
150
156
151
157
.. note:: Only for internal use. Please don't call this method from extensions.
152
- This will be removed in future .
158
+ This will be removed in Sphinx 6.0 .
153
159
"""
154
160
try :
155
161
locale .setlocale (category , value )
@@ -158,9 +164,9 @@ def setlocale(category: int, value: Union[str, Iterable[str], None] = None) -> N
158
164
159
165
160
166
def init_console (
161
- locale_dir : str = path .abspath (path .dirname (__file__ )),
167
+ locale_dir : str = path .abspath (path .dirname (__file__ )), # NoQA: B008
162
168
catalog : str = 'sphinx' ,
163
- ) -> Tuple [NullTranslations , bool ]:
169
+ ) -> ' Tuple[NullTranslations, bool]' :
164
170
"""Initialize locale for console.
165
171
166
172
.. versionadded:: 1.8
@@ -176,7 +182,7 @@ def init_console(
176
182
177
183
178
184
def get_translator (catalog : str = 'sphinx' , namespace : str = 'general' ) -> NullTranslations :
179
- return translators [( namespace , catalog )]
185
+ return translators . get (( namespace , catalog ), NullTranslations ())
180
186
181
187
182
188
def is_translator_registered (catalog : str = 'sphinx' , namespace : str = 'general' ) -> bool :
@@ -191,7 +197,7 @@ def _lazy_translate(catalog: str, namespace: str, message: str) -> str:
191
197
return translator .gettext (message )
192
198
193
199
194
- def get_translation (catalog : str , namespace : str = 'general' ) -> Callable [[str ], str ]:
200
+ def get_translation (catalog : str , namespace : str = 'general' ) -> ' Callable[[str], str]' :
195
201
"""Get a translation function based on the *catalog* and *namespace*.
196
202
197
203
The extension can use this API to translate the messages on the
@@ -216,7 +222,7 @@ def setup(app):
216
222
217
223
.. versionadded:: 1.8
218
224
"""
219
- def gettext (message : str , * args : Any ) -> str :
225
+ def gettext (message : str , * args : ' Any' ) -> str :
220
226
if not is_translator_registered (catalog , namespace ):
221
227
# not initialized yet
222
228
return _TranslationProxy (_lazy_translate , catalog , namespace , message ) # type: ignore # NOQA
@@ -254,7 +260,7 @@ def gettext(message: str, *args: Any) -> str:
254
260
}
255
261
256
262
# Moved to sphinx.directives.other (will be overridden later)
257
- versionlabels : Dict [str , str ] = {}
263
+ versionlabels : ' Dict[str, str]' = {}
258
264
259
265
# Moved to sphinx.domains.python (will be overridden later)
260
- pairindextypes : Dict [str , str ] = {}
266
+ pairindextypes : ' Dict[str, str]' = {}
0 commit comments