i18n 与 jinja2 + GAE
我在 google 上搜索了 GAE + jinja i18n 示例,但找不到它。任何人都可以提供链接或工作示例吗?
我的工作使用 django 翻译,我不知道这是否是推荐的方法。
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 = gettext
self.ngettext = ngettext
self.ugettext = ugettext
self.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)
感谢您的任何提示或建议。我还为 i18n 使用自定义请求处理程序:
from django.utils import translation
class 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.environ
self.reset_language()
def reset_language(self):
# Decide the language from Cookies/Headers
language = translation.get_language_from_request(self.request)
translation.activate(language)
self.request.LANGUAGE_CODE = translation.get_language()
# Set headers in response
self.response.headers['Content-Language'] = str(translation.get_language())
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 = gettext
self.ngettext = ngettext
self.ugettext = ugettext
self.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 translation
class 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.environ
self.reset_language()
def reset_language(self):
# Decide the language from Cookies/Headers
language = translation.get_language_from_request(self.request)
translation.activate(language)
self.request.LANGUAGE_CODE = translation.get_language()
# Set headers in response
self.response.headers['Content-Language'] = str(translation.get_language())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另一篇关于如何的博文在 Google AppEngine 上启用 jinja2 和 i18n 翻译
它更详细地介绍了运行 jinja2 + i18n 所需的所有步骤。
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.