Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address inconsistently localized entries in objects.inv #10882

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Features added
Bugs fixed
----------

* #9778: Address inconsistently localized entries in ``objects.inv`` by removing translation lazy-loader proxy class.

Testing
--------

Expand Down
111 changes: 5 additions & 106 deletions sphinx/locale/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,91 +5,6 @@
from os import path
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union


class _TranslationProxy:
"""
Class for proxy strings from gettext translations. This is a helper for the
lazy_* functions from this module.

The proxy implementation attempts to be as complete as possible, so that
the lazy objects should mostly work as expected, for example for sorting.
"""
__slots__ = ('_func', '_args')

def __new__(cls, func: Callable, *args: str) -> '_TranslationProxy':
if not args:
# not called with "function" and "arguments", but a plain string
return str(func) # type: ignore[return-value]
return object.__new__(cls)

def __getnewargs__(self) -> Tuple[str]:
return (self._func,) + self._args # type: ignore

def __init__(self, func: Callable, *args: str) -> None:
self._func = func
self._args = args

def __str__(self) -> str:
return str(self._func(*self._args))

def __dir__(self) -> List[str]:
return dir(str)

def __getattr__(self, name: str) -> Any:
return getattr(self.__str__(), name)

def __getstate__(self) -> Tuple[Callable, Tuple[str, ...]]:
return self._func, self._args

def __setstate__(self, tup: Tuple[Callable, Tuple[str]]) -> None:
self._func, self._args = tup

def __copy__(self) -> '_TranslationProxy':
return _TranslationProxy(self._func, *self._args)

def __repr__(self) -> str:
try:
return 'i' + repr(str(self.__str__()))
except Exception:
return f'<{self.__class__.__name__} broken>'

def __add__(self, other: str) -> str:
return self.__str__() + other

def __radd__(self, other: str) -> str:
return other + self.__str__()

def __mod__(self, other: str) -> str:
return self.__str__() % other

def __rmod__(self, other: str) -> str:
return other % self.__str__()

def __mul__(self, other: Any) -> str:
return self.__str__() * other

def __rmul__(self, other: Any) -> str:
return other * self.__str__()

def __hash__(self):
return hash(self.__str__())

def __eq__(self, other):
return self.__str__() == other

def __lt__(self, string):
return self.__str__() < string

def __contains__(self, char):
return char in self.__str__()

def __len__(self):
return len(self.__str__())

def __getitem__(self, index):
return self.__str__()[index]


translators: Dict[Tuple[str, str], NullTranslations] = {}


Expand Down Expand Up @@ -181,18 +96,6 @@ def get_translator(catalog: str = 'sphinx', namespace: str = 'general') -> NullT
return translators.get((namespace, catalog), NullTranslations())


def is_translator_registered(catalog: str = 'sphinx', namespace: str = 'general') -> bool:
return (namespace, catalog) in translators


def _lazy_translate(catalog: str, namespace: str, message: str) -> str:
"""Used instead of _ when creating TranslationProxy, because _ is
not bound yet at that time.
"""
translator = get_translator(catalog, namespace)
return translator.gettext(message)


def get_translation(catalog: str, namespace: str = 'general') -> Callable[[str], str]:
"""Get a translation function based on the *catalog* and *namespace*.

Expand All @@ -219,15 +122,11 @@ def setup(app):
.. versionadded:: 1.8
"""
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
else:
translator = get_translator(catalog, namespace)
if len(args) <= 1:
return translator.gettext(message)
else: # support pluralization
return translator.ngettext(message, args[0], args[1])
translator = get_translator(catalog, namespace)
if len(args) <= 1:
return translator.gettext(message)
else: # support pluralization
return translator.ngettext(message, args[0], args[1])

return gettext

Expand Down