librelist archives

« back to archive

Flask-Babel and using url prefix

Flask-Babel and using url prefix

From:
anh le
Date:
2010-10-27 @ 07:44
Hello all,

I'm using Flask-Babel to do some i18n stuffs
.
So far, I can customize the get_locale to extract locale from request.args
(e.g. http://localhost:5000/?lang=vi)

@babel.localeselector
def get_locale():
    return request.args.get('lang', 'en')

If I config the urls to encode locale in the url prefix like
http://localhost:5000/en/,
how can I
1) extract it (other than manually parse request.path) and
2) organize the routings so that I don't have to repeat the <lang> prefix

@app.route('/<lang>/admin')
def admin():
     pass
@app.route('<lang>/post')
def ...

Can app.register_module(admin, url_prefix='<lang>') do that?

thanks,

Re: [flask] Flask-Babel and using url prefix

From:
Armin Ronacher
Date:
2010-10-27 @ 07:55
Hi,

On 10/27/10 9:44 AM, anh le wrote:
> Can app.register_module(admin, url_prefix='<lang>') do that?
Yes, you need a trailing slash though:

    app.register_module(admin, url_prefix='/<lang>')

You could also subclass Flask to override the route function to always 
prefix the URL rule with "/<lang>" unless a certain parameter is set.


Regards,
Armin

Re: [flask] Flask-Babel and using url prefix

From:
anh le
Date:
2010-10-27 @ 08:22
Hi,

On Wed, Oct 27, 2010 at 2:55 PM, Armin Ronacher <armin.ronacher@active-4.com
> wrote:

> Hi,


> On 10/27/10 9:44 AM, anh le wrote:
> > Can app.register_module(admin, url_prefix='<lang>') do that?
> Yes, you need a trailing slash though:
>
>    app.register_module(admin, url_prefix='/<lang>')
>
> You could also subclass Flask to override the route function to always
> prefix the URL rule with "/<lang>" unless a certain parameter is set.
>

Thanks Armin, subclassing Flask is a propably the good way to implement my
stuffs.

Regarding app.register_module, I mean the '<lang>' is a parameter,
not fixed.  Can a module be registered with a paramterized url_prefix and
function get_locale could extract that prefix.

Regards,

Re: [flask] Flask-Babel and using url prefix

From:
Armin Ronacher
Date:
2010-10-27 @ 08:24
Hi,

On 10/27/10 10:22 AM, anh le wrote:
> Regarding app.register_module, I mean the '<lang>' is a parameter,
> not fixed.  Can a module be registered with a paramterized url_prefix and
> function get_locale could extract that prefix.
Yes, that is possible.  There is no restriction on the prefix.


Regards,
Armin

Re: [flask] Flask-Babel and using url prefix

From:
Tijmen Mulder
Date:
2010-10-27 @ 08:41
Hi,

I need a language code at all times but i don't want to hande the language
code in my views so i did a little @app.before_request middleware with:

language_code = request.view_args.pop('language_code', None)
g.context['language_code'] = language_code

I've also overridden url_for to add:
if endpoint != 'static':
    if not 'language_code' in values:
        values['language_code'] = g.context['language_code']

Then in @babel.localeselector:
return g.context['language_code']


Works great.

Regards,

On Wed, Oct 27, 2010 at 10:24 AM, Armin Ronacher <
armin.ronacher@active-4.com> wrote:

> Hi,
>
> On 10/27/10 10:22 AM, anh le wrote:
> > Regarding app.register_module, I mean the '<lang>' is a parameter,
> > not fixed.  Can a module be registered with a paramterized url_prefix and
> > function get_locale could extract that prefix.
> Yes, that is possible.  There is no restriction on the prefix.
>
>
> Regards,
> Armin
>