Hi, Armin.

On Oct 12, 4:23 pm, Armin Ronacher <armin.ronac...@active-4.com>
wrote:
> Something like this:
>
>     @contextfilter
>     def template_gettext(context, string):
>         language = context['LANGUAGE']
>         return get_translator_for(language).ugettext(string)
>
>     @contextfilter
>     def template_context(context, s, p, n):
>         language = context['LANGUAGE']
>         return get_translator_for(language).ungettext(s, p, n)

What i did was try to override _() which provided by Jinja2, and pass
'_' to Jinja2 as globals.

----
# Object which stored all translations, used for run-time language
switch.
cfg.allTranslations = {} # web.storage()

def get_translation(lang):
    # Init translations.
    if cfg.allTranslations.has_key(lang):
        translations = cfg.allTranslations[lang]
    elif lang == 'en_US':
        translations = gettext.NullTranslations()
    else:
        try:
            translations = gettext.translation(
                    'messages',
                    cfg['rootdir'] + 'i18n',
                    languages=[lang],
                    )
        except IOError:
            translations = gettext.NullTranslations()
    return translations

# Init translation for default language.
cfg.allTranslations[lang] = get_translation(lang)

# Custom _(), used to override _() which provided by Jinja2.
def _(s):
    lang = session.get('lang', 'en_US')
    #return get_translation(lang).ugettext(s)

    # debug
    a = get_translation(lang).ugettext(s)
    print >> sys.stderr, a
    return a

# init render
render = render_jinja(
        tmpldir,                           # template dir.
        extensions = ['jinja2.ext.i18n'],   # Jinja2 extensions.
        encoding = 'utf-8',                 # Encoding.
        globals = {
            'skin': cfg.general.get('skin', 'default'), # Used for
static files.
            'session': web.config._session,  # Used for session.
            'ctx': web.ctx,                  # Used to get 'homepath'.
            '_': _,                         # Override _() which
provided by Jinja2.
            },
        )
----

Error msg:

--------
Traceback (most recent call last):
  File "/usr/lib/python2.4/site-packages/web/application.py", line
241, in process
    return self.handle()
  File "/usr/lib/python2.4/site-packages/web/application.py", line
232, in handle
    return self._delegate(fn, self.fvars, args)
  File "/usr/lib/python2.4/site-packages/web/application.py", line
411, in _delegate
    return handle_class(cls)
  File "/usr/lib/python2.4/site-packages/web/application.py", line
386, in handle_class
    return tocall(*args)
  File "/var/www/iredadmin/controllers/ldap/basic.py", line 24, in GET
    return render.login(msg=msg)
  File "/usr/lib64/python2.4/site-packages/jinja2/environment.py",
line 669, in render
    return self.environment.handle_exception(exc_info, True)
  File "/var/www/iredadmin/libs/..//templates/default/ldap/
login.html", line 3, in top-level template code
    {% from "msgHandlers.html" import loginMsgHandler with context %}
  File "/var/www/iredadmin/libs/..//templates/default/ldap/
layout.html", line 6, in top-level template code
    <title>{% block title %}{%endblock%}</title>
  File "/var/www/iredadmin/libs/..//templates/default/ldap/
login.html", line 5, in block "title"
    {% block title %}{{ _('Login to manage your mail domains & users')
|title }}{% endblock %}
  File "/usr/lib64/python2.4/site-packages/jinja2/filters.py", line
136, in do_title
    return soft_unicode(s).title()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position
0: ordinal not in range(128)
--------

Could you please help me to find out the root case?
Thanks very much. :)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pocoo-libs" group.
To post to this group, send email to pocoo-libs@googlegroups.com
To unsubscribe from this group, send email to 
pocoo-libs+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/pocoo-libs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to