From 38a34587db916bc4295f65e45c89526d5249daa8 Mon Sep 17 00:00:00 2001 From: amercader Date: Wed, 12 Jun 2024 15:58:14 +0200 Subject: [PATCH 1/3] Add tests replicating the issue --- tests/test_gettext.py | 63 +++++++++++++++++- tests/test_integration.py | 7 +- tests/translations/ja/LC_MESSAGES/messages.mo | Bin 0 -> 590 bytes tests/translations/ja/LC_MESSAGES/messages.po | 35 ++++++++++ 4 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 tests/translations/ja/LC_MESSAGES/messages.mo create mode 100644 tests/translations/ja/LC_MESSAGES/messages.po 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 0000000000000000000000000000000000000000..18371bdadabd4a7d815609479f34292158ff290d GIT binary patch literal 590 zcmY*W&ui2`6drBykc&_d5j>0s!BS?Oq}x)myQSORw5}xGB^wKZ9;Um~G-Y-s%uJ;9 zWLgiUf|ueS;8hPE^zZQ!{6BnYp+6pceD51R=6k&NcVYe;f|>_B1+;5%tKLChzrP1O z1bhU{0nPzu_YnFD{0Q*)K0;4`3Gf5pZD92Lg)C-^26yvZ@Vm;OS6m2%!NO*I(L6(M zxj`eX4*A5A!IbQBZ89Z&Jc@^}gI{em9``@X*MOBMQTW*d>lmGLAcTF z_v2tZc1FX5^fYG{ejt5jxsN^SQbJeAGR3avudFUnk5Vv_n19IXq-&qoBy^Ry9(J4l za`Va;GGj@inKXh~rG32hCJgaTHwtk3PV~AHGMN@Eg~&s8El9%ItbW1mFx(JXL+Nzg z8HqwOLAI2h86V61Qkst2!dGpFww!1X1vkrZ8`SA3X}N@Pj&lgH{K&q{1;|@Y?Bh+g#|8EpZWAI hsSk8{!+)Q?psKv6PA;nQXH|Z$%3rsR2dXMRqyIf;sv7_R literal 0 HcmV?d00001 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 "はい" + From dad01d3b237a601ebd58c3eafa0028f21cf9703e Mon Sep 17 00:00:00 2001 From: amercader Date: Wed, 12 Jun 2024 16:41:22 +0200 Subject: [PATCH 2/3] Fix for wrong plurals with multiple domains When translations from different catalogs are being merged, there is a workaround to copy the `plurals()` function from the catalog to the combined `translations` object. If the locale catalog doesn't exist for a particular domain, a default value is copied, overriding the proper `plurals()`function. This results in the plural version of a string not being translated for languages with different plural values. See for instance `test_gettext.py::test_plurals_different_domains`: the Japanese locale exists only in the `messages` domain (`translations` folder) but not on the `myapp` one (`translations_different_domain` folder). First reported in ckan/ckan#8258 --- flask_babel/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_babel/__init__.py b/flask_babel/__init__.py index 9f94d22..78cbed6 100644 --- a/flask_babel/__init__.py +++ b/flask_babel/__init__.py @@ -630,7 +630,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 From e63da291b4dc1a4647fde8154e1eb55120bb90b6 Mon Sep 17 00:00:00 2001 From: Tyler Kennedy Date: Wed, 19 Jun 2024 02:43:29 -0400 Subject: [PATCH 3/3] Lint the entire repo --- docs/conf.py | 21 ++-- tests/test_app_factory.py | 13 +-- tests/test_date_formatting.py | 50 +++++---- tests/test_force_locale.py | 34 +++---- tests/test_gettext.py | 174 ++++++++++++++++---------------- tests/test_integration.py | 135 +++++++++++++------------ tests/test_multiple_apps.py | 14 ++- tests/test_number_formatting.py | 10 +- 8 files changed, 221 insertions(+), 230 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 18928d1..118032e 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -17,12 +17,12 @@ # -- Project information ----------------------------------------------------- -project = 'Flask-Babel' -copyright = '2020, Armin Ronacher' -author = 'Armin Ronacher' +project = "Flask-Babel" +copyright = "2020, Armin Ronacher" +author = "Armin Ronacher" # The full version, including alpha/beta/rc tags -release = '4.0.0' +release = "4.0.0" # -- General configuration --------------------------------------------------- @@ -30,18 +30,15 @@ # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. -extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.viewcode' -] +extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"] # Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +templates_path = ["_templates"] # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This pattern also affects html_static_path and html_extra_path. -exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] # -- Options for HTML output ------------------------------------------------- @@ -49,9 +46,9 @@ # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # -html_theme = 'furo' +html_theme = "furo" # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +html_static_path = ["_static"] diff --git a/tests/test_app_factory.py b/tests/test_app_factory.py index 0dbef3c..084c23d 100644 --- a/tests/test_app_factory.py +++ b/tests/test_app_factory.py @@ -6,19 +6,14 @@ def test_app_factory(): b = babel.Babel() def locale_selector(): - return 'de_DE' + return "de_DE" def create_app(): app_ = flask.Flask(__name__) - b.init_app( - app_, - default_locale='en_US', - locale_selector=locale_selector - ) + b.init_app(app_, default_locale="en_US", locale_selector=locale_selector) return app_ app = create_app() with app.test_request_context(): - assert str(babel.get_locale()) == 'de_DE' - assert babel.gettext(u'Hello %(name)s!', name='Peter') == \ - 'Hallo Peter!' + assert str(babel.get_locale()) == "de_DE" + assert babel.gettext("Hello %(name)s!", name="Peter") == "Hallo Peter!" diff --git a/tests/test_date_formatting.py b/tests/test_date_formatting.py index 545a59d..58b0bbd 100644 --- a/tests/test_date_formatting.py +++ b/tests/test_date_formatting.py @@ -13,37 +13,35 @@ def test_basics(): delta = timedelta(days=6) with app.test_request_context(): - assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00\u202fPM' - assert babel.format_date(d) == 'Apr 12, 2010' - assert babel.format_time(d) == '1:46:00\u202fPM' - assert babel.format_timedelta(delta) == '1 week' - assert babel.format_timedelta(delta, threshold=1) == '6 days' + assert babel.format_datetime(d) == "Apr 12, 2010, 1:46:00\u202fPM" + assert babel.format_date(d) == "Apr 12, 2010" + assert babel.format_time(d) == "1:46:00\u202fPM" + assert babel.format_timedelta(delta) == "1 week" + assert babel.format_timedelta(delta, threshold=1) == "6 days" with app.test_request_context(): - get_babel(app).default_timezone = 'Europe/Vienna' - assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00\u202fPM' - assert babel.format_date(d) == 'Apr 12, 2010' - assert babel.format_time(d) == '3:46:00\u202fPM' + get_babel(app).default_timezone = "Europe/Vienna" + assert babel.format_datetime(d) == "Apr 12, 2010, 3:46:00\u202fPM" + assert babel.format_date(d) == "Apr 12, 2010" + assert babel.format_time(d) == "3:46:00\u202fPM" with app.test_request_context(): - get_babel(app).default_locale = 'de_DE' - assert babel.format_datetime(d, 'long') == \ - '12. April 2010, 15:46:00 MESZ' + get_babel(app).default_locale = "de_DE" + assert babel.format_datetime(d, "long") == "12. April 2010, 15:46:00 MESZ" def test_custom_formats(): app = flask.Flask(__name__) app.config.update( - BABEL_DEFAULT_LOCALE='en_US', - BABEL_DEFAULT_TIMEZONE='Pacific/Johnston' + BABEL_DEFAULT_LOCALE="en_US", BABEL_DEFAULT_TIMEZONE="Pacific/Johnston" ) b = babel.Babel(app) - b.date_formats['datetime'] = 'long' - b.date_formats['datetime.long'] = 'MMMM d, yyyy h:mm:ss a' + b.date_formats["datetime"] = "long" + b.date_formats["datetime.long"] = "MMMM d, yyyy h:mm:ss a" d = datetime(2010, 4, 12, 13, 46) with app.test_request_context(): - assert babel.format_datetime(d) == 'April 12, 2010 3:46:00 AM' + assert babel.format_datetime(d) == "April 12, 2010 3:46:00 AM" def test_custom_locale_selector(): @@ -51,8 +49,8 @@ def test_custom_locale_selector(): b = babel.Babel(app) d = datetime(2010, 4, 12, 13, 46) - the_timezone = 'UTC' - the_locale = 'en_US' + the_timezone = "UTC" + the_locale = "en_US" def select_locale(): return the_locale @@ -64,13 +62,13 @@ def select_timezone(): get_babel(app).timezone_selector = select_timezone with app.test_request_context(): - assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00\u202fPM' + assert babel.format_datetime(d) == "Apr 12, 2010, 1:46:00\u202fPM" - the_locale = 'de_DE' - the_timezone = 'Europe/Vienna' + the_locale = "de_DE" + the_timezone = "Europe/Vienna" with app.test_request_context(): - assert babel.format_datetime(d) == '12.04.2010, 15:46:00' + assert babel.format_datetime(d) == "12.04.2010, 15:46:00" def test_refreshing(): @@ -78,7 +76,7 @@ def test_refreshing(): babel.Babel(app) d = datetime(2010, 4, 12, 13, 46) with app.test_request_context(): - assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00\u202fPM' - get_babel(app).default_timezone = 'Europe/Vienna' + assert babel.format_datetime(d) == "Apr 12, 2010, 1:46:00\u202fPM" + get_babel(app).default_timezone = "Europe/Vienna" babel.refresh() - assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00\u202fPM' + assert babel.format_datetime(d) == "Apr 12, 2010, 3:46:00\u202fPM" diff --git a/tests/test_force_locale.py b/tests/test_force_locale.py index 9239199..4b4858b 100644 --- a/tests/test_force_locale.py +++ b/tests/test_force_locale.py @@ -7,25 +7,25 @@ def test_force_locale(): app = flask.Flask(__name__) - babel.Babel(app, locale_selector=lambda: 'de_DE') + babel.Babel(app, locale_selector=lambda: "de_DE") with app.test_request_context(): - assert str(babel.get_locale()) == 'de_DE' - with babel.force_locale('en_US'): - assert str(babel.get_locale()) == 'en_US' - assert str(babel.get_locale()) == 'de_DE' + assert str(babel.get_locale()) == "de_DE" + with babel.force_locale("en_US"): + assert str(babel.get_locale()) == "en_US" + assert str(babel.get_locale()) == "de_DE" def test_force_locale_with_threading(): app = flask.Flask(__name__) - babel.Babel(app, locale_selector=lambda: 'de_DE') + babel.Babel(app, locale_selector=lambda: "de_DE") semaphore = Semaphore(value=0) def first_request(): with app.test_request_context(): - with babel.force_locale('en_US'): - assert str(babel.get_locale()) == 'en_US' + with babel.force_locale("en_US"): + assert str(babel.get_locale()) == "en_US" semaphore.acquire() thread = Thread(target=first_request) @@ -33,7 +33,7 @@ def first_request(): try: with app.test_request_context(): - assert str(babel.get_locale()) == 'de_DE' + assert str(babel.get_locale()) == "de_DE" finally: semaphore.release() thread.join() @@ -41,14 +41,14 @@ def first_request(): def test_force_locale_with_threading_and_app_context(): app = flask.Flask(__name__) - babel.Babel(app, locale_selector=lambda: 'de_DE') + babel.Babel(app, locale_selector=lambda: "de_DE") semaphore = Semaphore(value=0) def first_app_context(): with app.app_context(): - with babel.force_locale('en_US'): - assert str(babel.get_locale()) == 'en_US' + with babel.force_locale("en_US"): + assert str(babel.get_locale()) == "en_US" semaphore.acquire() thread = Thread(target=first_app_context) @@ -56,7 +56,7 @@ def first_app_context(): try: with app.app_context(): - assert str(babel.get_locale()) == 'de_DE' + assert str(babel.get_locale()) == "de_DE" finally: semaphore.release() thread.join() @@ -64,10 +64,10 @@ def first_app_context(): def test_refresh_during_force_locale(): app = flask.Flask(__name__) - babel.Babel(app, locale_selector=lambda: 'de_DE') + babel.Babel(app, locale_selector=lambda: "de_DE") with app.test_request_context(): - with babel.force_locale('en_US'): - assert str(babel.get_locale()) == 'en_US' + with babel.force_locale("en_US"): + assert str(babel.get_locale()) == "en_US" babel.refresh() - assert str(babel.get_locale()) == 'en_US' + assert str(babel.get_locale()) == "en_US" diff --git a/tests/test_gettext.py b/tests/test_gettext.py index c12d667..e53d60f 100644 --- a/tests/test_gettext.py +++ b/tests/test_gettext.py @@ -1,111 +1,112 @@ import flask import flask_babel as babel -from flask_babel import ( - gettext, - lazy_gettext, - lazy_ngettext, - ngettext, - get_babel -) +from flask_babel import gettext, lazy_gettext, lazy_ngettext, ngettext, get_babel def test_basics(): app = flask.Flask(__name__) - babel.Babel(app, default_locale='de_DE') + babel.Babel(app, default_locale="de_DE") with app.test_request_context(): - assert gettext(u'Hello %(name)s!', name='Peter') == 'Hallo Peter!' - assert ngettext(u'%(num)s Apple', u'%(num)s Apples', 3) == \ - u'3 Äpfel' - assert ngettext(u'%(num)s Apple', u'%(num)s Apples', 1) == \ - u'1 Apfel' + assert gettext("Hello %(name)s!", name="Peter") == "Hallo Peter!" + assert ngettext("%(num)s Apple", "%(num)s Apples", 3) == "3 Äpfel" + assert ngettext("%(num)s Apple", "%(num)s Apples", 1) == "1 Apfel" def test_template_basics(): app = flask.Flask(__name__) - babel.Babel(app, default_locale='de_DE') + babel.Babel(app, default_locale="de_DE") def t(x): - return flask.render_template_string('{{ %s }}' % x) + return flask.render_template_string("{{ %s }}" % x) with app.test_request_context(): - assert t("gettext('Hello %(name)s!', name='Peter')") == \ - u'Hallo Peter!' - assert t("ngettext('%(num)s Apple', '%(num)s Apples', 3)") == \ - u'3 Äpfel' - assert t("ngettext('%(num)s Apple', '%(num)s Apples', 1)") == \ - u'1 Apfel' - assert flask.render_template_string(''' + assert t("gettext('Hello %(name)s!', name='Peter')") == "Hallo Peter!" + assert t("ngettext('%(num)s Apple', '%(num)s Apples', 3)") == "3 Äpfel" + assert t("ngettext('%(num)s Apple', '%(num)s Apples', 1)") == "1 Apfel" + assert ( + flask.render_template_string( + """ {% trans %}Hello {{ name }}!{% endtrans %} - ''', name='Peter').strip() == 'Hallo Peter!' - assert flask.render_template_string(''' + """, + name="Peter", + ).strip() + == "Hallo Peter!" + ) + assert ( + flask.render_template_string( + """ {% trans num=3 %}{{ num }} Apple {%- pluralize %}{{ num }} Apples{% endtrans %} - ''', name='Peter').strip() == u'3 Äpfel' + """, + name="Peter", + ).strip() + == "3 Äpfel" + ) def test_lazy_gettext(): app = flask.Flask(__name__) - babel.Babel(app, default_locale='de_DE') - yes = lazy_gettext(u'Yes') + babel.Babel(app, default_locale="de_DE") + yes = lazy_gettext("Yes") with app.test_request_context(): - assert str(yes) == 'Ja' - assert yes.__html__() == 'Ja' + assert str(yes) == "Ja" + assert yes.__html__() == "Ja" - get_babel(app).default_locale = 'en_US' + get_babel(app).default_locale = "en_US" with app.test_request_context(): - assert str(yes) == 'Yes' - assert yes.__html__() == 'Yes' + assert str(yes) == "Yes" + assert yes.__html__() == "Yes" def test_lazy_ngettext(): app = flask.Flask(__name__) - babel.Babel(app, default_locale='de_DE') - one_apple = lazy_ngettext(u'%(num)s Apple', u'%(num)s Apples', 1) + babel.Babel(app, default_locale="de_DE") + one_apple = lazy_ngettext("%(num)s Apple", "%(num)s Apples", 1) with app.test_request_context(): - assert str(one_apple) == '1 Apfel' - assert one_apple.__html__() == '1 Apfel' - two_apples = lazy_ngettext(u'%(num)s Apple', u'%(num)s Apples', 2) + assert str(one_apple) == "1 Apfel" + assert one_apple.__html__() == "1 Apfel" + two_apples = lazy_ngettext("%(num)s Apple", "%(num)s Apples", 2) with app.test_request_context(): - assert str(two_apples) == u'2 Äpfel' - assert two_apples.__html__() == u'2 Äpfel' + assert str(two_apples) == "2 Äpfel" + assert two_apples.__html__() == "2 Äpfel" def test_lazy_gettext_defaultdomain(): app = flask.Flask(__name__) - babel.Babel(app, default_locale='de_DE', default_domain='test') - first = lazy_gettext('first') + babel.Babel(app, default_locale="de_DE", default_domain="test") + first = lazy_gettext("first") with app.test_request_context(): - assert str(first) == 'erste' + assert str(first) == "erste" - get_babel(app).default_locale = 'en_US' + get_babel(app).default_locale = "en_US" with app.test_request_context(): - assert str(first) == 'first' + assert str(first) == "first" def test_list_translations(): app = flask.Flask(__name__) - b = babel.Babel(app, default_locale='de_DE') + b = babel.Babel(app, default_locale="de_DE") with app.app_context(): translations = b.list_translations() assert len(translations) == 3 - assert str(translations[0]) == 'de' - assert str(translations[1]) == 'ja' - assert str(translations[2]) == 'de_DE' + assert str(translations[0]) == "de" + assert str(translations[1]) == "ja" + assert str(translations[2]) == "de_DE" def test_list_translations_default_locale_exists(): app = flask.Flask(__name__) - b = babel.Babel(app, default_locale='de') + b = babel.Babel(app, default_locale="de") with app.app_context(): translations = b.list_translations() assert len(translations) == 2 - assert str(translations[0]) == 'de' - assert str(translations[1]) == 'ja' + assert str(translations[0]) == "de" + assert str(translations[1]) == "ja" def test_no_formatting(): @@ -116,68 +117,63 @@ def test_no_formatting(): babel.Babel(app) with app.test_request_context(): - assert gettext(u'Test %s') == u'Test %s' - assert gettext(u'Test %(name)s', name=u'test') == u'Test test' - assert gettext(u'Test %s') % 'test' == u'Test test' + assert gettext("Test %s") == "Test %s" + assert gettext("Test %(name)s", name="test") == "Test test" + assert gettext("Test %s") % "test" == "Test test" def test_domain(): app = flask.Flask(__name__) - b = babel.Babel(app, default_locale='de_DE') - domain = babel.Domain(domain='test') + b = babel.Babel(app, default_locale="de_DE") + domain = babel.Domain(domain="test") with app.test_request_context(): - assert domain.gettext('first') == 'erste' - assert babel.gettext('first') == 'first' + assert domain.gettext("first") == "erste" + assert babel.gettext("first") == "first" def test_as_default(): app = flask.Flask(__name__) - b = babel.Babel(app, default_locale='de_DE') - domain = babel.Domain(domain='test') + b = babel.Babel(app, default_locale="de_DE") + domain = babel.Domain(domain="test") with app.test_request_context(): - assert babel.gettext('first') == 'first' + assert babel.gettext("first") == "first" domain.as_default() - assert babel.gettext('first') == 'erste' + assert babel.gettext("first") == "erste" def test_default_domain(): app = flask.Flask(__name__) - b = babel.Babel(app, default_locale='de_DE', default_domain='test') + b = babel.Babel(app, default_locale="de_DE", default_domain="test") with app.test_request_context(): - assert babel.gettext('first') == 'erste' + assert babel.gettext("first") == "erste" def test_multiple_apps(): app1 = flask.Flask(__name__) - b1 = babel.Babel(app1, default_locale='de_DE') + b1 = babel.Babel(app1, default_locale="de_DE") app2 = flask.Flask(__name__) - b2 = babel.Babel(app2, default_locale='de_DE') + b2 = babel.Babel(app2, default_locale="de_DE") with app1.test_request_context() as ctx: - assert babel.gettext('Yes') == 'Ja' + assert babel.gettext("Yes") == "Ja" - assert ('de_DE', 'messages') in b1.domain_instance.get_translations_cache(ctx) + assert ("de_DE", "messages") in b1.domain_instance.get_translations_cache(ctx) with app2.test_request_context() as ctx: - assert 'de_DE', 'messages' not in b2.domain_instance.get_translations_cache(ctx) + assert "de_DE", "messages" not in b2.domain_instance.get_translations_cache(ctx) def test_cache(mocker): load_mock = mocker.patch( - "babel.support.Translations.load", - side_effect=babel.support.Translations.load + "babel.support.Translations.load", side_effect=babel.support.Translations.load ) app = flask.Flask(__name__) - b = babel.Babel( - app, - default_locale="de_DE", - locale_selector=lambda: the_locale - ) + b = babel.Babel(app, default_locale="de_DE", locale_selector=lambda: the_locale) # first request, should load en_US the_locale = "en_US" @@ -249,16 +245,22 @@ 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', - )), - }) + app.config.update( + { + "BABEL_TRANSLATION_DIRECTORIES": ";".join( + ( + "translations", + "translations_different_domain", + ) + ), + "BABEL_DOMAIN": ";".join( + ( + "messages", + "myapp", + ) + ), + } + ) def set_locale(): return flask.request.environ["LANG"] diff --git a/tests/test_integration.py b/tests/test_integration.py index 78c77e4..16a03fa 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -26,29 +26,27 @@ def test_multiple_directories(): b = babel.Babel() app = flask.Flask(__name__) - app.config.update({ - 'BABEL_TRANSLATION_DIRECTORIES': ';'.join(( - 'translations', - 'renamed_translations' - )), - 'BABEL_DEFAULT_LOCALE': 'de_DE' - }) + app.config.update( + { + "BABEL_TRANSLATION_DIRECTORIES": ";".join( + ("translations", "renamed_translations") + ), + "BABEL_DEFAULT_LOCALE": "de_DE", + } + ) b.init_app(app) with app.test_request_context(): translations = b.list_translations() - assert(len(translations) == 4) - assert(str(translations[0]) == 'de') - assert(str(translations[1]) == 'ja') - assert(str(translations[2]) == 'de') - assert(str(translations[3]) == 'de_DE') + assert len(translations) == 4 + assert str(translations[0]) == "de" + assert str(translations[1]) == "ja" + assert str(translations[2]) == "de" + assert str(translations[3]) == "de_DE" - assert gettext( - u'Hello %(name)s!', - name='Peter' - ) == 'Hallo Peter!' + assert gettext("Hello %(name)s!", name="Peter") == "Hallo Peter!" def test_multiple_directories_multiple_domains(): @@ -59,33 +57,36 @@ def test_multiple_directories_multiple_domains(): b = babel.Babel() app = flask.Flask(__name__) - app.config.update({ - 'BABEL_TRANSLATION_DIRECTORIES': ';'.join(( - 'renamed_translations', - 'translations_different_domain', - )), - 'BABEL_DEFAULT_LOCALE': 'de_DE', - 'BABEL_DOMAIN': ';'.join(( - 'messages', - 'myapp', - )), - }) + app.config.update( + { + "BABEL_TRANSLATION_DIRECTORIES": ";".join( + ( + "renamed_translations", + "translations_different_domain", + ) + ), + "BABEL_DEFAULT_LOCALE": "de_DE", + "BABEL_DOMAIN": ";".join( + ( + "messages", + "myapp", + ) + ), + } + ) b.init_app(app) with app.test_request_context(): translations = b.list_translations() - assert(len(translations) == 3) - assert(str(translations[0]) == 'de') - assert(str(translations[1]) == 'de') - assert(str(translations[2]) == 'de_DE') + assert len(translations) == 3 + assert str(translations[0]) == "de" + assert str(translations[1]) == "de" + assert str(translations[2]) == "de_DE" - assert gettext( - u'Hello %(name)s!', - name='Peter' - ) == 'Hallo Peter!' - assert gettext(u'Good bye') == 'Auf Wiedersehen' + assert gettext("Hello %(name)s!", name="Peter") == "Hallo Peter!" + assert gettext("Good bye") == "Auf Wiedersehen" def test_multiple_directories_different_domain(): @@ -96,30 +97,28 @@ def test_multiple_directories_different_domain(): b = babel.Babel() app = flask.Flask(__name__) - app.config.update({ - 'BABEL_TRANSLATION_DIRECTORIES': ';'.join(( - 'translations_different_domain', - 'renamed_translations' - )), - 'BABEL_DEFAULT_LOCALE': 'de_DE', - 'BABEL_DOMAIN': 'myapp' - }) + app.config.update( + { + "BABEL_TRANSLATION_DIRECTORIES": ";".join( + ("translations_different_domain", "renamed_translations") + ), + "BABEL_DEFAULT_LOCALE": "de_DE", + "BABEL_DOMAIN": "myapp", + } + ) b.init_app(app) with app.test_request_context(): translations = b.list_translations() - assert(len(translations) == 3) - assert(str(translations[0]) == 'de') - assert(str(translations[1]) == 'de') - assert(str(translations[2]) == 'de_DE') + assert len(translations) == 3 + assert str(translations[0]) == "de" + assert str(translations[1]) == "de" + assert str(translations[2]) == "de_DE" - assert gettext( - u'Hello %(name)s!', - name='Peter' - ) == 'Hallo Peter!' - assert gettext(u'Good bye') == 'Auf Wiedersehen' + assert gettext("Hello %(name)s!", name="Peter") == "Hallo Peter!" + assert gettext("Good bye") == "Auf Wiedersehen" def test_different_domain(): @@ -129,34 +128,36 @@ def test_different_domain(): b = babel.Babel() app = flask.Flask(__name__) - app.config.update({ - 'BABEL_TRANSLATION_DIRECTORIES': 'translations_different_domain', - 'BABEL_DEFAULT_LOCALE': 'de_DE', - 'BABEL_DOMAIN': 'myapp' - }) + app.config.update( + { + "BABEL_TRANSLATION_DIRECTORIES": "translations_different_domain", + "BABEL_DEFAULT_LOCALE": "de_DE", + "BABEL_DOMAIN": "myapp", + } + ) b.init_app(app) with app.test_request_context(): translations = b.list_translations() - assert(len(translations) == 2) - assert(str(translations[0]) == 'de') - assert(str(translations[1]) == 'de_DE') + assert len(translations) == 2 + assert str(translations[0]) == "de" + assert str(translations[1]) == "de_DE" - assert gettext(u'Good bye') == 'Auf Wiedersehen' + assert gettext("Good bye") == "Auf Wiedersehen" def test_lazy_old_style_formatting(): - lazy_string = lazy_gettext(u'Hello %(name)s') - assert lazy_string % {u'name': u'test'} == u'Hello test' + lazy_string = lazy_gettext("Hello %(name)s") + assert lazy_string % {"name": "test"} == "Hello test" - lazy_string = lazy_gettext(u'test') - assert u'Hello %s' % lazy_string == u'Hello test' + lazy_string = lazy_gettext("test") + assert "Hello %s" % lazy_string == "Hello test" def test_lazy_pickling(): - lazy_string = lazy_gettext(u'Foo') + lazy_string = lazy_gettext("Foo") pickled = pickle.dumps(lazy_string) unpickled = pickle.loads(pickled) diff --git a/tests/test_multiple_apps.py b/tests/test_multiple_apps.py index 6d513e2..6609ab1 100644 --- a/tests/test_multiple_apps.py +++ b/tests/test_multiple_apps.py @@ -6,17 +6,15 @@ def test_multiple_apps(): b = babel.Babel() app1 = flask.Flask(__name__) - b.init_app(app1, default_locale='de_DE') + b.init_app(app1, default_locale="de_DE") app2 = flask.Flask(__name__) - b.init_app(app2, default_locale='en_US') + b.init_app(app2, default_locale="en_US") with app1.test_request_context(): - assert str(babel.get_locale()) == 'de_DE' - assert babel.gettext(u'Hello %(name)s!', name='Peter') == \ - 'Hallo Peter!' + assert str(babel.get_locale()) == "de_DE" + assert babel.gettext("Hello %(name)s!", name="Peter") == "Hallo Peter!" with app2.test_request_context(): - assert str(babel.get_locale()) == 'en_US' - assert babel.gettext(u'Hello %(name)s!', name='Peter') == \ - 'Hello Peter!' + assert str(babel.get_locale()) == "en_US" + assert babel.gettext("Hello %(name)s!", name="Peter") == "Hello Peter!" diff --git a/tests/test_number_formatting.py b/tests/test_number_formatting.py index 2759a51..1a984a9 100644 --- a/tests/test_number_formatting.py +++ b/tests/test_number_formatting.py @@ -11,8 +11,8 @@ def test_basics(): n = 1099 with app.test_request_context(): - assert babel.format_number(n) == u'1,099' - assert babel.format_decimal(Decimal('1010.99')) == u'1,010.99' - assert babel.format_currency(n, 'USD') == '$1,099.00' - assert babel.format_percent(0.19) == '19%' - assert babel.format_scientific(10000) == u'1E4' + assert babel.format_number(n) == "1,099" + assert babel.format_decimal(Decimal("1010.99")) == "1,010.99" + assert babel.format_currency(n, "USD") == "$1,099.00" + assert babel.format_percent(0.19) == "19%" + assert babel.format_scientific(10000) == "1E4"