test_gettext.py
6.3 KB
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
# -*- coding: utf-8 -*-
from __future__ import with_statement
import flask
import flask_babel as babel
from flask_babel import gettext, lazy_gettext, lazy_ngettext, ngettext
def test_basics():
app = flask.Flask(__name__)
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'
def test_template_basics():
app = flask.Flask(__name__)
babel.Babel(app, default_locale='de_DE')
t = lambda x: 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('''
{% 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() == u'3 Äpfel'
def test_lazy_gettext():
app = flask.Flask(__name__)
babel.Babel(app, default_locale='de_DE')
yes = lazy_gettext(u'Yes')
with app.test_request_context():
assert str(yes) == 'Ja'
assert yes.__html__() == 'Ja'
app.config['BABEL_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(u'%(num)s Apple', u'%(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)
with app.test_request_context():
assert str(two_apples) == u'2 Äpfel'
assert two_apples.__html__() == u'2 Äpfel'
def test_lazy_gettext_defaultdomain():
app = flask.Flask(__name__)
b = babel.Babel(app, default_locale='de_DE', default_domain='test')
first = lazy_gettext('first')
with app.test_request_context():
assert str(first) == 'erste'
app.config['BABEL_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')
translations = b.list_translations()
assert len(translations) == 1
assert str(translations[0]) == 'de'
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(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'
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")
@b.localeselector
def select_locale():
return 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