I tested Jinja2 i18n extension support in Juno[1] framework, it works
[2]. but it doesn't translate messages in webpy:
----
# Add 'translations' in __init__.
class render_jinja:
def __init__(self, *a, **kwargs):
extensions = kwargs.pop('extensions', [])
translations = kwargs.pop('translations', None)
globals = kwargs.pop('globals', {})
from jinja2 import Environment,FileSystemLoader
self._lookup = Environment(loader=FileSystemLoader(*a,
**kwargs), extensions=extensions)
if translations is not None:
self._lookup.install_gettext_translations(translations)
self._lookup.globals.update(globals)
def __getattr__(self, name):
# Assuming all templates end with .html
path = name + '.html'
t = self._lookup.get_template(path, globals={'_':_})
return t.render
----
in code.py
----
from web.contrib.template import render_jinja
import gettext
from gettext import gettext as _
gettext.install('appname', 'i18n', unicode=1)
lang_zh = gettext.translation('appname', 'i18n', languages=['zh_CN'])
render = render_jinja(
'templates',
extensions=['jinja2.ext.i18n'],
translations=lang_zh,
encoding = 'utf-8',
globals={'_': _,}
)
----
template file:
----
{{ _('Whitelist') }}
----
Translated msg in i18n/zh_CN/LC_MESSAGES/appname.po:
----
msgid "Whitelist"
msgstr "translated msg: whitelist"
----
Use msgfmt to convert po file:
----
# msgfmt -o i18n/zh_CN/LC_MESSAGES/appname.mo i18n/zh_CN/LC_MESSAGES/
appname.po
----
Refer to Jinja2 documentation[3], it should work. but doesn't. I'm
confused. :(
[1] Juno framework:
http://github.com/breily/juno/tree/master
[2] Localization?
http://groups.google.com/group/juno-framework/browse_thread/thread/b46d246ad525768f
[3] Jinja2 i18n extensions:
http://jinja.pocoo.org/2/documentation/extensions#id1
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---