在Django中使用Mako时如何处理国际化

1 投票
1 回答
1019 浏览
提问于 2025-04-16 09:49

这是代码:

{% load i18n %}
{% trans 'hello test' %}

但是,这段代码无法被 mako 读取,

那么,如何使用 mako 进行国际化呢,

谢谢

1 个回答

1

我查看了文档,里面提到Mako使用Babel来处理国际化(i18n)。所以你可以选择使用这个,或者像@Yuji建议的那样,使用Django的国际化,通过调用任意的Python代码来实现。

不过我觉得使用Babel看起来更好:

# /myproj/babel.cfg

# this loads all Django templates, 
# e.g. /myproj/templates/myapp/hello_world.html
[mako: **/templates/**.*]

# /myproj/templates/myapp/hello_world.html
<html>
   <body>
       ${_('Hello world!')}
   </body>
</html>

撰写回答