@@ -91,9 +91,10 @@ def test_list_translations():
91
91
92
92
with app .app_context ():
93
93
translations = b .list_translations ()
94
- assert len (translations ) == 2
94
+ assert len (translations ) == 3
95
95
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'
97
98
98
99
99
100
def test_list_translations_default_locale_exists ():
@@ -102,8 +103,9 @@ def test_list_translations_default_locale_exists():
102
103
103
104
with app .app_context ():
104
105
translations = b .list_translations ()
105
- assert len (translations ) == 1
106
+ assert len (translations ) == 2
106
107
assert str (translations [0 ]) == 'de'
108
+ assert str (translations [1 ]) == 'ja'
107
109
108
110
109
111
def test_no_formatting ():
@@ -219,3 +221,58 @@ def test_cache(mocker):
219
221
}
220
222
assert babel .gettext ("Yes" ) == "Ja"
221
223
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 個"
0 commit comments