|
6 | 6 | sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) |
7 | 7 |
|
8 | 8 | import pickle |
| 9 | +from threading import Thread, Semaphore |
9 | 10 |
|
10 | 11 | import unittest |
11 | 12 | from decimal import Decimal |
12 | 13 | import flask |
13 | 14 | from datetime import datetime, timedelta |
14 | 15 | import flask_babel as babel |
15 | | -from flask_babel import gettext, ngettext, lazy_gettext, get_translations |
| 16 | +from flask_babel import gettext, ngettext, lazy_gettext, lazy_ngettext, get_translations |
16 | 17 | from babel.support import NullTranslations |
17 | 18 | from flask_babel._compat import text_type |
18 | 19 |
|
@@ -204,6 +205,46 @@ def select_locale(): |
204 | 205 | assert str(babel.get_locale()) == 'en_US' |
205 | 206 | assert str(babel.get_locale()) == 'de_DE' |
206 | 207 |
|
| 208 | + def test_force_locale_with_threading(self): |
| 209 | + app = flask.Flask(__name__) |
| 210 | + b = babel.Babel(app) |
| 211 | + |
| 212 | + @b.localeselector |
| 213 | + def select_locale(): |
| 214 | + return 'de_DE' |
| 215 | + |
| 216 | + semaphore = Semaphore(value=0) |
| 217 | + |
| 218 | + def first_request(): |
| 219 | + with app.test_request_context(): |
| 220 | + with babel.force_locale('en_US'): |
| 221 | + assert str(babel.get_locale()) == 'en_US' |
| 222 | + semaphore.acquire() |
| 223 | + |
| 224 | + thread = Thread(target=first_request) |
| 225 | + thread.start() |
| 226 | + |
| 227 | + try: |
| 228 | + with app.test_request_context(): |
| 229 | + assert str(babel.get_locale()) == 'de_DE' |
| 230 | + finally: |
| 231 | + semaphore.release() |
| 232 | + thread.join() |
| 233 | + |
| 234 | + def test_refresh_during_force_locale(self): |
| 235 | + app = flask.Flask(__name__) |
| 236 | + b = babel.Babel(app) |
| 237 | + |
| 238 | + @b.localeselector |
| 239 | + def select_locale(): |
| 240 | + return 'de_DE' |
| 241 | + |
| 242 | + with app.test_request_context(): |
| 243 | + with babel.force_locale('en_US'): |
| 244 | + assert str(babel.get_locale()) == 'en_US' |
| 245 | + babel.refresh() |
| 246 | + assert str(babel.get_locale()) == 'en_US' |
| 247 | + |
207 | 248 |
|
208 | 249 | class NumberFormattingTestCase(unittest.TestCase): |
209 | 250 |
|
@@ -266,6 +307,18 @@ def test_lazy_gettext(self): |
266 | 307 | assert text_type(yes) == 'Yes' |
267 | 308 | assert yes.__html__() == 'Yes' |
268 | 309 |
|
| 310 | + def test_lazy_ngettext(self): |
| 311 | + app = flask.Flask(__name__) |
| 312 | + babel.Babel(app, default_locale='de_DE') |
| 313 | + one_apple = lazy_ngettext(u'%(num)s Apple', u'%(num)s Apples', 1) |
| 314 | + with app.test_request_context(): |
| 315 | + assert text_type(one_apple) == '1 Apfel' |
| 316 | + assert one_apple.__html__() == '1 Apfel' |
| 317 | + two_apples = lazy_ngettext(u'%(num)s Apple', u'%(num)s Apples', 2) |
| 318 | + with app.test_request_context(): |
| 319 | + assert text_type(two_apples) == u'2 Äpfel' |
| 320 | + assert two_apples.__html__() == u'2 Äpfel' |
| 321 | + |
269 | 322 | def test_list_translations(self): |
270 | 323 | app = flask.Flask(__name__) |
271 | 324 | b = babel.Babel(app, default_locale='de_DE') |
|
0 commit comments