1

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?

ArtyIF
  • 21
  • 3

2 Answers2

1

Turns out I used the wrong path. It should be conf/locale and not just locale.

ArtyIF
  • 21
  • 3
0

The most basic option to switch language in to use this in view:

from django.utils import translation

translation.activate(language) #eg. translation.activate('en')

You can also use this solution for changing language in template: https://stackoverflow.com/a/18393243/4992056

wojteck
  • 71
  • 6
  • I tried using activate, and the result is the same. – ArtyIF Apr 24 '20 at 18:55
  • try using makemessages and compilemessages without any flags: python manage.py makemessages python manage.py compilemessages – wojteck Apr 24 '20 at 19:22
  • I really can't, not adding ignore flags results in permission errors and `Unknown encoding "utf8". Proceeding with ASCII instead.` error, but I did remove the `-l en` and it still used Russian. – ArtyIF Apr 24 '20 at 19:31