Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
Add fix and tests for RU-locale (#8)
Browse files Browse the repository at this point in the history
* Add fix and tests for RU-locale

* Return test for DE plural locale

* Split tests on plural for differen locales
  • Loading branch information
slego committed Feb 5, 2020
1 parent 01e156b commit 201f5a7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion flask_icu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def format(string, values=None):
ctx = _request_ctx_stack.top
locale = get_locale()
icu_msg = get_message(string)
msg = MessageFormat(icu_msg)
msg = MessageFormat(icu_msg, locale)
if values is not None:
keys = []
vals = []
Expand Down
56 changes: 54 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,58 @@ def test_icu_plural(self):
=0 {no apples} \
one {one apple} \
other {# apples}}.", {'numApples': 3}) == 'I have 3 apples.'

def test_icu_plural_ru(self):
app = flask.Flask(__name__)
icu = ICU(app, default_locale='ru')

with app.test_request_context():
assert format("I have {numApples, plural, \
=0 {no apples} \
one {one apple} \
other {# apples}}.", {'numApples': 0}) == 'У меня нет яблок.'
assert format("I have {numApples, plural, \
=0 {no apples} \
one {one apple} \
other {# apples}}.", {'numApples': 1}) == 'У меня одно яблоко.'
assert format("I have {numApples, plural, \
=0 {no apples} \
one {one apple} \
other {# apples}}.", {'numApples': 2}) == 'У меня 2 яблока.'
assert format("I have {numApples, plural, \
=0 {no apples} \
one {one apple} \
other {# apples}}.", {'numApples': 3}) == 'У меня 3 яблока.'
assert format("I have {numApples, plural, \
=0 {no apples} \
one {one apple} \
other {# apples}}.", {'numApples': 4}) == 'У меня 4 яблока.'
assert format("I have {numApples, plural, \
=0 {no apples} \
one {one apple} \
other {# apples}}.", {'numApples': 5}) == 'У меня 5 яблок.'
assert format("I have {numApples, plural, \
=0 {no apples} \
one {one apple} \
other {# apples}}.", {'numApples': 10}) == 'У меня 10 яблок.'
assert format("I have {numApples, plural, \
=0 {no apples} \
one {one apple} \
other {# apples}}.", {'numApples': 11}) == 'У меня 11 яблок.'
assert format("I have {numApples, plural, \
=0 {no apples} \
one {one apple} \
other {# apples}}.", {'numApples': 20}) == 'У меня 20 яблок.'
assert format("I have {numApples, plural, \
=0 {no apples} \
one {one apple} \
other {# apples}}.", {'numApples': 21}) == 'У меня 21 яблоко.'


def test_icu_plural_de(self):
app = flask.Flask(__name__)
icu = ICU(app, default_locale='de')

with app.test_request_context():
app.config['ICU_DEFAULT_LOCALE'] = 'de'
icu_refresh()
Expand All @@ -192,6 +242,8 @@ def test_icu_plural(self):
one {one apple} \
other {# apples}}.", {'numApples': 3}) == 'Ich habe 3 Äpfeln.'



def test_icu_select(self):
app = flask.Flask(__name__)
icu = ICU(app, default_locale='en')
Expand Down Expand Up @@ -227,8 +279,8 @@ def test_list_translations(self):
app = flask.Flask(__name__)
icu = ICU(app, default_locale='de')
translations = icu.list_translations()
assert len(translations) == 2
assert ('en' in translations and 'de' in translations)
assert len(translations) == 3
assert ('en' in translations and 'de' in translations and 'ru' in translations)


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions tests/translations/ru/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Hello {name}!":"Hello {name}!","{z} and {a} and {2x} and {new} and {0} like this library.":"{z} and {a} and {2x} and {new} and {0} like this library.","I have {numApples, plural, =0 {no apples} one {one apple} other {# apples}}.":"У меня {numApples, plural, =0 {нет яблок} =1 {одно яблоко} one {# яблоко} few {# яблока} other {# яблок}}.","{gender, select, male {He} female {She} other {They}} will respond shortly.":"{gender, select, male {He} female {She} other {They}} will respond shortly."}

2 comments on commit 201f5a7

@ezmiller
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slego Thank you for making this contribution!

@slego
Copy link
Author

@slego slego commented on 201f5a7 Feb 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ezmiller My pleasure. Thank you for supporting and tutoring me! :)

Please sign in to comment.