Skip to content

Commit

Permalink
fix: Fixes bug where the current locale would fail to be set
Browse files Browse the repository at this point in the history
This also allows bots to be initialised without a seperate function call
  • Loading branch information
raclettes committed May 11, 2021
1 parent 5169a12 commit bd5d30e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion py18n/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
from .language import Language
from .extension import I18nExtension

__version__ = "v1.1"
__version__ = "1.1.1"
29 changes: 24 additions & 5 deletions py18n/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,33 @@
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
----------
languages : List[Language]
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
Expand All @@ -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):
"""
Expand All @@ -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
----------
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions tests/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .test_extension import *
from .test_i18n import *
from .test_language import *

if __name__ == "__main__":
unittest.main(verbosity=2)
28 changes: 24 additions & 4 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")
Expand All @@ -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)

0 comments on commit bd5d30e

Please sign in to comment.