Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django translation

Django translation

A basic overview on how translations can be managed through Django.

Guiomar Valderrama

June 28, 2017
Tweet

Other Decks in Programming

Transcript

  1. INTRO ➤ python code ➤ trans template tag ➤ blocktrans

    template tag ➤ js ➤ context ➤ pluralization ➤ urls ➤ translation files ➤ recommended practices
  2. 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
  3. 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 %}
  4. 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 "
  5. 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
  6. 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')
  7. 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)
  8. 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' )
  9. 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
  10. 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