I googled for a GAE + jinja i18n example but could not find it. Can anyone provide a link or working example?
My effort uses the django translations and I don't know if this is the recommend way of doing it.
import jinja2
from django.utils import translation
from django.utils.translation import gettext, ngettext, ugettext, ungettext, get_language, activate
class DjangoTranslator(object):def __init__(self):self.gettext = gettextself.ngettext = ngettextself.ugettext = ugettextself.ungettext = ungettext
from jinja2 import Environment, FileSystemLoader
class DjangoEnvironment(jinja2.Environment):def get_translator(self, context):return DjangoTranslator()jinja_environment = DjangoEnvironment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), extensions=['jinja2.ext.i18n'])
jinja_environment.install_gettext_translations(translation)
Thanks for any hint or advice. I also use a custom request handler for i18n:
from django.utils import translationclass I18NHandler(webapp2.RequestHandler):def render_template(self, file, template_args):path = os.path.join(os.path.dirname(__file__), 'templates',file)self.response.out.write(template.render(path, template_args))def initialize(self, request, response):webapp2.RequestHandler.initialize(self, request, response)self.request.COOKIES = Cookies(self)self.request.META = os.environself.reset_language()def reset_language(self):# Decide the language from Cookies/Headerslanguage = translation.get_language_from_request(self.request)translation.activate(language)self.request.LANGUAGE_CODE = translation.get_language()# Set headers in responseself.response.headers['Content-Language'] = str(translation.get_language())