mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-06-02 16:41:04 +03:00
Merge pull request #2189 from aussig/enhancement/2188/translation-override
[2188] Support Translation Language Override
This commit is contained in:
commit
8e8d3491f6
@ -1214,6 +1214,15 @@ Many plugins use `_` as the singleton name. We discourage that in versions 5.11
|
|||||||
If your plugin has multiple files that need translations, simply import the `plugin_tl` function to that location.
|
If your plugin has multiple files that need translations, simply import the `plugin_tl` function to that location.
|
||||||
You should only need to add the boilerplate once.
|
You should only need to add the boilerplate once.
|
||||||
|
|
||||||
|
If you wish to override EDMCs current language when translating,
|
||||||
|
`l10n.translations.tl()` also takes an optional `lang` parameter which can
|
||||||
|
be passed a language identifier. For example to define a function to override
|
||||||
|
all translations to German:
|
||||||
|
|
||||||
|
```python
|
||||||
|
plugin_tl_de = functools.partial(l10n.Translations.translate, context=__file__, lang="de")
|
||||||
|
```
|
||||||
|
|
||||||
If you display localized strings in EDMarketConnector's main window you should
|
If you display localized strings in EDMarketConnector's main window you should
|
||||||
refresh them in your `prefs_changed` function in case the user has changed
|
refresh them in your `prefs_changed` function in case the user has changed
|
||||||
their preferred language.
|
their preferred language.
|
||||||
|
39
l10n.py
39
l10n.py
@ -20,6 +20,7 @@ from contextlib import suppress
|
|||||||
from os import listdir, sep, makedirs
|
from os import listdir, sep, makedirs
|
||||||
from os.path import basename, dirname, isdir, join, abspath, exists
|
from os.path import basename, dirname, isdir, join, abspath, exists
|
||||||
from typing import TYPE_CHECKING, Iterable, TextIO, cast
|
from typing import TYPE_CHECKING, Iterable, TextIO, cast
|
||||||
|
|
||||||
from config import config
|
from config import config
|
||||||
from EDMCLogging import get_main_logger
|
from EDMCLogging import get_main_logger
|
||||||
|
|
||||||
@ -159,25 +160,45 @@ class Translations:
|
|||||||
|
|
||||||
return translations
|
return translations
|
||||||
|
|
||||||
def tl(self, x: str, context: str | None = None) -> str:
|
def tl(self, x: str, context: str | None = None, lang: str | None = None) -> str:
|
||||||
"""Use the shorthand Dummy loader for the translate function."""
|
"""Use the shorthand Dummy loader for the translate function."""
|
||||||
return self.translate(x, context)
|
return self.translate(x, context, lang)
|
||||||
|
|
||||||
def translate(self, x: str, context: str | None = None) -> str:
|
def translate(self, x: str, context: str | None = None, lang: str | None = None) -> str: # noqa: CCR001
|
||||||
"""
|
"""
|
||||||
Translate the given string to the current lang.
|
Translate the given string to the current lang or an overriden lang.
|
||||||
|
|
||||||
:param x: The string to translate
|
:param x: The string to translate
|
||||||
:param context: Whether or not to search the given directory for translation files, defaults to None
|
:param context: Contains the full path to the file being localised, from which the plugin name is parsed and
|
||||||
|
used to locate the plugin translation files, defaults to None
|
||||||
|
:param lang: Contains a language code to override the EDMC language for this translation, defaults to None
|
||||||
:return: The translated string
|
:return: The translated string
|
||||||
"""
|
"""
|
||||||
|
plugin_name: str | None = None
|
||||||
|
plugin_path: str | None = None
|
||||||
|
|
||||||
if context:
|
if context:
|
||||||
# TODO: There is probably a better way to go about this now.
|
# TODO: There is probably a better way to go about this now.
|
||||||
context = context[len(config.plugin_dir)+1:].split(sep)[0]
|
plugin_name = context[len(config.plugin_dir)+1:].split(sep)[0]
|
||||||
if self.translations[None] and context not in self.translations:
|
plugin_path = join(config.plugin_dir_path, plugin_name, LOCALISATION_DIR)
|
||||||
logger.debug(f'No translations for {context!r}')
|
|
||||||
|
|
||||||
return self.translations.get(context, {}).get(x) or self.translate(x)
|
if lang:
|
||||||
|
contents: dict[str, str] = self.contents(lang=lang, plugin_path=plugin_path)
|
||||||
|
|
||||||
|
if not contents or type(contents) is not dict:
|
||||||
|
logger.debug(f'Failure loading translations for overridden language {lang!r}')
|
||||||
|
return self.translate(x)
|
||||||
|
elif x not in contents.keys():
|
||||||
|
logger.debug(f'Missing translation: {x!r} for overridden language {lang!r}')
|
||||||
|
return self.translate(x)
|
||||||
|
else:
|
||||||
|
return contents.get(x) or self.translate(x)
|
||||||
|
|
||||||
|
if plugin_name:
|
||||||
|
if self.translations[None] and plugin_name not in self.translations:
|
||||||
|
logger.debug(f'No translations for {plugin_name!r}')
|
||||||
|
|
||||||
|
return self.translations.get(plugin_name, {}).get(x) or self.translate(x)
|
||||||
|
|
||||||
if self.translations[None] and x not in self.translations[None]:
|
if self.translations[None] and x not in self.translations[None]:
|
||||||
logger.debug(f'Missing translation: {x!r}')
|
logger.debug(f'Missing translation: {x!r}')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user