to translate') ➤ limited to: gettext gettext_lazy ugettext ugettext_lazy ➤ can only be used for one function per file ➤ ugettext returns unicode vs UTF-8 bytestring ➤ lazy translation translates when accessed vs when called ➤ get_language() get the current language ➤ activate('language_code') / deactivate() activate translation for a language / deactivate the current language in favour of the default
between quotation marks marked for translation ➤ Cannot use variables ➤ Any filter applied to the text (upper, title…) occurs later ➤ as variable save the translation as a variable {% trans 'string to translate' as translated_string %}
Text within block marked for translation ➤ HTML is rendered, but marked for translation ➤ Cannot use variable attributes or filters within the block ➤ with variable=value assign values to variables within the block ➤ asvar save the translation as a variable ➤ trimmed remove newlines and white spaces {% blocktrans trimmed %} string to translate {% endblocktrans %} untrimmed: "\n string to translate\n " trimmed: "string to translate "
Works for static files ➤ Uses functions with same name as python translation gettext('string to translate') ➤ Generates an additional translation file (djangojs) for each language
Different translation for each context ➤ With trans tag {% trans 'May' context 'month name' %} ➤ With block trans tag {% blocktrans context 'month name' %}May{% endblocktrans %} ➤ With python / JS Catalog pgettext('month name', 'May')
With blocktrans tag {% blocktrans count number=houses|length %} There is {{ number }} house. {% plural %} There are {{ number }} houses. {% endblocktrans %} ➤ With python / JS Catalog number = 4 ungettext('There is {number} house', 'There are {number} houses', number).format(number=number)
break sentences to include variables ➤ Use variables when posible ➤ Set case in translation string ➤ Favour _('').format() over _('%s') % ➤ with tag use the newer notation (former) variable=value variable as value ➤ Model fields ➤ translate verbose_name ➤ translate help_text