[New cookbook article] Run-time language switch

391 views
Skip to first unread message

Zhang Huangbin

unread,
Oct 22, 2009, 11:26:13 PM10/22/09
to we...@googlegroups.com
Hi, all.

Just want to let you know there's a simple tutorial used to implement
run-time language switch, hope it's useful for someone need it.
http://webpy.org/cookbook/runtime-language-switch

Enjoy

--
Best Regards.

Zhang Huangbin

- Open Source Mail Server Solution for Red Hat(R) Enterprise Linux,
CentOS, Debian, Ubuntu: http://www.iredmail.org/

Anand Chitipothu

unread,
Oct 23, 2009, 12:12:46 AM10/23/09
to we...@googlegroups.com
On Fri, Oct 23, 2009 at 8:56 AM, Zhang Huangbin <michae...@gmail.com> wrote:
>
> Hi, all.
>
> Just want to let you know there's a simple tutorial used to implement
> run-time language switch, hope it's useful for someone need it.
> http://webpy.org/cookbook/runtime-language-switch

I generally do it something like this:

def i18n_loadhook():
web.ctx.lang = web.input(lang=None, _method="GET").lang

app.app_processor(web.loadhook(i18n_loadhook))

def custom_gettext(string):
"""Translate a given string to the language of the application."""
translation = web.ctx.lang and load_translations(web.ctx.lang)
if translation is None:
return unicode(string)
return translation.ugettext(string)

Zhang Huangbin

unread,
Oct 23, 2009, 1:29:53 AM10/23/09
to web.py


On Oct 23, 12:12 pm, Anand Chitipothu <anandol...@gmail.com> wrote:
> I generally do it something like this:
>
> def i18n_loadhook():
>     web.ctx.lang = web.input(lang=None, _method="GET").lang
>
> app.app_processor(web.loadhook(i18n_loadhook))
>
> def custom_gettext(string):
>     """Translate a given string to the language of the application."""
>     translation = web.ctx.lang and load_translations(web.ctx.lang)
>     if translation is None:
>         return unicode(string)
>     return translation.ugettext(string)

Already append your method as reference in cookbook. :)
http://webpy.org/cookbook/runtime-language-switch

Anand Chitipothu

unread,
Oct 23, 2009, 3:24:51 AM10/23/09
to we...@googlegroups.com
> def i18n_loadhook():
>    web.ctx.lang = web.input(lang=None, _method="GET").lang
>
> app.app_processor(web.loadhook(i18n_loadhook))

Oops! It should be:

app.add_processor(web.loadhook(i18n_loadhook))

Reply all
Reply to author
Forward
0 new messages