1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
import flask
import flask_babel as 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")
with app.test_request_context():
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")
def t(x):
return flask.render_template_string("{{ %s }}" % x)
with app.test_request_context():
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(
"""
{% trans num=3 %}{{ num }} Apple
{%- pluralize %}{{ num }} Apples{% endtrans %}
""",
name="Peter",
).strip()
== "3 Äpfel"
)
def test_lazy_gettext():
app = flask.Flask(__name__)
babel.Babel(app, default_locale="de_DE")
yes = lazy_gettext("Yes")
with app.test_request_context():
assert str(yes) == "Ja"
assert yes.__html__() == "Ja"
get_babel(app).default_locale = "en_US"
with app.test_request_context():
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("%(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("%(num)s Apple", "%(num)s Apples", 2)
with app.test_request_context():
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")
with app.test_request_context():
assert str(first) == "erste"
get_babel(app).default_locale = "en_US"
with app.test_request_context():
assert str(first) == "first"
def test_list_translations():
app = flask.Flask(__name__)
b = babel.Babel(app, default_locale="de_DE")
with app.app_context():
translations = sorted(b.list_translations(), key=str)
assert len(translations) == 3
assert str(translations[0]) == "de"
assert str(translations[1]) == "de_DE"
assert str(translations[2]) == "ja"
def test_list_translations_default_locale_exists():
app = flask.Flask(__name__)
b = babel.Babel(app, default_locale="de")
with app.app_context():
translations = sorted(b.list_translations(), key=str)
assert len(translations) == 2
assert str(translations[0]) == "de"
assert str(translations[1]) == "ja"
def test_no_formatting():
"""
Ensure we don't format strings unless a variable is passed.
"""
app = flask.Flask(__name__)
babel.Babel(app)
with app.test_request_context():
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")
with app.test_request_context():
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")
with app.test_request_context():
assert babel.gettext("first") == "first"
domain.as_default()
assert babel.gettext("first") == "erste"
def test_default_domain():
app = flask.Flask(__name__)
b = babel.Babel(app, default_locale="de_DE", default_domain="test")
with app.test_request_context():
assert babel.gettext("first") == "erste"
def test_multiple_apps():
app1 = flask.Flask(__name__)
b1 = babel.Babel(app1, default_locale="de_DE")
app2 = flask.Flask(__name__)
b2 = babel.Babel(app2, default_locale="de_DE")
with app1.test_request_context() as ctx:
assert babel.gettext("Yes") == "Ja"
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)
def test_cache(mocker):
load_mock = mocker.patch(
"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)
# first request, should load en_US
the_locale = "en_US"
with app.test_request_context() as ctx:
assert b.domain_instance.get_translations_cache(ctx) == {}
assert babel.gettext("Yes") == "Yes"
assert load_mock.call_count == 1
# second request, should use en_US from cache
with app.test_request_context() as ctx:
assert set(b.domain_instance.get_translations_cache(ctx)) == {
("en_US", "messages")
}
assert babel.gettext("Yes") == "Yes"
assert load_mock.call_count == 1
# third request, should load de_DE from cache
the_locale = "de_DE"
with app.test_request_context() as ctx:
assert set(b.domain_instance.get_translations_cache(ctx)) == {
("en_US", "messages")
}
assert babel.gettext("Yes") == "Ja"
assert load_mock.call_count == 2
# now everything is cached, so no more loads should happen!
the_locale = "en_US"
with app.test_request_context() as ctx:
assert set(b.domain_instance.get_translations_cache(ctx)) == {
("en_US", "messages"),
("de_DE", "messages"),
}
assert babel.gettext("Yes") == "Yes"
assert load_mock.call_count == 2
the_locale = "de_DE"
with app.test_request_context() as ctx:
assert set(b.domain_instance.get_translations_cache(ctx)) == {
("en_US", "messages"),
("de_DE", "messages"),
}
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 個"
|