Skip to content

Commit

Permalink
Refactor sphinx.locale
Browse files Browse the repository at this point in the history
- Skip typing imports
- Simplify `translators`
  • Loading branch information
AA-Turner committed Sep 28, 2022
1 parent 363cdc0 commit 0466fa8
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions sphinx/locale/__init__.py
@@ -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:
Expand Down Expand Up @@ -37,7 +39,7 @@ def __str__(self) -> str:
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, ...]]':
Expand Down Expand Up @@ -67,10 +69,10 @@ def __mod__(self, other: str) -> str:
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):
Expand All @@ -92,11 +94,15 @@ def __getitem__(self, index):
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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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.
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -216,7 +222,7 @@ def setup(app):
.. 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
Expand Down Expand Up @@ -254,7 +260,7 @@ def gettext(message: str, *args: Any) -> str:
}

# 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]' = {}

0 comments on commit 0466fa8

Please sign in to comment.