Skip to content

Commit

Permalink
[1812] Update Docs and Fix Compat Layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Rixxan committed Apr 22, 2024
1 parent 10be2c2 commit 91e4e79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 12 additions & 4 deletions PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions l10n.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def install_dummy(self) -> None:
"""
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
Expand Down Expand Up @@ -131,7 +131,7 @@ def install(self, lang: str | None = None) -> None: # noqa: CCR001
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]:
Expand Down Expand Up @@ -351,7 +351,7 @@ def __init__(self):
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
Expand Down

0 comments on commit 91e4e79

Please sign in to comment.