12.1. Localization Translation¶
i18n - Internationalization
l10n - Localization
12.1.1. Gettext¶
.po
files.mo
filesUsing
gettext
in codeGenerating translation files
Compiling translation files
Updating translation files
12.1.2. Settings¶
MIDDLEWARE = [
...
'django.middleware.locale.LocaleMiddleware',
...
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
12.1.3. Change¶
from django.conf.locale.en import formats as en_formats
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'
TIME_INPUT_FORMATS = ['%H:%M', '%H:%M:%S']
12.1.4. Manage¶
django-admin makemessages -l en
django-admin compilemessages
$ cd myproject
$ 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 /tmp/myproject/locale/en/LC_MESSAGES
processing file django.po in /tmp/myproject/locale/pl/LC_MESSAGES
12.1.5. Templates¶
{% load i18n %}
{% translate %}
{% blocktranslate %}
and{% endblocktranslate %}
12.1.6. Files¶
from django.utils.translation import gettext_lazy as _
``_('My string to translate')
12.1.7. Poedit¶
12.1.8. Transifex¶
python -m pip install transifex-client
tx push
tx pull
12.1.9. Further Reading¶
https://github.com/django/django/blob/master/django/utils/formats.py
https://github.com/django/django/blob/master/django/conf/locale/pl/formats.py
https://github.com/django/django/blob/master/django/conf/locale/en/formats.py
https://github.com/django/django/blob/main/django/conf/locale/hi/formats.py