Skip to content

Commit

Permalink
Merge branch 'fix-single-plural-multiple-domains' of github.com:amerc…
Browse files Browse the repository at this point in the history
…ader/flask-babel into amercader-fix-single-plural-multiple-domains
  • Loading branch information
TkTech committed Jun 19, 2024
2 parents 645b489 + 1a5cd7f commit f9fe46a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 7 deletions.
2 changes: 1 addition & 1 deletion flask_babel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 60 additions & 3 deletions tests/test_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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():
Expand Down Expand Up @@ -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 個"
7 changes: 4 additions & 3 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!',
Expand Down
Binary file added tests/translations/ja/LC_MESSAGES/messages.mo
Binary file not shown.
35 changes: 35 additions & 0 deletions tests/translations/ja/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
@@ -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 <EMAIL@ADDRESS>, 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 <EMAIL@ADDRESS>\n"
"Language: ja\n"
"Language-Team: ja <[email protected]>\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 "はい"

0 comments on commit f9fe46a

Please sign in to comment.