Django 模板:翻译包含 HTML 的文本块的最佳实践
在 Django 模板中,如何翻译包含 HTML 的块?例如:
{% trans "Please" %}
<a href="{% url login %}?next={{ currentUrlPath }}">
{% trans "log in" %}
</a>
{% trans "in order to use MyApplicationName." %}
拆分翻译的字符串允许我随时更改模板中的 HTML,但我想将其放入单个翻译字符串中会更有意义,如下所示:
{% url login as loginUrl %}
{% blocktrans %}
Please
<a href="{{ loginUrl }}?next={{ currentUrlPath }}">
log in
</a>
in order to use MyApplicationName.
{% endblocktrans %}
但是 HTML 标记位于翻译字符串中,即如果我想更改 HTML(例如锚点的 CSS 类),我必须重新翻译每种语言的字符串。
还有更好的选择吗?
In Django templates, how would I translate a block that contains HTML? For example:
{% trans "Please" %}
<a href="{% url login %}?next={{ currentUrlPath }}">
{% trans "log in" %}
</a>
{% trans "in order to use MyApplicationName." %}
Splitting up translated strings allows me to change the HTML in the template at any time, but I guess it would make more sense to put it into a single translation string, like so:
{% url login as loginUrl %}
{% blocktrans %}
Please
<a href="{{ loginUrl }}?next={{ currentUrlPath }}">
log in
</a>
in order to use MyApplicationName.
{% endblocktrans %}
But then the HTML markup is in the translation string, i.e. if I wanted to change the HTML (e.g. CSS class for the anchor), I'd have to retranslate the string for each language.
Are there any better alternatives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自文档:
然后在
blocktrans
下:这样您翻译的字符串就有占位符。在您的情况下:
您将需要生成视图函数中的
anchor
中的文本。这会将其排除在翻译的字符串之外。From the docs:
Then under
blocktrans
:This way your translated strings have the placeholders. In your case:
You will need to generate the text that goes in
anchor
in your view function. This keeps it out of your translated string.将整个句子放在一个翻译字符串中不仅更有意义,而且当句子被分成几部分时,译者可能无法正确地获得句子。请记住,句子的不同部分可能会相互影响,包括时态、格、性别等。更不用说其他语言的行为与英语不同。例如,“请”一词在提出请求和提出要求时可能是不同的。
始终在翻译字符串中使用完整的句子,以便译者可以用目标语言造出正确的句子。
Mike DeSimone 提出了正确的建议,我只会对其进行一项调整:
这可以保持翻译字符串中的 HTML 平衡。如果字符串中没有开始标记,它很容易看起来像是字符串中的错误。
Not only does it make more sense to put the entire sentence in one translation string, it may be impossible for translators to get the sentence correct when it's split into pieces. Remember that the different parts of the sentence can affect each other, with tenses, cases, gender, and so on. Not to mention that other languages behave differently than English. The word "please" for example could be different when making a request and making a demand, for example.
Always use complete sentences in your translation strings so that the translators can make a correct sentence in the target language.
Mike DeSimone makes the right recommendation, I would make just one tweak to it:
This keeps the HTML in the translation string balanced. Without the opening tag in the string, it could easily look like an error in the string.
我可以为仅部分片段提供方便的解决方案,这些片段对于任何翻译都是恒定的。
在这种情况下,在使用自定义模板标记例如下一个时,您可以避免在 .po 文件中使用任何 HTML 或 CSS:
因此,在您的 .po 文件中您有:
但这是一个困难的问题- 如何处理需要翻译的部分片段(如您的情况)。
I can offer a convenient solution for only partial fragments which are constant for any translation.
In this case you can avoid to use any HTML or CSS inside of your .po file when using custom template tag such as the next:
So, in your .po file you have:
But it's a difficult question - what to do with partial fragments which require translation (like in your case).