python-babel / flask-babel

i18n and l10n support for Flask based on Babel and pytz
https://python-babel.github.io/flask-babel/
Other
432 stars 159 forks source link

Test with Babel #102

Closed pythonstory closed 8 years ago

pythonstory commented 8 years ago

Hello,

Flask-Babel extension works fine when it is run as an application, but I have a problem when running tests.

(venv) C:\projects\rabiang\repo>python -m flask test
ERROR

======================================================================
ERROR: setUpClass (test_basics.BasicsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\projects\rabiang\repo\tests\test_basics.py", line 17, in setUpClass
    app = create_app(config='../test.cfg', blueprints=blueprints)
  File "C:\projects\rabiang\repo\app\app.py", line 23, in create_app
    configure_extensions(app)
  File "C:\projects\rabiang\repo\app\app.py", line 73, in configure_extensions
    @babel.localeselector
  File "C:\projects\rabiang\venv\lib\site-packages\flask_babel\__init__.py", line 127, in localeselector
    'a localeselector function is already registered'
AssertionError: a localeselector function is already registered

----------------------------------------------------------------------
Ran 0 tests in 0.004s

FAILED (errors=1)

You can check the files related to Babel as follows:

app/app.py tests/test.py run.py babel.cfg test.cfg

I use Blueprints and create_app() factory.

I tried to use @classmethod setUpClass instead of setUp to make sure that call app.py only once, but it doesn't work.

I actually asked this issue at stackoverflow.com, but I didn't get answer at all.

Please, help me. What's wrong with me?

pythonstory commented 8 years ago

I moved @babel.localeselector and @babel.timezoneselector decorated function declaration into app/extensions.py from app/app.py so that they are not loaded.

It seems to be good, but I want to know a better way if you have. Thank you.

zgoda commented 7 years ago

I know this is old and closed with some workaround but I found another. If you follow application structure with app factory then in your configure_extensions() you can just check if babel.locale_selector_func is not None and set up .localeselector only if it not set already.