Reputation: 3695
I am using django 1.7 & python 2.7.
Is it possible to concatenate django translation strings?
For example, I have the following translation string:
{% trans "institution< br />country / region< br />location< br />mm/yyyy - mm/yyyy (X years, X months)< br />< br />" as overseas_experience_suggestion_09 %}
Is it possible to break the above long translation string into many individual strings, then concatenate the strings and still display the concatenated string as a overseas_experience_suggestion_09
template variable?
Here is essentially what I am asking.
The following 5 individual translation strings are somehow concatenated as overseas_experience_suggestion_09
?
{% trans "institution< br />" %}
{% trans "country / region< br />" %}
{% trans "location< br />" %}
{% trans "mm/yyyy - mm/yyyy" %}
{% trans "(X years, X months)< br />< br />" %}
`as overseas_experience_suggestion_09`
I have looked on the django docs, searched google and SO, but da nada. There is a reference to concatenating translation string in python, but I do not think I can use this in the django template.
I am hoping there is some kind of work around that would help me out.
Upvotes: 0
Views: 903
Reputation: 27861
You can combine multiple strings/variables in Django templates:
{% with foo='foo'|add:'bar' %}
{{ foo }}
{% endwith %}
You can technically do something similar with translations:
{% trans 'foo'|add:'bar'|add:'things' %}
however please dont try to do that.
Reason is because each translation string has to be present for your local (e.g. in your message file). If you simply combine them however do not include complete string for your local, Django will not be able to translate it. Therefore I would recommend to leave separate strings separately:
{% trans 'foo' %}{% trans 'bar' %}
To make life easier, you can always include that in a separate template file which you can include in other templates hence follow DRY:
{# my-translation.html #}
{% trans 'foo' %}{% trans 'bar' %}
{# foo.html #}
{% include 'my-translation.html' %}
More in i18n docs https://docs.djangoproject.com/en/1.9/topics/i18n/ and template filter docs https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#std:templatefilter-add
Upvotes: 2