| 1 |
"""ft_gettext.py |
| 2 |
|
| 3 |
The usual pattern when using gettext is to surround strings to be translated |
| 4 |
by a call to a function named _, as in |
| 5 |
|
| 6 |
_("This string should be translated.") |
| 7 |
|
| 8 |
This is done when having gettext "install" a given language: it adds a function |
| 9 |
named _ to the global builtins. However, this can fail if some other program, |
| 10 |
such as the Python REPL, also modifies the builtins and define _ in its own |
| 11 |
ways. |
| 12 |
|
| 13 |
To avoid such problems, we use a custom class that keep track of the language |
| 14 |
preferred for the translation. Inside any namespace, when we need to |
| 15 |
provide a translation, we define locally _ to be |
| 16 |
|
| 17 |
_ = current_lang.translate |
| 18 |
|
| 19 |
where current_lang.translate means gettext.translation().gettext where |
| 20 |
gettext.translation() is the class-based API for gettext. |
| 21 |
""" |
| 22 |
|
| 23 |
import gettext |
| 24 |
import os |
| 25 |
from typing import Optional |
| 26 |
|
| 27 |
from . import debug_helper |
| 28 |
from .typing_info import Translator |
| 29 |
|
| 30 |
|
| 31 |
class LangState: |
| 32 |
def __init__(self) -> None: |
| 33 |
self._translate: Translator = lambda text: text |
| 34 |
self.lang = "en" |
| 35 |
|
| 36 |
def install(self, lang: Optional[str] = None) -> None: |
| 37 |
"""Sets the language to be used for translations""" |
| 38 |
if lang is None: |
| 39 |
lang = "en" |
| 40 |
try: |
| 41 |
|
| 42 |
_lang = gettext.translation( |
| 43 |
f"friendly_tb_{lang}", |
| 44 |
localedir=os.path.normpath( |
| 45 |
os.path.join(os.path.dirname(__file__), "locales") |
| 46 |
), |
| 47 |
languages=[lang], |
| 48 |
fallback=False, |
| 49 |
) |
| 50 |
except FileNotFoundError: |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
lang = lang[:2] |
| 55 |
_lang = gettext.translation( |
| 56 |
f"friendly_tb_{lang}", |
| 57 |
localedir=os.path.normpath( |
| 58 |
os.path.join(os.path.dirname(__file__), "locales") |
| 59 |
), |
| 60 |
languages=[lang], |
| 61 |
fallback=True, |
| 62 |
|
| 63 |
|
| 64 |
) |
| 65 |
|
| 66 |
self.lang = lang |
| 67 |
self._translate = _lang.gettext |
| 68 |
|
| 69 |
def translate(self, text: str) -> str: |
| 70 |
translation = self._translate(text) |
| 71 |
if translation == text and self.lang == "fr": |
| 72 |
debug_helper.log(f"Potentially untranslated text for {self.lang}:") |
| 73 |
debug_helper.log(text) |
| 74 |
return translation |
| 75 |
|
| 76 |
|
| 77 |
current_lang = LangState() |
| 78 |
_ = current_lang.translate |
| 79 |
|
| 80 |
|
| 81 |
def please_report() -> str: |
| 82 |
return _( |
| 83 |
"Please report this example to\n" |
| 84 |
"https://github.com/friendly-traceback/friendly-traceback/issues/new\n" |
| 85 |
"If you are using a REPL, use `www('bug')` to do so.\n\n" |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
def unknown_case() -> str: |
| 90 |
return _("Friendly-traceback does not know the cause of this error.\n") |
| 91 |
|
| 92 |
|
| 93 |
def no_information() -> str: |
| 94 |
debug_helper.log("New case to consider.") |
| 95 |
return ( |
| 96 |
_("No information is known about this exception.\n") |
| 97 |
+ please_report() |
| 98 |
+ _( |
| 99 |
"If you are using the Friendly console, use `www()` to\n" |
| 100 |
"do an Internet search for this particular case.\n" |
| 101 |
) |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
def internal_error(e: Optional[BaseException]) -> str: |
| 106 |
debug_helper.log(f"--> Internal error: {repr(e)}") |
| 107 |
return _("Internal error for Friendly.\n") + please_report() |