【问题标题】:Using Babel with Mako templates in tornado Web Application在 tornado Web 应用程序中使用 Babel 和 Mako 模板
【发布时间】:2015-01-06 21:02:26
【问题描述】:

我有一个 Tornado 应用程序,它使用 Mako 作为模板。使用 Babel 我能够创建翻译(mo 文件),但是当我无法呈现翻译后的字符串时。

我的 html 块(index.mako):

`<li> <a id="login"> ${_('Login')} / ${_('Signup')} </a></li>`

handler.py 类 HomeHandler(BaseHandler):

    def get(self):
        hometemplate = makoLookup.get_template("index.mako")
        response_str = hometemplate.render()
        self.write(response_str)

response_str 返回 &lt;li&gt; &lt;a id="login"&gt; Login / Signup &lt;/a&gt;&lt;/li&gt;

如何在 mako 中实现国际化?我搜索的所有文档都解释了提取翻译,但找不到任何关于在 mako 模板中使用翻译的文档。

我尝试过使用 Tornado-Babel,但找不到与 mako 模板集成的文档。

【问题讨论】:

    标签: internationalization tornado mako babeljs


    【解决方案1】:

    使用 TornadoBabelMixin。我将 self._ 传递给我的模板渲染函数。现在我的处理程序代码如下所示。

    class HomeHandler(TornadoBabelMixin, BaseHandler):
        def get_user_locale(self):
            return locale.get("en_US")
    
        def get(self):
            hometemplate = makoLookup.get_template("index.mako")
            response_str = hometemplate.render(_ = self._)
            self.write(response_str)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-08
      • 2014-09-29
      • 1970-01-01
      • 2013-01-17
      • 2011-10-14
      • 2016-11-03
      • 1970-01-01
      • 2012-01-01
      相关资源
      最近更新 更多