Slide 1

Slide 1 text

DJANGO TRANSLATING In and out of templates

Slide 2

Slide 2 text

INTRO ➤ python code ➤ trans template tag ➤ blocktrans template tag ➤ js ➤ context ➤ pluralization ➤ urls ➤ translation files ➤ recommended practices

Slide 3

Slide 3 text

In python code OUT OF TEMPLATES

Slide 4

Slide 4 text

PYTHON CODE from django.utils.translation import trans_function as _ ➤ _('string 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

Slide 5

Slide 5 text

HTML IN TEMPLATES

Slide 6

Slide 6 text

TRANS TAG {% trans 'string to translate' %} ➤ Text 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 %}

Slide 7

Slide 7 text

BLOCKTRANS TAG {% blocktrans %}string to translate{% endblocktrans %} ➤ 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 "

Slide 8

Slide 8 text

JS IN TEMPLATES

Slide 9

Slide 9 text

JAVASCRIPT CATALOG ➤ Allows for translation directly in JS ➤ 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

Slide 10

Slide 10 text

MORE

Slide 11

Slide 11 text

TRANSLATION CONTEXT ➤ Provide a context to ease translation ➤ 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')

Slide 12

Slide 12 text

PLURALIZATION ➤ Different translations depending on a count variable ➤ 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)

Slide 13

Slide 13 text

INTERNATIONALIZATION IN URLS ➤ Use a language_code prefix from django.conf.urls.i18n import i18n_patterns urlpatterns += i18n_patterns(url(r'^', include('app_name.urls')),) ➤ Translate the url pattern url( pgettext_lazy('url context', r'^translatable_url_pattern/$'), MyView.as_view(), name='url_name' )

Slide 14

Slide 14 text

TRANSLATION FILES ➤ Create / update message file (.po): ➤ ./manage.py makemessages -d djangojs -l language_code ➤ ./manage.py makemessages -l language_code ➤ Compile messages (.mo): ➤ ./manage.py compilemessages ➤ Translation strings are not shared between .po files

Slide 15

Slide 15 text

RECOMMENDED PRACTICES ➤ Use short, full sentences ➤ Do not 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

Slide 16

Slide 16 text

DJANGO TRANSLATING END