2016-02-05 45 views
2

目前我使用Flask和Jinja2與Babel結合使用。到目前爲止,一切都很好。在Jinja2中使用babel的鏈接

我面臨的唯一問題是在翻譯文本中使用鏈接時。假設我們在HTML中有以下內容:

<p>You can change this in your <a href="{{ url_for("settings") }}">settings</a>.</p> 

我該如何將它與babel結合使用?

我一直在考慮下面的代碼,但是當翻譯語言中的單詞順序不一致時,會出現問題。

<p>{{ _("You can change this in your ")<a href="{{ url_for("settings") }}">{{ _("settings")</a>.</p> 

另一個想法是將整個HTML放入要翻譯的字符串中,但不能再使用字符串轉義。

這樣做的首選方法是什麼?

+0

只是一個想法:如果你能提供一些降價的文字是什麼? –

+0

[Jinja2 translation of links]可能重複(http://stackoverflow.com/questions/9835727/jinja2-translation-of-links) –

回答

1

我正在尋找解決方案,並遇到了這個優秀的page

您可以使用可用於jinja2變量的字符串替換來添加鏈接。這裏有一個例子:那麼

{{ _("You can change this in your %(open)ssettings%(close)s.", open='<a href="/user/%d">' % user_id, close='</a>')|safe }} 

你的.po文件都會有一個這樣的條目:

msgid "You can change this in your %(open)ssettings%(close)s." 
msgstr ""