Skip to content

Commit

Permalink
Add description, make tests directory, return new errors
Browse files Browse the repository at this point in the history
  • Loading branch information
raclettes committed Apr 18, 2021
1 parent 4f5fe73 commit a2b0832
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 66 deletions.
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,8 @@ i18n = I18n([
>>> # However we can make it not fallback, but this will throw an error if the translation isn't found
>>> i18n.get_text("english", "fr", should_fallback=False)
Traceback (most recent call last):
File "C:\Users\Avery\AppData\Local\Programs\Python\Python39\lib\site-packages\py18n-1.0-py3.9.egg\py18n\i18n.py", line 87, in get_text
File "C:\Users\Avery\AppData\Local\Programs\Python\Python39\lib\site-packages\py18n-1.0-py3.9.egg\py18n\language.py", line 178, in get_text
File "C:\Users\Avery\AppData\Local\Programs\Python\Python39\lib\site-packages\py18n-1.0-py3.9.egg\py18n\language.py", line 66, in _get_translation_from_key
KeyError: 'english'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Avery\AppData\Local\Programs\Python\Python39\lib\site-packages\py18n-1.0-py3.9.egg\py18n\i18n.py", line 91, in get_text
KeyError: 'Translation english not found for fr!'
...
py18n.i18n.InvalidTranslationKeyError: 'Translation foo not found for en!'
```

### Discord
Expand Down
4 changes: 3 additions & 1 deletion py18n/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@

from .i18n import I18n
from .language import Language
from .extension import I18nExtension
from .extension import I18nExtension

__version__ = "v1.1"
16 changes: 7 additions & 9 deletions py18n/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@

from discord.ext import commands

try:
from .i18n import I18n
from .language import Language
except ImportError:
from i18n import I18n
from language import Language
from .i18n import I18n
from .language import Language


class I18nExtension(I18n):
default_i18n_instance = None
Expand All @@ -48,7 +45,7 @@ def __init__(self, languages: List[Language], fallback: Union[str, int], bot: Op
If there is no default i18n instance, this parameter is ignored and
it is always set.
The default is used by :func:`I18nExtension.contextual_get_text`.
"""
super().__init__(languages, fallback)
Expand Down Expand Up @@ -123,7 +120,7 @@ def contextual_get_text(
) -> str:
"""
Wraps :func:`get_text` to use the current context's locale
.. seealso: documentation for :func:`Language.get_text`
Parameters
Expand Down Expand Up @@ -156,4 +153,5 @@ def contextual_get_text(
use_translations=use_translations, should_fallback=should_fallback,
**kwargs)

_ = I18nExtension.contextual_get_text

_ = I18nExtension.contextual_get_text
65 changes: 43 additions & 22 deletions py18n/i18n.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
# Copyright (C) 2021 Avery
#
#
# This file is part of py18n.
#
#
# py18n is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# py18n is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License
# along with py18n. If not, see <http://www.gnu.org/licenses/>.

from typing import List, Union

try:
from .language import Language
except ImportError:
from language import Language
from .language import Language


class Py18nError(KeyError):
pass


class InvalidLocaleError(Py18nError):
def __init__(self, *args, locale: str) -> None:
super().__init__(*args)
self.locale = locale


class InvalidTranslationKeyError(Py18nError):
def __init__(self, *args, key: str) -> None:
super().__init__(*args)
self.key = key


class I18n:
Expand All @@ -33,14 +46,16 @@ def __init__(self, languages: List[Language], fallback: Union[str, int]) -> None
self._fallback = None
if isinstance(fallback, str):
self._fallback = fallback

if self._fallback not in self._languages:
raise KeyError(f"No language found with code {fallback} as fallback")
raise KeyError(
f"No language found with code {fallback} as fallback")
elif isinstance(fallback, int):
self._fallback = self._languages[languages[fallback].code]

if self._fallback is None:
raise KeyError(f"No fallback language set. Check documentation for correct usage")
raise KeyError(
f"No fallback language set. Check documentation for correct usage")

def get_text(
self,
Expand All @@ -53,7 +68,7 @@ def get_text(
) -> str:
"""
Wraps :func:`Language.get_text` to get translation based on the given locale
.. seealso: documentation for :func:`Language.get_text`
Parameters
Expand All @@ -74,31 +89,37 @@ def get_text(
Raises
------
KeyError
If the key could not be found in the locale, nro in the fallback
InvalidLocaleError
If the locale does not exist on this instance
InvalidTranslationKeyError
If the key could not be found in the locale, nor in the fallback
if `should_fallback` is `True`
"""
# Get locale
if locale not in self._languages:
raise KeyError(f"Given locale `{locale}` does not exist!")
raise InvalidLocaleError(
f"Given locale `{locale}` does not exist!", locale=locale)

language = self._languages[locale]

try:
result = language.get_text(key, list_formatter=list_formatter, use_translations=use_translations, **kwargs)
result = language.get_text(
key, list_formatter=list_formatter, use_translations=use_translations, **kwargs)
except KeyError as exc:
# If we shouldn't fallback or this is the fallback
if not should_fallback or locale == self._fallback:
raise KeyError(f"Translation {key} not found for {locale}!") from exc
raise InvalidTranslationKeyError(
f"Translation {key} not found for {locale}!", key=key) from exc
else:
return result

# We only get here if fallback is enabled and the text wasn't found in
# the initial language
try:
result = self._languages[self._fallback].get_text(
key, list_formatter=list_formatter, use_translations=use_translations, **kwargs)
except KeyError as exc:
raise KeyError(f"Translation {key} not found for {locale} nor fallback {self._fallback}") from exc
raise InvalidTranslationKeyError(
f"Translation {key} not found for {locale} nor fallback {self._fallback}", key=key) from exc
else:
return result

9 changes: 7 additions & 2 deletions py18n/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ def get_text(
-------
str
The translated string
Raises
------
KeyError
The translation was not found (raised through `_get_translation_from_key`)
"""
base_string = self._get_translation_from_key(key)

Expand All @@ -191,5 +196,5 @@ def get_text(
**self._translations,
**mapping
}
return base_string.format_map(safedict(**mapping))

return base_string.format_map(safedict(**mapping))
22 changes: 15 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
# Copyright (C) 2021 Avery
#
#
# This file is part of py18n.
#
#
# py18n is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# py18n is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License
# along with py18n. If not, see <http://www.gnu.org/licenses/>.


from os import path
from setuptools import setup

with open(path.join(path.abspath(path.dirname(__file__)), 'README.md'), encoding='utf-8') as f:
long_description = f.read()

setup(name='Py18n',
version='1.0',
version='1.1',
description='I18n for Discord.py',
author='starsflower',
url='https://github.com/starsflower/py18n',
packages=['py18n'],
package_dir={'py18n': './py18n'},
install_requires=['discord.py']
)
install_requires=['discord.py'],

# Description
long_description=long_description,
long_description_content_type='text/markdown'
)
8 changes: 2 additions & 6 deletions py18n/test_extension.py → tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@

import unittest

try:
from .extension import I18nExtension, _
from .language import Language
except ImportError:
from extension import I18nExtension, _
from language import Language
from py18n.extension import I18nExtension, _
from py18n.language import Language


class I18nTesting(unittest.TestCase):
Expand Down
13 changes: 6 additions & 7 deletions py18n/test_i18n.py → tests/test_i18n.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import unittest

try:
from .i18n import I18n
from .language import Language
except ImportError:
from i18n import I18n
from language import Language
from py18n.i18n import I18n, InvalidLocaleError, InvalidTranslationKeyError
from py18n.language import Language


class I18nTesting(unittest.TestCase):
Expand All @@ -30,9 +26,12 @@ def test_basic_get(self):

def test_fallback(self):
self.assertEqual(self.i18n.get_text("english", "fr"), "English")
with self.assertRaises(KeyError):
with self.assertRaises(InvalidTranslationKeyError):
self.i18n.get_text("english", "fr", should_fallback=False)

def test_locale_error(self):
with self.assertRaises(InvalidLocaleError):
self.i18n.get_text("foo", "bar", should_fallback=False)

if __name__ == '__main__':
unittest.main(verbosity=2)
2 changes: 1 addition & 1 deletion py18n/test_language.py → tests/test_language.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from language import Language
from py18n.language import Language

class LanguageTesting(unittest.TestCase):
def setUp(self) -> None:
Expand Down

0 comments on commit a2b0832

Please sign in to comment.