Refactor sphinx.locale

- Skip typing imports
- Simplify `translators`
This commit is contained in:
Adam Turner 2022-09-27 19:20:56 +01:00
parent 363cdc0906
commit 0466fa88eb

View File

@ -1,11 +1,13 @@
"""Locale utilities."""
import gettext
import locale
from collections import defaultdict
from gettext import NullTranslations
from gettext import NullTranslations, translation
from os import path
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
if False:
# NoQA
from collections.abc import Callable, Iterable
from typing import Any, Dict, List, Optional, Tuple, Union
class _TranslationProxy:
@ -37,7 +39,7 @@ class _TranslationProxy:
def __dir__(self) -> 'List[str]':
return dir(str)
def __getattr__(self, name: str) -> Any:
def __getattr__(self, name: str) -> 'Any':
return getattr(self.__str__(), name)
def __getstate__(self) -> 'Tuple[Callable, Tuple[str, ...]]':
@ -67,10 +69,10 @@ class _TranslationProxy:
def __rmod__(self, other: str) -> str:
return other % self.__str__()
def __mul__(self, other: Any) -> str:
def __mul__(self, other: 'Any') -> str:
return self.__str__() * other
def __rmul__(self, other: Any) -> str:
def __rmul__(self, other: 'Any') -> str:
return other * self.__str__()
def __hash__(self):
@ -92,11 +94,15 @@ class _TranslationProxy:
return self.__str__()[index]
translators: Dict[Tuple[str, str], NullTranslations] = defaultdict(NullTranslations)
translators: 'Dict[Tuple[str, str], NullTranslations]' = {}
def init(locale_dirs: List[Optional[str]], language: Optional[str],
catalog: str = 'sphinx', namespace: str = 'general') -> Tuple[NullTranslations, bool]:
def init(
locale_dirs: 'List[Optional[str]]',
language: 'Optional[str]',
catalog: str = 'sphinx',
namespace: str = 'general',
) -> 'Tuple[NullTranslations, bool]':
"""Look for message catalogs in `locale_dirs` and *ensure* that there is at
least a NullTranslations catalog set in `translators`. If called multiple
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],
if language and '_' in language:
# for language having country code (like "de_AT")
languages: Optional[List[str]] = [language, language.split('_')[0]]
languages: 'Optional[List[str]]' = [language, language.split('_')[0]]
elif language:
languages = [language]
else:
@ -121,7 +127,7 @@ def init(locale_dirs: List[Optional[str]], language: Optional[str],
# loading
for dir_ in locale_dirs:
try:
trans = gettext.translation(catalog, localedir=dir_, languages=languages)
trans = translation(catalog, localedir=dir_, languages=languages)
if translator is None:
translator = trans
else:
@ -137,7 +143,7 @@ def init(locale_dirs: List[Optional[str]], language: Optional[str],
return translator, has_translation
def setlocale(category: int, value: Union[str, Iterable[str], None] = None) -> None:
def setlocale(category: int, value: 'Union[str, Iterable[str], None]' = None) -> None:
"""Update locale settings.
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
* https://bugs.python.org/issue18378#msg215215
.. note:: Only for internal use. Please don't call this method from extensions.
This will be removed in future.
This will be removed in Sphinx 6.0.
"""
try:
locale.setlocale(category, value)
@ -158,9 +164,9 @@ def setlocale(category: int, value: Union[str, Iterable[str], None] = None) -> N
def init_console(
locale_dir: str = path.abspath(path.dirname(__file__)),
locale_dir: str = path.abspath(path.dirname(__file__)), # NoQA: B008
catalog: str = 'sphinx',
) -> Tuple[NullTranslations, bool]:
) -> 'Tuple[NullTranslations, bool]':
"""Initialize locale for console.
.. versionadded:: 1.8
@ -176,7 +182,7 @@ def init_console(
def get_translator(catalog: str = 'sphinx', namespace: str = 'general') -> NullTranslations:
return translators[(namespace, catalog)]
return translators.get((namespace, catalog), NullTranslations())
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:
return translator.gettext(message)
def get_translation(catalog: str, namespace: str = 'general') -> Callable[[str], str]:
def get_translation(catalog: str, namespace: str = 'general') -> 'Callable[[str], str]':
"""Get a translation function based on the *catalog* and *namespace*.
The extension can use this API to translate the messages on the
@ -216,7 +222,7 @@ def get_translation(catalog: str, namespace: str = 'general') -> Callable[[str],
.. versionadded:: 1.8
"""
def gettext(message: str, *args: Any) -> str:
def gettext(message: str, *args: 'Any') -> str:
if not is_translator_registered(catalog, namespace):
# not initialized yet
return _TranslationProxy(_lazy_translate, catalog, namespace, message) # type: ignore # NOQA
@ -254,7 +260,7 @@ admonitionlabels = {
}
# Moved to sphinx.directives.other (will be overridden later)
versionlabels: Dict[str, str] = {}
versionlabels: 'Dict[str, str]' = {}
# Moved to sphinx.domains.python (will be overridden later)
pairindextypes: Dict[str, str] = {}
pairindextypes: 'Dict[str, str]' = {}