I decided to add an English translation to my Russian language website. I decided to start with translating strings in my Python code. This is how I tried to do it:
(project name)/settings.py
:
MIDDLEWARE = [
# ...
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
# ...
]
# ...
LANGUAGE_CODE = 'en'
TIME_ZONE = 'Europe/Moscow'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = [ os.path.join(BASE_DIR, 'locale'), ]
LANGUAGES = [ ('en', 'English'), ('ru', 'Русский'), ]
(app name)/views.py:
from django.contrib import messages
from django.utils.translation import gettext as _
# ...
# in a function based view
messages.success(request, _("Тема изменена успешно!"))
Then, I run:
python3 manage.py makemessages --ignore="venv" --ignore="collectedstatic" -l en
This creates file in conf/locale/en/LC_MESSAGES
called django.po
. When I open it, it contains this:
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-04-24 21:08+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: front/views.py:29
msgid "Тема изменена успешно!"
msgstr ""
I change the last msgstr line to:
msgstr "Theme changed successfully!"
Then, I make changes to my Docker entrypoint (I have to use Docker because it's my school's requirement. Yes, I'm doing an English localization for my school project, it's almost ready and I have one more month until the deadline) by adding this line:
python manage.py compilemessages -l en
After that, I start Docker containers through docker-compose:
docker-compose up
Then, I check if a django.mo
file was created (it was), open the site, trigger the theme change, but the message is in Russian and not English.
How do I make this message use the English translation?
Turns out I used the wrong path. It should be conf/locale
and not just locale
.