12.1. Localization Translation

  • i18n - Internationalization

  • l10n - Localization

12.1.1. Gettext

  • .po files

  • .mo files

  • Using gettext in code

  • Generating translation files

  • Compiling translation files

  • Updating translation files

12.1.2. Settings

Code 12.1. settings.py
MIDDLEWARE = [
    ...
    'django.middleware.locale.LocaleMiddleware',
    ...
]
Code 12.2. settings.py
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

Code 12.3. settings.py
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

  • python manage.py makemessages -l en

  • python manage.py compilemessages

$ cd myproject
$ mkdir -p locale
$ python manage.py makemessages -l en
processing locale en
$ python manage.py makemessages -l pl
processing locale pl
$ python manage.py 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

../../_images/locale-i18n-poedit11.png
../../_images/locale-i18n-poedit21.png
../../_images/locale-i18n-poedit31.png

12.1.8. Transifex

12.1.9. Further Reading

12.1.10. Assignments

# TODO: Create Tests
# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Django Locale Translation
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% English
# 0. Use `myproject`
# 1. Add locale middleware to `settings.MIDDLEWARE`
# 2. Create a directory `locale` in each app of `myproject`
# 3. Generate language files for English (en) and Polish (pl)
# 4. Translate language files using LLM (i.e., GitHub Copilot)
# 5. Compile language files
# 6. Restart server
# 7. Open your browser at `http://127.0.0.1:8000/admin`
# 8. Change preferred language in your browser: English <-> Polish

# %% Polish
# 0. Użyj `myproject.shop`
# 1. Dodaj locale middleware do `settings.MIDDLEWARE`
# 2. Stwórz katalog `locale` w każdej apce projektu `myproject`
# 3. Wygeneruj pliki językowe dla angielskiego (en) i polskiego (pl)
# 4. Przetłumacz pliki językowe wykorzystując LLM (np. GitHub Copilot)
# 5. Skompiluj pliki językowe
# 6. Zrestartuj serwer
# 7. Otwórz przeglądarkę na `http://localhost:8000/admin`
# 8. Zmień preferowany język w przeglądarce: angielski <-> polski

# %% Hints
# - `python manage.py makemessages -l en -l pl`
# - `python manage.py compilemessages`
# - `python manage.py runserver`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""

...