i18n with jinja2 + GAE

2024/11/15 13:41:38

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())
Answer

Another blog post on how to Enable jinja2 and i18n translations on Google AppEngine

It's kind of more detailed with all the steps needed to run jinja2 + i18n.

https://en.xdnf.cn/q/72109.html

Related Q&A

Interpolating one time series onto another in pandas

I have one set of values measured at regular times. Say:import pandas as pd import numpy as np rng = pd.date_range(2013-01-01, periods=12, freq=H) data = pd.Series(np.random.randn(len(rng)), index=rng)…

Reference class variable in a comprehension of another class variable

This may be a simple question, but Im having trouble making a unique search for it. I have a class that defines a static dictionary, then attempts to define a subset of that dictionary, also statically…

Pyspark module not found

Im trying to execute a simple Pyspark job in Yarn. This is the code:from pyspark import SparkConf, SparkContextconf = (SparkConf().setMaster("yarn-client").setAppName("HDFS Filter")…

Multiple windows in PyQt4?

Ive just begun using pyqt4. I followed a tutorial (http://zetcode.com/tutorials/pyqt4/) One thing that puzzles me is this part:def main():app = QtGui.QApplication(sys.argv)ex = GUI()sys.exit(app.exec()…

Fill missing timeseries data using pandas or numpy

I have a list of dictionaries which looks like this :L=[ { "timeline": "2014-10", "total_prescriptions": 17 }, { "timeline": "2014-11", "total_…

Can Biopython perform Seq.find() accounting for ambiguity codes

I want to be able to search a Seq object for a subsequnce Seq object accounting for ambiguity codes. For example, the following should be true:from Bio.Seq import Seq from Bio.Alphabet.IUPAC import IUP…

MySQL and lock a table, read, and then truncate

I am using mysqldb in python.I need to do the following for a table.1) Lock 2) Read 3) Truncate the table 4) UnlockWhen I run the below code, I get the below error. So, I am rather unsure on how to lo…

Train and predict on variable length sequences

Sensors (of the same type) scattered on my site are manually reporting on irregular intervals to my backend. Between reports the sensors aggregate events and report them as a batch. The following datas…

What should a Python project structure look like for Travis CI to find and run tests?

I currently have a project with the following .travis.yml file:language: python install: "pip install tox" script: "tox"Locally, tox properly executes and runs 35 tests, but on Trav…

Having trouble building a Dns Packet in Python

Im trying to build a dns packet to send over a socket. I dont want to use any libraries because I want direct access to the socket variable that sends it. Whenever I send the DNS packet, wireshark says…