|
4 | 4 |
|
5 | 5 | import locale |
6 | 6 | from gettext import NullTranslations, translation |
7 | | -from os import getenv, path |
| 7 | +from os import path |
8 | 8 | from typing import Any, Callable |
9 | 9 |
|
10 | 10 |
|
@@ -105,22 +105,10 @@ def init( |
105 | 105 | if translator.__class__ is NullTranslations: |
106 | 106 | translator = None |
107 | 107 |
|
108 | | - if getenv('SOURCE_DATE_EPOCH') is not None: |
109 | | - # Disable localization during reproducible source builds |
110 | | - # See https://reproducible-builds.org/docs/source-date-epoch/ |
111 | | - # |
112 | | - # Note: Providing an empty/none value to gettext.translation causes |
113 | | - # it to consult various language-related environment variables to find |
114 | | - # locale(s). We don't want that during a reproducible build; we want |
115 | | - # to run through the same code path, but to return NullTranslations. |
116 | | - # |
117 | | - # To achieve that, specify the ISO-639-3 'undetermined' language code, |
118 | | - # which should not match any translation catalogs. |
119 | | - languages: list[str] | None = ['und'] |
120 | | - elif language: |
| 108 | + if language: |
121 | 109 | if '_' in language: |
122 | 110 | # for language having country code (like "de_AT") |
123 | | - languages = [language, language.split('_')[0]] |
| 111 | + languages: list[str] | None = [language, language.split('_')[0]] |
124 | 112 | else: |
125 | 113 | languages = [language] |
126 | 114 | else: |
@@ -203,7 +191,12 @@ def setup(app): |
203 | 191 | .. versionadded:: 1.8 |
204 | 192 | """ |
205 | 193 | def gettext(message: str) -> str: |
206 | | - return _TranslationProxy(catalog, namespace, message) # type: ignore[return-value] |
| 194 | + if not is_translator_registered(catalog, namespace): |
| 195 | + # not initialized yet |
| 196 | + return _TranslationProxy(catalog, namespace, message) # type: ignore[return-value] # noqa: E501 |
| 197 | + else: |
| 198 | + translator = get_translator(catalog, namespace) |
| 199 | + return translator.gettext(message) |
207 | 200 |
|
208 | 201 | return gettext |
209 | 202 |
|
|
0 commit comments