From bd5d30e72773e7fca66d2a32bbb12fa4d1454b46 Mon Sep 17 00:00:00 2001 From: Avery <19372006+starsflower@users.noreply.github.com> Date: Tue, 11 May 2021 09:54:04 +0100 Subject: [PATCH] fix: Fixes bug where the current locale would fail to be set This also allows bots to be initialised without a seperate function call --- py18n/__init__.py | 2 +- py18n/extension.py | 29 ++++++++++++++++++++++++----- setup.py | 2 +- tests/__main__.py | 6 ++++++ tests/test_extension.py | 28 ++++++++++++++++++++++++---- 5 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 tests/__main__.py diff --git a/py18n/__init__.py b/py18n/__init__.py index 4b0a67a..9f2c74f 100644 --- a/py18n/__init__.py +++ b/py18n/__init__.py @@ -19,4 +19,4 @@ from .language import Language from .extension import I18nExtension -__version__ = "v1.1" \ No newline at end of file +__version__ = "1.1.1" \ No newline at end of file diff --git a/py18n/extension.py b/py18n/extension.py index 34e87de..d5ad03e 100644 --- a/py18n/extension.py +++ b/py18n/extension.py @@ -28,9 +28,22 @@ class I18nExtension(I18n): default_i18n_instance = None - def __init__(self, languages: List[Language], fallback: Union[str, int], bot: Optional[commands.Bot] = None, default: bool = True) -> None: + def __init__( + self, + languages: List[Language], + fallback: Union[str, int], + bot: Optional[commands.Bot] = None, + get_locale_func: Callable = None, + default: bool = True + ) -> None: """ - Initialize the extension class + Initialize the extension class. + + .. warning:: + + The bot will only be attached to if both `bot` and `get_locale_func` + are provided to this function. Otherwise it will not attach + automatically. Parameters ---------- @@ -38,8 +51,10 @@ def __init__(self, languages: List[Language], fallback: Union[str, int], bot: Op List of lanugages to use fallback : Union[str, int] String ID or list index of the fallback locale - bot : Optional[commands.Bot], optional + bot : commands.Bot, optional The bot to attach to, by default None + get_locale_func : Callable, optional + If provided, init_bot will be run for you default : bool, optional Whether to make this i18n instance the default, by default True @@ -54,6 +69,9 @@ def __init__(self, languages: List[Language], fallback: Union[str, int], bot: Op if default or I18nExtension.default_i18n_instance is None: I18nExtension.default_i18n_instance = self + + if self._bot and get_locale_func: + self.init_bot(self._bot, get_locale_func) def init_bot(self, bot: commands.Bot, get_locale_func: Callable = None): """ @@ -67,7 +85,7 @@ def init_bot(self, bot: commands.Bot, get_locale_func: Callable = None): I recommend creating an override to have multiple pre- and post- invoke hooks if required, or setting the current locale yourself - with :func`set_current_locale`. + with :func:`set_current_locale`. Parameters ---------- @@ -80,10 +98,11 @@ def init_bot(self, bot: commands.Bot, get_locale_func: Callable = None): """ self._bot = bot if get_locale_func is None: + # Just use the fallback get_locale_func = lambda *_: self._fallback async def pre(ctx): - ctx.set_current_locale(get_locale_func(ctx)) + self.set_current_locale(get_locale_func(ctx)) self._bot.before_invoke(pre) diff --git a/setup.py b/setup.py index 7f1551b..1a0608a 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ long_description = f.read() setup(name='Py18n', - version='1.1', + version='1.1.1', description='I18n for Discord.py', author='starsflower', url='https://github.com/starsflower/py18n', diff --git a/tests/__main__.py b/tests/__main__.py new file mode 100644 index 0000000..bb60b25 --- /dev/null +++ b/tests/__main__.py @@ -0,0 +1,6 @@ +from .test_extension import * +from .test_i18n import * +from .test_language import * + +if __name__ == "__main__": + unittest.main(verbosity=2) \ No newline at end of file diff --git a/tests/test_extension.py b/tests/test_extension.py index 1e0aa18..a6ae1a5 100644 --- a/tests/test_extension.py +++ b/tests/test_extension.py @@ -17,12 +17,14 @@ import unittest +from discord.ext import commands + from py18n.extension import I18nExtension, _ from py18n.language import Language -class I18nTesting(unittest.TestCase): - def setUp(self) -> None: +class I18nExtensionTesting(unittest.TestCase): + def test_basic_get_contextual(self): self.i18n = I18nExtension([ Language("English", "en", { "hello": "Hello", @@ -35,8 +37,7 @@ def setUp(self) -> None: "francais": "Français" }), ], fallback="en") - - def test_basic_get_contextual(self): + self.i18n.set_current_locale("en") self.assertEqual(_("hello"), "Hello") self.i18n.set_current_locale("fr") @@ -48,6 +49,25 @@ def test_no_i18n_set(self): with self.assertRaises(NameError): _("hello") + def test_bot(self): + def get_locale(_): + return "en" + + self.i18n = I18nExtension([ + Language("English", "en", { + "hello": "Hello", + "goodbye": "Goodbye", + "english": "English" + }), + Language("French", "fr", { + "hello": "Bonjour", + "goodbye": "Au revoir", + "francais": "Français" + }), + ], bot=commands.Bot("!"), get_locale_func=get_locale, fallback="en") + + self.assertEqual(self.i18n.contextual_get_text("hello"), "Hello") + if __name__ == '__main__': unittest.main(verbosity=2)