Skip to content

Commit 38a3458

Browse files
committed
Add tests replicating the issue
1 parent a754ead commit 38a3458

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed

tests/test_gettext.py

+60-3
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ def test_list_translations():
9191

9292
with app.app_context():
9393
translations = b.list_translations()
94-
assert len(translations) == 2
94+
assert len(translations) == 3
9595
assert str(translations[0]) == 'de'
96-
assert str(translations[1]) == 'de_DE'
96+
assert str(translations[1]) == 'ja'
97+
assert str(translations[2]) == 'de_DE'
9798

9899

99100
def test_list_translations_default_locale_exists():
@@ -102,8 +103,9 @@ def test_list_translations_default_locale_exists():
102103

103104
with app.app_context():
104105
translations = b.list_translations()
105-
assert len(translations) == 1
106+
assert len(translations) == 2
106107
assert str(translations[0]) == 'de'
108+
assert str(translations[1]) == 'ja'
107109

108110

109111
def test_no_formatting():
@@ -219,3 +221,58 @@ def test_cache(mocker):
219221
}
220222
assert babel.gettext("Yes") == "Ja"
221223
assert load_mock.call_count == 2
224+
225+
226+
def test_plurals():
227+
228+
app = flask.Flask(__name__)
229+
230+
def set_locale():
231+
return flask.request.environ["LANG"]
232+
233+
babel.Babel(app, locale_selector=set_locale)
234+
235+
# Plural-Forms: nplurals=2; plural=(n != 1)
236+
with app.test_request_context(environ_overrides={"LANG": "de_DE"}):
237+
238+
assert ngettext("%(num)s Apple", "%(num)s Apples", 1) == "1 Apfel"
239+
assert ngettext("%(num)s Apple", "%(num)s Apples", 2) == "2 Äpfel"
240+
241+
# Plural-Forms: nplurals=1; plural=0;
242+
with app.test_request_context(environ_overrides={"LANG": "ja"}):
243+
244+
assert ngettext("%(num)s Apple", "%(num)s Apples", 1) == "リンゴ 1 個"
245+
assert ngettext("%(num)s Apple", "%(num)s Apples", 2) == "リンゴ 2 個"
246+
247+
248+
def test_plurals_different_domains():
249+
250+
app = flask.Flask(__name__)
251+
252+
app.config.update({
253+
'BABEL_TRANSLATION_DIRECTORIES': ';'.join((
254+
'translations',
255+
'translations_different_domain',
256+
)),
257+
'BABEL_DOMAIN': ';'.join((
258+
'messages',
259+
'myapp',
260+
)),
261+
})
262+
263+
def set_locale():
264+
return flask.request.environ["LANG"]
265+
266+
babel.Babel(app, locale_selector=set_locale)
267+
268+
# Plural-Forms: nplurals=2; plural=(n != 1)
269+
with app.test_request_context(environ_overrides={"LANG": "de_DE"}):
270+
271+
assert ngettext("%(num)s Apple", "%(num)s Apples", 1) == "1 Apfel"
272+
assert ngettext("%(num)s Apple", "%(num)s Apples", 2) == "2 Äpfel"
273+
274+
# Plural-Forms: nplurals=1; plural=0;
275+
with app.test_request_context(environ_overrides={"LANG": "ja"}):
276+
277+
assert ngettext("%(num)s Apple", "%(num)s Apples", 1) == "リンゴ 1 個"
278+
assert ngettext("%(num)s Apple", "%(num)s Apples", 2) == "リンゴ 2 個"

tests/test_integration.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ def test_multiple_directories():
3939
with app.test_request_context():
4040
translations = b.list_translations()
4141

42-
assert(len(translations) == 3)
42+
assert(len(translations) == 4)
4343
assert(str(translations[0]) == 'de')
44-
assert(str(translations[1]) == 'de')
45-
assert(str(translations[2]) == 'de_DE')
44+
assert(str(translations[1]) == 'ja')
45+
assert(str(translations[2]) == 'de')
46+
assert(str(translations[3]) == 'de_DE')
4647

4748
assert gettext(
4849
u'Hello %(name)s!',
590 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Japanese translations for PROJECT.
2+
# Copyright (C) 2010 ORGANIZATION
3+
# This file is distributed under the same license as the PROJECT project.
4+
# FIRST AUTHOR <EMAIL@ADDRESS>, 2010.
5+
#
6+
msgid ""
7+
msgstr ""
8+
"Project-Id-Version: PROJECT VERSION\n"
9+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
10+
"POT-Creation-Date: 2010-05-30 12:56+0200\n"
11+
"PO-Revision-Date: 2024-06-12 14:34+0200\n"
12+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
13+
"Language: ja\n"
14+
"Language-Team: ja <LL@li.org>\n"
15+
"Plural-Forms: nplurals=1; plural=0;\n"
16+
"MIME-Version: 1.0\n"
17+
"Content-Type: text/plain; charset=utf-8\n"
18+
"Content-Transfer-Encoding: 8bit\n"
19+
"Generated-By: Babel 2.14.0\n"
20+
21+
#: tests.py:94
22+
#, python-format
23+
msgid "Hello %(name)s!"
24+
msgstr "こんにちは %(name)s!"
25+
26+
#: tests.py:95 tests.py:96
27+
#, python-format
28+
msgid "%(num)s Apple"
29+
msgid_plural "%(num)s Apples"
30+
msgstr[0] "リンゴ %(num)s 個"
31+
32+
#: tests.py:119
33+
msgid "Yes"
34+
msgstr "はい"
35+

0 commit comments

Comments
 (0)