Localization?

8 views
Skip to first unread message

Voltron

unread,
Mar 12, 2009, 5:20:38 PM3/12/09
to juno-framework
Hi Brian!

Do you have an example of localization in a template?


Thanks

Brian

unread,
Mar 12, 2009, 9:31:49 PM3/12/09
to juno-framework
I haven't put a whole lot of thought into localization, but I can give
it a look. Do you mean getting this set up:

http://jinja.pocoo.org/2/documentation/extensions#id1
http://jinja.pocoo.org/2/documentation/templates#i18n-in-templates

?

Zhang Huangbin

unread,
Mar 12, 2009, 9:54:42 PM3/12/09
to juno-framework
Yes, that's what i need. :)

URGENT

Brian

unread,
Mar 12, 2009, 11:20:50 PM3/12/09
to juno-framework
For Jinja2 this would work for now:

> init({'template_kwargs': {'extensions': ['jinja2.ext.i18n']}})
> config('template_env').install_gettext_translations(translations)

Where translations would be your translations object (http://
jinja.pocoo.org/2/documentation/
extensions#jinja2.Environment.install_gettext_translations).

I don't have a lot of experience doing i18n - is this common enough
where it would be useful to have an option to set it up automatically?

Zhang Huangbin

unread,
Mar 12, 2009, 11:52:07 PM3/12/09
to juno-framework
Finally, i make it work with Brian's help. below is code snippet:

----
import juno
import gettext

rootdir = os.path.abspath(os.path.dirname(__file__)) + '/'

juno.init({
'mode': 'wsgi',
'static_url': '/static/*:file/',
'static_root': rootdir + 'static/',
'static_handler': juno.static_serve,
'template_lib': 'jinja2',
'template_root': rootdir + 'templates/',
'template_kwargs': {
'extensions': ['jinja2.ext.i18n'],
}
})

gettext.install('iredadmin', rootdir + 'i18n', unicode=True)
lang_zh = gettext.translation(
'iredadmin',
rootdir + 'i18n',
languages=['zh_CN'],
)

juno.config('template_env').install_gettext_translations(lang_zh)
----

Big thanks to Brian. :)

--
Best regards.

Zhang Huangbin

- Open Source Mail Server Solution for RHEL/CentOS 5.x:
http://code.google.com/p/iredmail/

Voltron

unread,
Mar 13, 2009, 1:34:11 AM3/13/09
to juno-framework
I think this feature would be common enough, all bigger frameworks
have it. Maybe this would help?

"Babel, A collection of tools for internationalizing Python
applications"

http://babel.edgewall.org/

Voltron

unread,
Mar 13, 2009, 1:36:20 AM3/13/09
to juno-framework
Or this:


http://pypi.python.org/pypi/wsgi18n/



On Mar 13, 4:20 am, Brian <bre...@gmail.com> wrote:

Brian

unread,
Mar 13, 2009, 1:54:31 AM3/13/09
to juno-framework
I did decide to include it, so Zhang's example gets changed a bit:

----
import juno
import gettext

rootdir = os.path.abspath(os.path.dirname(__file__)) + '/'

gettext.install('iredadmin', rootdir + 'i18n', unicode=True)
lang_zh = gettext.translation(
'iredadmin',
rootdir + 'i18n',
languages=['zh_CN'],
)

juno.init({
'mode': 'wsgi',
'static_url': '/static/*:file/',
'static_root': rootdir + 'static/',
'static_handler': juno.static_serve,
'template_lib': 'jinja2',
'template_root': rootdir + 'templates/',
'translations': [lang_zh],
})

----

Basically you can pass a list of translation objects as
'translations' (these objects can come from gettext, Babel, presumably
others (maybe the wsgi18n one, but I couldn't find docs for it)). If
you give it translation objects it automatically adds the i18n
extension and installs each language. An empty list means it doesn't
load the extension.

Only works for Jinja2 right now, I gotta figure out how Mako does
this.

Zhang Huangbin

unread,
Mar 13, 2009, 1:58:38 AM3/13/09
to juno-fr...@googlegroups.com
2009/3/13 Brian <bre...@gmail.com>:

> Only works for Jinja2 right now, I gotta figure out how Mako does

Mako use ${ _('string') } in template file. :)

--
Best Regards.

Zhang Huangbin

- iRedMail: Mail Server Solution for Red Hat(R) Enterprise Linux & CentOS 5.x:
http://code.google.com/p/iredmail/

Brian

unread,
Mar 13, 2009, 2:11:19 AM3/13/09
to juno-framework
On Mar 13, 1:58 am, Zhang Huangbin <michaelbi...@gmail.com> wrote:
> Mako use ${ _('string') } in template file. :)

Ah, clever. Out of curiosity, does that require setup, or does it
just call gettext.gettext with your local language settings?

Zhang Huangbin

unread,
Mar 13, 2009, 2:18:10 AM3/13/09
to juno-fr...@googlegroups.com
2009/3/13 Brian <bre...@gmail.com>:

Read this cookbook i wrote for webpy before:
http://webpy.org/cookbook/i18n_support_in_template_file

I think just use these lines and mako i18n will work:
----
import gettext

lang = gettext.translation('messages', 'i18n', languages=['en_US'])
lang.install()
----

And in mako template file, just use ${ _('string') } should work fine.

Reply all
Reply to author
Forward
0 new messages