15.4. Manage Translation

  • gettext

  • from django.utils.translation import gettext_lazy as _

  • _('string')

  • {% blocktranslate %} and {% endblocktranslate %}

  • {% translate "string" %}

  • django-admin makemessages -l pl

  • django-admin compilemessages

  • LANGUAGE_CODE = 'en-us'

  • USE_I18N = True

  • django.middleware.locale.LocaleMiddleware

  • .po and .mo files

  • locale directory

15.4.1. Timezone

  • django.utils.timezone

  • from django.conf.locale.en import formats as en_formats

>>> en_formats.DATETIME_FORMAT = 'Y-m-d H:i'    
>>> en_formats.DATE_FORMAT = 'Y-m-d'            
>>> en_formats.TIME_FORMAT = 'H:i'              
>>>
>>> USE_TZ = True
>>> TIME_ZONE = 'UTC'

15.4.2. Gettext

  • .po files

  • .mo files

  • Using gettext in code

  • Generating translation files

  • Compiling translation files

  • Updating translation files

  • Transifex (tx push and tx pull)

15.4.3. i18n - internationalization

  • https://docs.djangoproject.com/en/stable/topics/i18n/

  • from django.utils.translation import gettext_lazy as _

  • {% blocktranslate %} and {% endblocktranslate %}

  • {% translate %}

  • django-admin makemessages -l en

  • django-admin compilemessages

  • transifex-client

  • gettext

  • poedit

Append to myproject/settings.py:

>>> LANGUAGE_CODE = 'en-us'
>>> USE_I18N = True
>>> USE_L10N = True
$ cd myproject/shop
$ mkdir locale
$ django-admin makemessages -l en
processing locale en
$ django-admin makemessages -l pl
processing locale pl
$ django-admin compilemessages
processing file django.po in /src/myproject/shop/locale/en/LC_MESSAGES
processing file django.po in /src/myproject/shop/locale/pl/LC_MESSAGES