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

后端 未结 3 551
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 02:54

In Django templates, how would I translate a block that contains HTML? For example:

{% trans \"Please\" %}
    

        
3条回答
  •  我在风中等你
    2020-12-29 03:12

    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 log in 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.

提交回复
热议问题