11"""Locale utilities."""
22
3- import gettext
43import locale
5- from collections import defaultdict
6- from gettext import NullTranslations
4+ from gettext import NullTranslations , translation
75from 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
911
1012
1113class _TranslationProxy :
@@ -37,7 +39,7 @@ def __str__(self) -> str:
3739 def __dir__ (self ) -> 'List[str]' :
3840 return dir (str )
3941
40- def __getattr__ (self , name : str ) -> Any :
42+ def __getattr__ (self , name : str ) -> ' Any' :
4143 return getattr (self .__str__ (), name )
4244
4345 def __getstate__ (self ) -> 'Tuple[Callable, Tuple[str, ...]]' :
@@ -67,10 +69,10 @@ def __mod__(self, other: str) -> str:
6769 def __rmod__ (self , other : str ) -> str :
6870 return other % self .__str__ ()
6971
70- def __mul__ (self , other : Any ) -> str :
72+ def __mul__ (self , other : ' Any' ) -> str :
7173 return self .__str__ () * other
7274
73- def __rmul__ (self , other : Any ) -> str :
75+ def __rmul__ (self , other : ' Any' ) -> str :
7476 return other * self .__str__ ()
7577
7678 def __hash__ (self ):
@@ -92,11 +94,15 @@ def __getitem__(self, index):
9294 return self .__str__ ()[index ]
9395
9496
95- translators : Dict [Tuple [str , str ], NullTranslations ] = defaultdict ( NullTranslations )
97+ translators : ' Dict[Tuple[str, str], NullTranslations]' = {}
9698
9799
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]' :
100106 """Look for message catalogs in `locale_dirs` and *ensure* that there is at
101107 least a NullTranslations catalog set in `translators`. If called multiple
102108 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],
112118
113119 if language and '_' in language :
114120 # 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 ]]
116122 elif language :
117123 languages = [language ]
118124 else :
@@ -121,7 +127,7 @@ def init(locale_dirs: List[Optional[str]], language: Optional[str],
121127 # loading
122128 for dir_ in locale_dirs :
123129 try :
124- trans = gettext . translation (catalog , localedir = dir_ , languages = languages )
130+ trans = translation (catalog , localedir = dir_ , languages = languages )
125131 if translator is None :
126132 translator = trans
127133 else :
@@ -137,7 +143,7 @@ def init(locale_dirs: List[Optional[str]], language: Optional[str],
137143 return translator , has_translation
138144
139145
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 :
141147 """Update locale settings.
142148
143149 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
149155 * https://bugs.python.org/issue18378#msg215215
150156
151157 .. 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 .
153159 """
154160 try :
155161 locale .setlocale (category , value )
@@ -158,9 +164,9 @@ def setlocale(category: int, value: Union[str, Iterable[str], None] = None) -> N
158164
159165
160166def init_console (
161- locale_dir : str = path .abspath (path .dirname (__file__ )),
167+ locale_dir : str = path .abspath (path .dirname (__file__ )), # NoQA: B008
162168 catalog : str = 'sphinx' ,
163- ) -> Tuple [NullTranslations , bool ]:
169+ ) -> ' Tuple[NullTranslations, bool]' :
164170 """Initialize locale for console.
165171
166172 .. versionadded:: 1.8
@@ -176,7 +182,7 @@ def init_console(
176182
177183
178184def get_translator (catalog : str = 'sphinx' , namespace : str = 'general' ) -> NullTranslations :
179- return translators [( namespace , catalog )]
185+ return translators . get (( namespace , catalog ), NullTranslations ())
180186
181187
182188def is_translator_registered (catalog : str = 'sphinx' , namespace : str = 'general' ) -> bool :
@@ -191,7 +197,7 @@ def _lazy_translate(catalog: str, namespace: str, message: str) -> str:
191197 return translator .gettext (message )
192198
193199
194- def get_translation (catalog : str , namespace : str = 'general' ) -> Callable [[str ], str ]:
200+ def get_translation (catalog : str , namespace : str = 'general' ) -> ' Callable[[str], str]' :
195201 """Get a translation function based on the *catalog* and *namespace*.
196202
197203 The extension can use this API to translate the messages on the
@@ -216,7 +222,7 @@ def setup(app):
216222
217223 .. versionadded:: 1.8
218224 """
219- def gettext (message : str , * args : Any ) -> str :
225+ def gettext (message : str , * args : ' Any' ) -> str :
220226 if not is_translator_registered (catalog , namespace ):
221227 # not initialized yet
222228 return _TranslationProxy (_lazy_translate , catalog , namespace , message ) # type: ignore # NOQA
@@ -254,7 +260,7 @@ def gettext(message: str, *args: Any) -> str:
254260}
255261
256262# Moved to sphinx.directives.other (will be overridden later)
257- versionlabels : Dict [str , str ] = {}
263+ versionlabels : ' Dict[str, str]' = {}
258264
259265# Moved to sphinx.domains.python (will be overridden later)
260- pairindextypes : Dict [str , str ] = {}
266+ pairindextypes : ' Dict[str, str]' = {}
0 commit comments