如何为 jinja 模板启用 {% trans %} 标签?

发布于 2024-12-20 15:23:20 字数 2423 浏览 1 评论 0原文

我尝试启用 trans 标签,并制作了一个测试模板 i18n.html:

{% trans %}For sale{% endtrans %}--{{message}}-- {{decimal_format}} 这是我根据手册页编写的Python代码:

from webapp2_extras import i18n as multilingua
import jinja2
from webapp2_extras.i18n import lazy_gettext as gettext
from webapp2_extras.i18n import lazy_gettext as _
from jinja2 import Environment, FileSystemLoader
jinja_environment =  jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
                      extensions=['jinja2.ext.i18n'])

# The code below seems wrong since it is django but it was the only way I could make the page load

jinja_environment.install_gettext_translations(django.utils.translation)

class HelloWorldHandler(webapp2.RequestHandler):
    def get(self):
        # Set the requested locale.
        locale = self.request.GET.get('locale', 'pt')
        multilingua.get_i18n().set_locale(locale)
        message = multilingua.gettext('For sale')
        #django.utils.translation.activate('pt')
        template = jinja_environment.get_template('templates/i18n.html')
    decimal_format = multilingua.I18n(self.request).format_decimal(1000)
        self.response.out.write(template.render(message=message, decimal_format=decimal_format))

如果没有它,我就无法让它工作django,因此我问如何丢失 django 翻译并继续使用 webapp2.i18n + jinja。

主题中也有一个讨论,但我没有参与其中唯一说文档有些不完整或很难找到。您能否回答或评论这是使 trans 标记起作用的推荐方法以及为什么我必须包含 jinja_environment.install_gettext_translations(django.utils.translation)

当我尝试删除对 django 的使用时,我也失去了 webapp2.i18n 的功能。我的语言环境文件都在 locale/... 和 conf/locale.. 中,因为第一个是 webapp2 的默认设置,第二个是 django 翻译的默认设置,所以我真的可以在这里使用一些最佳实践指南来摆脱django 依赖项并使用 webapp2 和 jinja 来呈现我的本地化。

如果有任何帮助,我在尝试删除 django 时确实收到了一条错误消息:

    self.response.out.write(template.render(message=message, decimal_format=decimal_format))
  File "/media/Lexar/montao/montaoproject/jinja2/environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "/media/Lexar/montao/montaoproject/templates/i18n.html", line 2, in top-level template code
    {{ _('For sale') }}--{{message}}--{{decimal_format}}
UndefinedError: 'gettext' is undefined

谢谢

I try to enable the trans tag and I've made a test template i18n.html:

{% trans %}For sale{% endtrans %}--{{message}}--{{decimal_format}}
Here is my python code according to the manpages:

from webapp2_extras import i18n as multilingua
import jinja2
from webapp2_extras.i18n import lazy_gettext as gettext
from webapp2_extras.i18n import lazy_gettext as _
from jinja2 import Environment, FileSystemLoader
jinja_environment =  jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
                      extensions=['jinja2.ext.i18n'])

# The code below seems wrong since it is django but it was the only way I could make the page load

jinja_environment.install_gettext_translations(django.utils.translation)

class HelloWorldHandler(webapp2.RequestHandler):
    def get(self):
        # Set the requested locale.
        locale = self.request.GET.get('locale', 'pt')
        multilingua.get_i18n().set_locale(locale)
        message = multilingua.gettext('For sale')
        #django.utils.translation.activate('pt')
        template = jinja_environment.get_template('templates/i18n.html')
    decimal_format = multilingua.I18n(self.request).format_decimal(1000)
        self.response.out.write(template.render(message=message, decimal_format=decimal_format))

I could not make it work without django and therefore I ask how to lose the django translation and staying with webapp2.i18n + jinja instead.

There was also a discussion in a thread where I'm not the only one saying that documentation is somewhat incomplete or hard to find. Could you please answer or comment which is the recommended way of making the trans tag work and why I must include jinja_environment.install_gettext_translations(django.utils.translation)
?

When I try to remove my use of django I also lose the functions of webapp2.i18n. My locale files are both in locale/... and conf/locale.. since the first is the default for webapp2 and the second is the default for django translations, so I could really use some guidelines for best practice here to get rid of the django dependecies and use webapp2 and jinja for rendering my localizations.

If to any help, I did receive an error message when trying to remove django:

    self.response.out.write(template.render(message=message, decimal_format=decimal_format))
  File "/media/Lexar/montao/montaoproject/jinja2/environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "/media/Lexar/montao/montaoproject/templates/i18n.html", line 2, in top-level template code
    {{ _('For sale') }}--{{message}}--{{decimal_format}}
UndefinedError: 'gettext' is undefined

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

棒棒糖 2024-12-27 15:23:20

查看 Jinja2 的 i18n 扩展文档。调用 install_gettext_translations 基本上设置了 Jinja2 将调用 gettext、ngettext 等的对象,以便在遇到 {% trans %} 标记时翻译字符串。

由于这些函数是在 webapp2.i18n 上定义的(请参阅 此处),jinja2 将成功调用这些函数来检索翻译,具体取决于您对 set_locale 内部的调用的请求。我面前没有代码,但我猜测 gettextwebapp2.i18n 中定义的 company 只是调用 webapp 的代理。 i18.get_i18n().gettext,这就是让这一切顺利进行的魔力。

Take a look at Jinja2's i18n Extension documentation. Calling install_gettext_translations basically sets the object through which Jinja2 will call gettext, ngettext, etc, in order to translate strings when it encounters a {% trans %} tag.

Since those functions are defined on webapp2.i18n (see here), jinja2 will successfully call those functions to retrieve translations, dependent upon your call to set_locale inside of the request. I don't have the code in front of me, but I'd guess that gettext and company defined in webapp2.i18n are merely proxies to call webapp.i18.get_i18n().gettext, which is the magic that makes all of this work.

小梨窩很甜 2024-12-27 15:23:20

这是 Django+jinja2 的工作示例:

from jinja2 import PackageLoader, Environment 
from django.utils import translation

...

jinja_environment = Environment(loader=PackageLoader('website', 'templates'),
                                extensions=['jinja2.ext.i18n'])

jinja_environment.install_gettext_translations(translation)

template = jinja_environment.get_template('test.jinja.html')

page_next_app_table = template.render()

...

在 test.jinja.html 中:

<html>
<b> {{ _( "Traslate This" ) }}:</b>
</html>

Here is a working example for Django+jinja2:

from jinja2 import PackageLoader, Environment 
from django.utils import translation

...

jinja_environment = Environment(loader=PackageLoader('website', 'templates'),
                                extensions=['jinja2.ext.i18n'])

jinja_environment.install_gettext_translations(translation)

template = jinja_environment.get_template('test.jinja.html')

page_next_app_table = template.render()

...

In test.jinja.html:

<html>
<b> {{ _( "Traslate This" ) }}:</b>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文