diff --git a/flask_babel/__init__.py b/flask_babel/__init__.py index e84e75b..c25155f 100644 --- a/flask_babel/__init__.py +++ b/flask_babel/__init__.py @@ -619,7 +619,7 @@ def get_translations(self): # does not copy _info, plural(), or any other instance variables # populated by GNUTranslations. We probably want to stop using # `support.Translations.merge` entirely. - if hasattr(catalog, "plural"): + if catalog.info() and hasattr(catalog, "plural"): translations.plural = catalog.plural cache[str(locale), self.domain[0]] = translations diff --git a/tests/test_gettext.py b/tests/test_gettext.py index 6a57832..c12d667 100644 --- a/tests/test_gettext.py +++ b/tests/test_gettext.py @@ -91,9 +91,10 @@ def test_list_translations(): with app.app_context(): translations = b.list_translations() - assert len(translations) == 2 + assert len(translations) == 3 assert str(translations[0]) == 'de' - assert str(translations[1]) == 'de_DE' + assert str(translations[1]) == 'ja' + assert str(translations[2]) == 'de_DE' def test_list_translations_default_locale_exists(): @@ -102,8 +103,9 @@ def test_list_translations_default_locale_exists(): with app.app_context(): translations = b.list_translations() - assert len(translations) == 1 + assert len(translations) == 2 assert str(translations[0]) == 'de' + assert str(translations[1]) == 'ja' def test_no_formatting(): @@ -219,3 +221,58 @@ def test_cache(mocker): } assert babel.gettext("Yes") == "Ja" assert load_mock.call_count == 2 + + +def test_plurals(): + + app = flask.Flask(__name__) + + def set_locale(): + return flask.request.environ["LANG"] + + babel.Babel(app, locale_selector=set_locale) + + # Plural-Forms: nplurals=2; plural=(n != 1) + with app.test_request_context(environ_overrides={"LANG": "de_DE"}): + + assert ngettext("%(num)s Apple", "%(num)s Apples", 1) == "1 Apfel" + assert ngettext("%(num)s Apple", "%(num)s Apples", 2) == "2 Äpfel" + + # Plural-Forms: nplurals=1; plural=0; + with app.test_request_context(environ_overrides={"LANG": "ja"}): + + assert ngettext("%(num)s Apple", "%(num)s Apples", 1) == "リンゴ 1 個" + assert ngettext("%(num)s Apple", "%(num)s Apples", 2) == "リンゴ 2 個" + + +def test_plurals_different_domains(): + + app = flask.Flask(__name__) + + app.config.update({ + 'BABEL_TRANSLATION_DIRECTORIES': ';'.join(( + 'translations', + 'translations_different_domain', + )), + 'BABEL_DOMAIN': ';'.join(( + 'messages', + 'myapp', + )), + }) + + def set_locale(): + return flask.request.environ["LANG"] + + babel.Babel(app, locale_selector=set_locale) + + # Plural-Forms: nplurals=2; plural=(n != 1) + with app.test_request_context(environ_overrides={"LANG": "de_DE"}): + + assert ngettext("%(num)s Apple", "%(num)s Apples", 1) == "1 Apfel" + assert ngettext("%(num)s Apple", "%(num)s Apples", 2) == "2 Äpfel" + + # Plural-Forms: nplurals=1; plural=0; + with app.test_request_context(environ_overrides={"LANG": "ja"}): + + assert ngettext("%(num)s Apple", "%(num)s Apples", 1) == "リンゴ 1 個" + assert ngettext("%(num)s Apple", "%(num)s Apples", 2) == "リンゴ 2 個" diff --git a/tests/test_integration.py b/tests/test_integration.py index f3aef3a..78c77e4 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -39,10 +39,11 @@ def test_multiple_directories(): with app.test_request_context(): translations = b.list_translations() - assert(len(translations) == 3) + assert(len(translations) == 4) assert(str(translations[0]) == 'de') - assert(str(translations[1]) == 'de') - assert(str(translations[2]) == 'de_DE') + assert(str(translations[1]) == 'ja') + assert(str(translations[2]) == 'de') + assert(str(translations[3]) == 'de_DE') assert gettext( u'Hello %(name)s!', diff --git a/tests/translations/ja/LC_MESSAGES/messages.mo b/tests/translations/ja/LC_MESSAGES/messages.mo new file mode 100644 index 0000000..18371bd Binary files /dev/null and b/tests/translations/ja/LC_MESSAGES/messages.mo differ diff --git a/tests/translations/ja/LC_MESSAGES/messages.po b/tests/translations/ja/LC_MESSAGES/messages.po new file mode 100644 index 0000000..b645549 --- /dev/null +++ b/tests/translations/ja/LC_MESSAGES/messages.po @@ -0,0 +1,35 @@ +# Japanese translations for PROJECT. +# Copyright (C) 2010 ORGANIZATION +# This file is distributed under the same license as the PROJECT project. +# FIRST AUTHOR , 2010. +# +msgid "" +msgstr "" +"Project-Id-Version: PROJECT VERSION\n" +"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" +"POT-Creation-Date: 2010-05-30 12:56+0200\n" +"PO-Revision-Date: 2024-06-12 14:34+0200\n" +"Last-Translator: FULL NAME \n" +"Language: ja\n" +"Language-Team: ja \n" +"Plural-Forms: nplurals=1; plural=0;\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: Babel 2.14.0\n" + +#: tests.py:94 +#, python-format +msgid "Hello %(name)s!" +msgstr "こんにちは %(name)s!" + +#: tests.py:95 tests.py:96 +#, python-format +msgid "%(num)s Apple" +msgid_plural "%(num)s Apples" +msgstr[0] "リンゴ %(num)s 個" + +#: tests.py:119 +msgid "Yes" +msgstr "はい" +