New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to correctly work out of context #137
Comments
I also see this function:
I track codes to this function and find the ctx has the attribute 'babel_translations', and this translation catalog seems to be related with the default locale. Is it a bug, or I can get the locale from 'babel_localeselector'?? |
@babel.localeselector
def get_locale():
return request.accept_languages.best_match(['zh', 'en']) is what decided which locale to use. If it returns For example, if you would want to use locale @babel.localeselector
def get_locale():
return 'zh' Probably you want to use something smarter there. I think you should take a look at |
thanks, @tvuotila I did not use |
Hi, I'm still unable to use this even with @task
def async_translation_test():
from instababy import app
with force_locale("fi"):
output = _("Test notification 4 %(name)s", name="Test")
app.logger.debug(output) I have working translations on HTML -templates and routines that use requests, but somehow this doesn't seem to work. The logger just messages the default English translation and not Finnish one. |
You need the request for this to work. @task
def async_translation_test():
from instababy import app
with app.test_request_context():
with force_locale("fi"):
output = _("Test notification 4 %(name)s", name="Test")
app.logger.debug(output) |
Ah thanks for the reply. I assumed test_request_context() was only used for testing, but seems it is used in variety of situations. :) |
Uff.. somehow doesn't seem to work :/ I'll try to get more info to the get bottom of this. |
Hi, all. I have two subprocesses. one for flask web(called web.py), and one for some background tasks(called task.py).
flask-babel works inside the web context, but also the task subprocess will need some translations.
so, I import web app into the task.py and use the app.test_request_context in order to make translations work for me.
it works at first but then I found task.py will also get the default language only.
here is the web.py:
task.py
so how can I get the correct locale before I return the translation?? TIA :)
The text was updated successfully, but these errors were encountered: