Python 如何在i18n中使用uo-in-mako渲染模板?
现在我正在使用金字塔框架和mako模板引擎。并希望添加i18n功能 如果我编写以下代码,则没有问题:Python 如何在i18n中使用uo-in-mako渲染模板?,python,internationalization,rendering,pyramid,mako,Python,Internationalization,Rendering,Pyramid,Mako,现在我正在使用金字塔框架和mako模板引擎。并希望添加i18n功能 如果我编写以下代码,则没有问题: myprj/templates/index.html <h1>${_('Home')}</h1> 并在此文件中写入代码: myprj/templates/_detail_panel_a.html <h1>${_('Detail')}</h1> 并在\uuuu init\uuuuu.py文件中调用它: myprj/myprj/__init__
myprj/templates/index.html
<h1>${_('Home')}</h1>
并在此文件中写入代码:
myprj/templates/_detail_panel_a.html
<h1>${_('Detail')}</h1>
并在\uuuu init\uuuuu.py
文件中调用它:
myprj/myprj/__init__.py
config.add_subscriber('myprj.subscribers.add_renderer_globals', 'pyramid.events.BeforeRender')
我不知道为什么它是不工作时,我使用渲染模板页面。我认为,如果有必要定义u事件,不仅要定义request.translate
,还要定义类似于render方法的东西
但在我检查完后,我不知道该怎么做
怎么办?你应该参考
它应该能帮助你完成翻译。如您所见,您应该添加一个tsf全局变量(从上面参考资料的第11行开始)
另外,您可能希望使用mako检查模板的呈现,因为根据我的了解,您正在将mako占位符放置到html文件中。我建议这一资源:
注意:如果将html标记添加到msg字符串中,请在mako占位符中使用| n filter,如${n}
看看这些,如果你有进一步的问题,我很乐意帮助你,我刚刚在我的金字塔应用程序上实现了国际化,从这里,我知道原因:看起来你使用的u是不正确的,因为它是一个函数。所以它应该更像u(context.detail_panel(order)),但您收到的错误似乎是另一个问题。
Traceback (most recent call last):
...
File "/mypath/myprj/templates/_detail_panel_a.html", line 5, in render_body
<h1>${_('\u934j\u29jd\u01ld\u9dk3')}</h1>
MakoRenderingException:
Traceback (most recent call last):
...
File "/mypath/myprj/templates/_detail_panel_a.html", line 5, in render_body
<h1>${_('\u934j\u29jd\u01ld\u9dk3')}</h1>
UnboundLocalError: local variable '_' referenced before assignment
myprj/myprj/subscribers.py
def add_renderer_globals(event):
request = event['request']
event['_'] = request.translate
event['localizer'] = request.localizer
myprj/myprj/__init__.py
config.add_subscriber('myprj.subscribers.add_renderer_globals', 'pyramid.events.BeforeRender')