13.4. Manage Translation

  • python manage.py makemessages -l en

  • python manage.py compilemessages

  • locale directory

  • .po and .mo files

13.4.1. Gettext

  • .po files

  • .mo files

  • Using gettext in code

  • Generating translation files

  • Compiling translation files

  • Updating translation files

  • gettext

  • from django.utils.translation import gettext_lazy as _

  • _('string')

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

  • {% translate "string" %}

  • LANGUAGE_CODE = 'en-us'

  • USE_I18N = True

  • django.middleware.locale.LocaleMiddleware

13.4.2. i18n - internationalization

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

  • from django.utils.translation import gettext_lazy as _

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

  • {% translate %}

  • python manage.py makemessages -l en

  • python manage.py compilemessages

  • transifex-client

  • gettext

  • poedit

  • ChatGPT

  • Github Copilot

Append to myproject/settings.py:

>>> LANGUAGE_CODE = 'en-us'
>>> USE_I18N = True
>>> USE_L10N = True

13.4.3. Locale Directory

$ mkdir -p myproject/myapp/locale

13.4.4. Make Messages

$ python manage.py makemessages -l en
processing locale en
$ python manage.py makemessages -l pl
processing locale pl

13.4.5. Compile Messages

$ python manage.py compilemessages
processing file django.po in myproject/myapp/locale/en/LC_MESSAGES
processing file django.po in myproject/myapp/locale/pl/LC_MESSAGES