[英]Localization in Pyramid ZPT Chameleon Template
I try to get string translations working in Pyramid with ZPT templates. 我尝试使用ZPT模板在金字塔中使用字符串翻译。 I followed the Pyramid guide on internationalization and localization, ie http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/i18n.html#chameleon-template-support-for-translation-strings .
我遵循了有关国际化和本地化的金字塔指南,即http://docs.pylonsproject.org/projects/pyramid/zh-CN/latest/narr/i18n.html#chameleon-template-support-for-translation-strings 。
However, when I add the line 但是,当我添加行时
<span>${some_translation_string}</span>
to my .pt template file I just get an assertion message from waitress: 到我的.pt模板文件中,我只是从女服务员那里收到一条断言消息:
NameError: some_translation_string
When I translate the string some_translation_string
outside the ZPT templates (ie in the Python code of the view) it translates correctly. 当我在ZPT模板之外(即,在视图的Python代码中)翻译字符串
some_translation_string
,它可以正确翻译。 Thus, I think to have a valid compiled message catalog in place (though created manually due to missing Python3 support of babel/lingua). 因此,我认为有一个有效的已编译消息目录(尽管由于缺少babel / lingua的Python3支持而手动创建)。
I guess I misunderstand the way to insert localized strings in ZPT templates in general. 我想我通常会误解在ZPT模板中插入本地化字符串的方式。 It probably cannot be the same as for referencing variables?
它可能与引用变量不同吗?
As I understand now, translations within the ZPT template should look like this: 据我了解,ZPT模板中的翻译应如下所示:
<span i18n:translate="">some_translation_string</h1>
If you omit the string identifier in i18n:translate
the string itself is used for this. 如果您在
i18n:translate
省略了字符串标识符,则将字符串本身用于此目的。
Also you must add add the domain name in the template header, eg: 另外,您还必须在模板标题中添加域名,例如:
<html lang="en"
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="my_domain">
This information seems to be missing in the referenced Pyramid documentation. 引用的金字塔文档中似乎缺少此信息。
For reference, to do translations for messages in your JavaScript code, I found this way handy: 作为参考,为了对JavaScript代码中的消息进行翻译,我发现这种方式很方便:
In your view, add a TranslationStringFactory
instance to the template parameters: 在您的视图中,将
TranslationStringFactory
实例添加到模板参数:
from translationstring import TranslationStringFactory
@view_config(route_name='site', renderer='templates/site.pt')
def form(request):
...
return {'_': TranslationStringFactory('yourapp')}
Then in the template you can write: 然后,您可以在模板中编写:
<script type="text/javascript">
...
yourapp.i18n['error'] = '${_('Error')}';
yourapp.i18n['warning'] = '${_('Warning')}';
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.