簡體   English   中英

金字塔ZPT變色龍模板中的本地化

[英]Localization in Pyramid ZPT Chameleon Template

我嘗試使用ZPT模板在金字塔中使用字符串翻譯。 我遵循了有關國際化和本地化的金字塔指南,即http://docs.pylonsproject.org/projects/pyramid/zh-CN/latest/narr/i18n.html#chameleon-template-support-for-translation-strings

但是,當我添加行時

<span>${some_translation_string}</span>

到我的.pt模板文件中,我只是從女服務員那里收到一條斷言消息:

NameError: some_translation_string

當我在ZPT模板之外(即,在視圖的Python代碼中)翻譯字符串some_translation_string ,它可以正確翻譯。 因此,我認為有一個有效的已編譯消息目錄(盡管由於缺少babel / lingua的Python3支持而手動創建)。

我想我通常會誤解在ZPT模板中插入本地化字符串的方式。 它可能與引用變量不同嗎?

據我了解,ZPT模板中的翻譯應如下所示:

<span i18n:translate="">some_translation_string</h1>

如果您在i18n:translate省略了字符串標識符,則將字符串本身用於此目的。

另外,您還必須在模板標題中添加域名,例如:

<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">

引用的金字塔文檔中似乎缺少此信息。

作為參考,為了對JavaScript代碼中的消息進行翻譯,我發現這種方式很方便:

在您的視圖中,將TranslationStringFactory實例添加到模板參數:

from translationstring import TranslationStringFactory

@view_config(route_name='site', renderer='templates/site.pt')
def form(request):
    ...
    return {'_': TranslationStringFactory('yourapp')}

然后,您可以在模板中編寫:

<script type="text/javascript">
...
yourapp.i18n['error'] = '${_('Error')}';
yourapp.i18n['warning'] = '${_('Warning')}';

</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM