Django I18n - 在 Python 代码中翻译的问题
Django I18n - issues with translating in Python code
我决定在我的俄语网站上添加英文翻译。我决定从翻译 Python 代码中的字符串开始。这就是我尝试这样做的方式:
(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, _("Тема изменена успешно!"))
那么,我运行:
python3 manage.py makemessages --ignore="venv" --ignore="collectedstatic" -l en
这会在 conf/locale/en/LC_MESSAGES
中创建名为 django.po
的文件。当我打开它时,它包含以下内容:
# 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 ""
我将最后一个 msgstr 行更改为:
msgstr "Theme changed successfully!"
然后,我更改了我的 Docker 入口点(我必须使用 Docker 因为这是我学校的要求。是的,我正在为我的学校项目做英语本地化,它几乎准备好了,我还有一个月的时间到截止日期)通过添加以下行:
python manage.py compilemessages -l en
之后,我通过 docker-compose:
启动 Docker 个容器
docker-compose up
然后,我检查是否创建了 django.mo
文件(它是),打开网站,触发主题更改,但是 the message is in Russian and not English.
如何使此消息使用英文翻译?
切换语言以在视图中使用此选项的最基本选项:
from django.utils import translation
translation.activate(language) #eg. translation.activate('en')
您也可以使用此解决方案更改模板中的语言:
原来我用错了路径。它应该是 conf/locale
而不仅仅是 locale
.
我决定在我的俄语网站上添加英文翻译。我决定从翻译 Python 代码中的字符串开始。这就是我尝试这样做的方式:
(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, _("Тема изменена успешно!"))
那么,我运行:
python3 manage.py makemessages --ignore="venv" --ignore="collectedstatic" -l en
这会在 conf/locale/en/LC_MESSAGES
中创建名为 django.po
的文件。当我打开它时,它包含以下内容:
# 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 ""
我将最后一个 msgstr 行更改为:
msgstr "Theme changed successfully!"
然后,我更改了我的 Docker 入口点(我必须使用 Docker 因为这是我学校的要求。是的,我正在为我的学校项目做英语本地化,它几乎准备好了,我还有一个月的时间到截止日期)通过添加以下行:
python manage.py compilemessages -l en
之后,我通过 docker-compose:
启动 Docker 个容器docker-compose up
然后,我检查是否创建了 django.mo
文件(它是),打开网站,触发主题更改,但是 the message is in Russian and not English.
如何使此消息使用英文翻译?
切换语言以在视图中使用此选项的最基本选项:
from django.utils import translation
translation.activate(language) #eg. translation.activate('en')
您也可以使用此解决方案更改模板中的语言:
原来我用错了路径。它应该是 conf/locale
而不仅仅是 locale
.