Django 模板:翻译包含 HTML 的文本块的最佳实践

发布于 2024-10-11 10:07:10 字数 622 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙女山的月亮 2024-10-18 10:07:10

来自文档

不可能在 {% trans %} 内的字符串中混合模板变量。如果您的翻译需要带有变量(占位符)的字符串,请改用 {% blocktrans %}。

然后在blocktrans下:

要翻译模板表达式(例如,访问对象属性或使用模板过滤器),您需要将表达式绑定到局部变量以在翻译块中使用。示例:

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

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

这样您翻译的字符串就有占位符。在您的情况下:

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

您将需要生成视图函数中的 anchor 中的文本。这会将其排除在翻译的字符串之外。

From the docs:

It's not possible to mix a template variable inside a string within {% trans %}. If your translations require strings with variables (placeholders), use {% blocktrans %} instead.

Then under 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. This keeps it out of your translated string.

七秒鱼° 2024-10-18 10:07:10

将整个句子放在一个翻译字符串中不仅更有意义,而且当句子被分成几部分时,译者可能无法正确地获得句子。请记住,句子的不同部分可能会相互影响,包括时态、格、性别等。更不用说其他语言的行为与英语不同。例如,“请”一词在提出请求和提出要求时可能是不同的。

始终在翻译字符串中使用完整的句子,以便译者可以用目标语言造出正确的句子。

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 %}

这可以保持翻译字符串中的 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:

{% 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. Without the opening tag in the string, it could easily look like an error in the string.

零度℉ 2024-10-18 10:07:10

我可以为仅部分片段提供方便的解决方案,这些片段对于任何翻译都是恒定的。

在这种情况下,在使用自定义模板标记例如下一个时,您可以避免在 .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 %}

因此,在您的 .po 文件中您有:

It costs %(sum2)s dollars.

但这是一个困难的问题- 如何处理需要翻译的部分片段(如您的情况)。

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:

@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:

It costs %(sum2)s dollars.

But it's a difficult question - what to do with partial fragments which require translation (like in your case).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文