Search code examples
djangodjango-templateslocalizationinternationalization

Django Localization: How do we properly translate a string if there are links embedded?


How do we properly add translation functionality to strings without breaking them into smaller parts? I would like to make it easier for translators to be able to translate strings.

For example, please consider the following:

<p>By clicking on "register", you agree to our <a href="{% url 'terms' %}">terms of service</a>, <a href="{% url 'privacy' %}">privacy policy</a> and <a href="{% url 'forum_guidelines' %}">forum guidelines</a></p>

If we use trans tags {% trans "" %}, this would have to be broken up into 6 individual parts, which would make it very difficult to localize into other languages:

1) {% trans "By clicking on "register", you agree to our" %}
2) {% trans "terms of service" %} 
3) {% trans "," %} 
4) {% trans "privacy policy" %}
5) {% trans "and" %}
6) {% trans "forum guidelines" %}

I've read the documentation, but I still do not understand how to approach this issue. I am hoping you guys know how to do this or have a properly solution for this type of scenario.


Solution

  • Use blocktrans:

    {% url 'terms' as terms_url %}
    {% url 'privacy_url' as privacy_url %}
    {% url 'forum_guidelines' as forum_guidelines_url %}
    
    <p>
        {% blocktrans trimmed %}
            By clicking on "register", you agree to our 
            <a href="{{ terms_url }}">terms of service</a>, 
            <a href="{{ privacy_url }}">privacy policy</a> and 
            <a href="{{ forum_guidelines_url }}">forum guidelines</a>
        {% endblocktrans %}
    </p>