test_integration.py
2.9 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
# -*- coding: utf-8 -*-
from __future__ import with_statement
import pickle
import flask
from babel.support import NullTranslations
import flask_babel as babel
from flask_babel import get_translations, gettext, lazy_gettext
def test_no_request_context():
b = babel.Babel()
app = flask.Flask(__name__)
b.init_app(app)
with app.app_context():
assert isinstance(get_translations(), NullTranslations)
def test_multiple_directories():
"""
Ensure we can load translations from multiple directories.
"""
b = babel.Babel()
app = flask.Flask(__name__)
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) == 2)
assert(str(translations[0]) == 'de')
assert(str(translations[1]) == 'de')
assert gettext(
u'Hello %(name)s!',
name='Peter'
) == 'Hallo Peter!'
def test_multiple_directories_different_domain():
"""
Ensure we can load translations from multiple directories with a
custom 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'
})
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')
assert gettext(
u'Hello %(name)s!',
name='Peter'
) == 'Hallo Peter!'
assert gettext(u'Good bye') == 'Auf Wiedersehen'
def test_different_domain():
"""
Ensure we can load translations from multiple directories.
"""
b = babel.Babel()
app = flask.Flask(__name__)
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) == 1)
assert(str(translations[0]) == 'de')
assert gettext(u'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(u'test')
assert u'Hello %s' % lazy_string == u'Hello test'
def test_lazy_pickling():
lazy_string = lazy_gettext(u'Foo')
pickled = pickle.dumps(lazy_string)
unpickled = pickle.loads(pickled)
assert unpickled == lazy_string