librelist archives

« back to archive

Flask-Babel change language on user request

Flask-Babel change language on user request

From:
Frank Lazzarini
Date:
2012-05-01 @ 08:12
Hi there,

I've got babel setup and working just fine, with two languages 'de' and 
'fr', I use the .accept_languages.best_match(['de', 'fr'])) to identify 
the browser's language, which works just fine. If i use a French Browser 
the site is shown in the french translation, all other browser languages 
are shown the default german translation. Which is just fine for now. 
But the thing is I want to add the possibility to the user to switch the 
language from the site. But I can't get this to work. Here is briefly 
what I have done so far. I will only show some code, instead of 
everything hope that is o.k



from flask          import Flask, Response, redirect, request, session, 
abort, render_template, flash, url_for, jsonify, send_from_directory, g
from flaskext.babel import Babel

...
app.config['BABEL_DEFAULT_LOCALE'] = 'de'
babel = Babel(app)
...
''' This is my language changer '''
@app.route('/lang/<language>')
def lang(language=None):
     setattr(g, 'lang', language)
     return render_template('home.html')

...
@babel.localeselector
def get_locale():
     browser = request.accept_languages.best_match(['de', 'fr'])
     lang = session.get('lang', browser)
     setattr(g, 'lang', lang)
     return lang


When I try the request on /lang/fr the language on the site home.html 
doesn't change appropriately. Can anyone show me what I am doing wrong, 
or what I should do differently to get this behavior to work...

Thanks a lot for any suggestions in advance.



Re: [flask] Flask-Babel change language on user request

From:
Oliver Berger
Date:
2012-05-01 @ 09:35
Hi,

I think you should put the user-selected language within the lang
function into the session - so that you later can retrieve that...


cheers,
Oliver



On 01.05.2012 10:12, Frank Lazzarini wrote:
> Hi there,
> 
> I've got babel setup and working just fine, with two languages 'de' and 
> 'fr', I use the .accept_languages.best_match(['de', 'fr'])) to identify 
> the browser's language, which works just fine. If i use a French Browser 
> the site is shown in the french translation, all other browser languages 
> are shown the default german translation. Which is just fine for now. 
> But the thing is I want to add the possibility to the user to switch the 
> language from the site. But I can't get this to work. Here is briefly 
> what I have done so far. I will only show some code, instead of 
> everything hope that is o.k
> 
> 
> 
> from flask          import Flask, Response, redirect, request, session, 
> abort, render_template, flash, url_for, jsonify, send_from_directory, g
> from flaskext.babel import Babel
> 
> ...
> app.config['BABEL_DEFAULT_LOCALE'] = 'de'
> babel = Babel(app)
> ...
> ''' This is my language changer '''
> @app.route('/lang/<language>')
> def lang(language=None):
>      setattr(g, 'lang', language)
>      return render_template('home.html')
> 
> ...
> @babel.localeselector
> def get_locale():
>      browser = request.accept_languages.best_match(['de', 'fr'])
>      lang = session.get('lang', browser)
>      setattr(g, 'lang', lang)
>      return lang
> 
> 
> When I try the request on /lang/fr the language on the site home.html 
> doesn't change appropriately. Can anyone show me what I am doing wrong, 
> or what I should do differently to get this behavior to work...
> 
> Thanks a lot for any suggestions in advance.
> 
> 
> 
> 

Re: [flask] Flask-Babel change language on user request

From:
Frank Lazzarini
Date:
2012-05-01 @ 09:43
Hi there,

that's exactly what I am doing with this right?

setattr(g, 'lang', language)

Thanks for helping

On Tue, May 1, 2012 at 11:35 AM, Oliver Berger
<oliver@digitalarchitekt.de>wrote:

> Hi,
>
> I think you should put the user-selected language within the lang
> function into the session - so that you later can retrieve that...
>
>
> cheers,
> Oliver
>
>
>
> On 01.05.2012 10:12, Frank Lazzarini wrote:
> > Hi there,
> >
> > I've got babel setup and working just fine, with two languages 'de' and
> > 'fr', I use the .accept_languages.best_match(['de', 'fr'])) to identify
> > the browser's language, which works just fine. If i use a French Browser
> > the site is shown in the french translation, all other browser languages
> > are shown the default german translation. Which is just fine for now.
> > But the thing is I want to add the possibility to the user to switch the
> > language from the site. But I can't get this to work. Here is briefly
> > what I have done so far. I will only show some code, instead of
> > everything hope that is o.k
> >
> >
> >
> > from flask          import Flask, Response, redirect, request, session,
> > abort, render_template, flash, url_for, jsonify, send_from_directory, g
> > from flaskext.babel import Babel
> >
> > ...
> > app.config['BABEL_DEFAULT_LOCALE'] = 'de'
> > babel = Babel(app)
> > ...
> > ''' This is my language changer '''
> > @app.route('/lang/<language>')
> > def lang(language=None):
> >      setattr(g, 'lang', language)
> >      return render_template('home.html')
> >
> > ...
> > @babel.localeselector
> > def get_locale():
> >      browser = request.accept_languages.best_match(['de', 'fr'])
> >      lang = session.get('lang', browser)
> >      setattr(g, 'lang', lang)
> >      return lang
> >
> >
> > When I try the request on /lang/fr the language on the site home.html
> > doesn't change appropriately. Can anyone show me what I am doing wrong,
> > or what I should do differently to get this behavior to work...
> >
> > Thanks a lot for any suggestions in advance.
> >
> >
> >
> >
>