In a Django template, I need to transanlate some strings to a specific language (different from current one).
I would need something like this:
{% tans_to "de" "my string to translate" %}
or
{% blocktrans_to "de" %}my bloc to translate {% endblocktrans_to %}
to force translation to German.
I know I can call the following code in a view:
gettext.translation('django', 'locale', ['de'], fallback=True).ugettext("my string to translate")
Do I need to create a specific template tag ? Or does it already exist a dedicated tag in Django ?
Use the function django. utils. translation. gettext_noop() to mark a string as a translation string without translating it.
{% extends variable %} uses the value of variable . If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.
Create a language switcher template Firstly, add a {% load i18n %} line at the top to load Django i18n-related tags. Open up an HTML form with its action set to send a POST request to set_language view. Note: As mentioned in the Django documentation, this view specifically expects a POST request to be sent.
This header is sent by your browser and tells the server which language(s) you prefer, in order by priority. Django tries each language in the header until it finds one with available translations. Failing that, it uses the global LANGUAGE_CODE setting.
From Django version 1.6
, there is a language
template tag so you can simply pass the desired language code:
{% language "de" %}my block to translate{% endlanguage %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With