-
-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathtest_force_locale.py
73 lines (53 loc) · 1.94 KB
/
test_force_locale.py
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
from threading import Semaphore, Thread
import flask
import flask_babel as babel
def test_force_locale():
app = flask.Flask(__name__)
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'
def test_force_locale_with_threading():
app = flask.Flask(__name__)
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'
semaphore.acquire()
thread = Thread(target=first_request)
thread.start()
try:
with app.test_request_context():
assert str(babel.get_locale()) == 'de_DE'
finally:
semaphore.release()
thread.join()
def test_force_locale_with_threading_and_app_context():
app = flask.Flask(__name__)
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'
semaphore.acquire()
thread = Thread(target=first_app_context)
thread.start()
try:
with app.app_context():
assert str(babel.get_locale()) == 'de_DE'
finally:
semaphore.release()
thread.join()
def test_refresh_during_force_locale():
app = flask.Flask(__name__)
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'
babel.refresh()
assert str(babel.get_locale()) == 'en_US'