diff --git a/PLUGINS.md b/PLUGINS.md index 03f9d31c..d050afb0 100644 --- a/PLUGINS.md +++ b/PLUGINS.md @@ -1193,19 +1193,27 @@ widget if you need to display routine status information. ## Localisation You can localise your plugin to one of the languages that EDMarketConnector -itself supports. Add the following import near the top of each source +itself supports. Add the following boilerplate near the top of the source file that contains strings that needs translating: ```python -from l10n import translations as tr +import l10n +import functools +plugin_tl = functools.partial(l10n.translations.tl, context=__file__) + ``` -Wrap each string that needs translating with the `tr.tl()` function, e.g.: +Wrap each string that needs translating with the `plugin_tl()` function, e.g.: ```python - somewidget["text"] = tr.tl("Happy!") + somewidget["text"] = plugin_tl("Happy!") ``` +Note that you can name the "plugin_tl" function whatever you want - just make sure to stay consistent! +Many plugins use `_` as the singleton name. We discourage that in versions 5.11 onward, but it should still work. +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. + 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 their preferred language. diff --git a/l10n.py b/l10n.py index f3215062..e72499de 100755 --- a/l10n.py +++ b/l10n.py @@ -86,7 +86,7 @@ class Translations: """ self.translations = {None: {}} # WARNING: '_' is Deprecated. Will be removed in 6.0 or later. - # Migrate to calling Translations.translate directly. + # Migrate to calling translations.translate or tr.tl directly. builtins.__dict__['_'] = lambda x: str(x).replace(r'\"', '"').replace('{CR}', '\n') def install(self, lang: str | None = None) -> None: # noqa: CCR001 @@ -131,7 +131,7 @@ class Translations: logger.exception(f'Exception occurred while parsing {lang}.strings in plugin {plugin}') # WARNING: '_' is Deprecated. Will be removed in 6.0 or later. - # Migrate to calling Translations.translate directly. + # Migrate to calling translations.translate or tr.tl directly. builtins.__dict__['_'] = self.translate def contents(self, lang: str, plugin_path: str | None = None) -> dict[str, str]: @@ -351,7 +351,7 @@ class _Translations(Translations): super().__init__() -Translations: Translations = _Translations() # type: ignore # Yes, I know this is awful. But we need it for compat. +Translations = translations # Yes, I know this is awful renaming garbage. But we need it for compat. # End Deprecation Zone # generate template strings file - like xgettext