简体   繁体   English

Django模板:在其中翻译带有HTML的文本块的最佳实践

[英]Django templates: Best practice for translating text block with HTML in it

In Django templates, how would I translate a block that contains HTML? 在Django模板中,我如何翻译包含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: 拆分翻译的字符串允许我随时更改模板中的HTML,但我想将它放入单个翻译字符串会更有意义,如下所示:

{% 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, ie if I wanted to change the HTML (eg CSS class for the anchor), I'd have to retranslate the string for each language. 但是HTML标记在翻译字符串中,即如果我想更改HTML(例如锚的CSS类),我必须重新翻译每种语言的字符串。

Are there any better alternatives? 还有更好的选择吗?

From the docs : 来自文档

It's not possible to mix a template variable inside a string within {% trans %}. 在{%trans%}内的字符串中混合模板变量是不可能的。 If your translations require strings with variables (placeholders), use {% blocktrans %} instead. 如果您的翻译需要带变量的字符串(占位符),请改用{%blocktrans%}。

Then under blocktrans : 然后在blocktrans下:

To translate a template expression -- say, accessing object attributes or using template filters -- you need to bind the expression to a local variable for use within the translation block. 要转换模板表达式(例如,访问对象属性或使用模板过滤器),您需要将表达式绑定到本地变量以在转换块中使用。 Examples: 例子:

{% blocktrans with article.price as amount %}
That will cost $ {{ amount }}.
{% endblocktrans %}

{% blocktrans with value|filter as myvar %}
This will have {{ myvar }} inside.
{% endblocktrans %}

This way your translated strings have the placeholders. 这样您的翻译字符串就有占位符。 In your case: 在你的情况下:

{% blocktrans with login_object.anchor as anchor %}
    Please {{ anchor|safe }}log in</a> in order to use MyApplicationName.
{% endblocktrans %}

You will need to generate the text that goes in anchor in your view function. 您需要在视图函数中生成anchor中的文本。 This keeps it out of your translated string. 这使它远离你翻译的字符串。

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: Mike DeSimone提出了正确的建议,我只想做一个调整:

{% blocktrans with login_object.anchor_attr as anchor_attr %}
    Please <a {{ anchor_attr|safe }}>log in</a> in order to use MyApplicationName.
{% endblocktrans %}

This keeps the HTML in the translation string balanced. 这使得翻译字符串中的HTML保持平衡。 Without the opening tag in the string, it could easily look like an error in the string. 如果没有字符串中的开始标记,它很容易看起来像字符串中的错误。

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 : 在这种情况下,您可以避免在使用自定义模板标记时使用.po文件中的任何HTML或CSS, 例如下一个

@register.filter( name='safewrap' )
def safewrap( val, arg ):
    return val.format( arg )
safewrap.is_safe = True

...

{% blocktrans with sum2="<a href='mysite.com/offer'>{0}</a>"|safewrap:sum %}
    It costs {{sum2}} dollars.
{% endblocktrans %}

So, in your .po file you have: 所以,在您的.po文件中,您有:

It costs %(sum2)s dollars.

But it's a difficult question - what to do with partial fragments which require translation (like in your case). 但这是一个棘手的问题 - 如何处理需要翻译的部分片段(如你的情况)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM