python-babel / flask-babel

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

Flask-Babel 0.10.0 breaks testing without application context #94

Closed RobertDeRose closed 8 years ago

RobertDeRose commented 8 years ago

In Flask-Babel 0.9.0, testing code that didn't use an Application Context worked, now in Flask-Babel 0.10.0 it throws a runtime exception "RuntimeError: working outside of application context"

This is a breaking change for at least testing. Is there a way to handle testing of code that doesn't go through the application test client?

RobertDeRose commented 8 years ago

I did find a workaround for this, but is this required?

from flask import Flask
from flask.ext.babel import Babel

class TestClass(unittest.TestCase):
    def setUp(self):
        app = Flask(__name__)
        Babel(app)
        self.context = app.test_request_context("/")

    def test_something(self):
        with self.context:
             some_code_that_calls_gettext()
TkTech commented 8 years ago

If you don't have an application context, use the lazy versions.

In previous versions, the translations directory was hardcoded to '/translations'. This is now a configuration option, requiring an application context, allowing you to specify multiple and alternative translation directories and soon domains.

This is about the 10th issue for the same problem, guess I should add a big header.

RobertDeRose commented 8 years ago

Interesting, but why break the old behavior? Why not just fallback to /translations if there is no application context? Also, using the lazy versions is not a real options as this uses speaklater for that, which does not support Python 3. The maintainer of the project also seems to have stop caring about it :(

RobertDeRose commented 8 years ago

@TkTech I don't think you should close these issues, it's clearly a breaking change as this issue clearly points out: https://github.com/python-babel/flask-babel/issues/90