我正在尝试将django项目从英语翻译成德语。目前,它包含两个应用程序。 mainapp已正确翻译。另一个应用程序已在django.po文件中进行了完全翻译,但只会显示一些翻译后的字符串,而其他仍然保留为英语。
要编译语言文件(当前两个应用程序各一个),我总是在项目的根目录中执行以下命令:
django-admin makemessages -l de
# Now doing some translations
django-admin compilemessages
当我更改任何翻译字符串并执行makemessage和compilemessage时,django仍将使用旧翻译而不是新翻译。因此,看起来好像是在前一段时间工作的,但是现在在编译/使用翻译时出现了问题。
例如,在此模板中,“登录”和“注册”字符串已正确翻译,但“是否需要帐户?”之所以不是string,是因为在翻译不再起作用时添加了它。但不幸的是,我不知道发生了什么变化以及为什么它不再起作用。
{% extends "mainapp/base.html" %}
{% load crispy_forms_tags %}
{% load i18n %}
{% block content %}
<div class="content-section">
<form method="POST"> {% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">{% trans "Log in" %}</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">{% trans "Log in" %}</button>
</div>
</form>
<div class="border-top pt-3">
<small class="text-muted">{% trans "Need an account?" %}<a class="ml-2" href="{% url 'register' %}">{% trans "Sign up" %}</a></small>
</div>
</div>
{% endblock content%}
这些是我的设置:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
LANGUAGE_CODE = 'en'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = (os.path.join(BASE_DIR, 'locale'),)
我也研究了该主题及其所有参考主题: Django: Only a single element gets translated
非常感谢您的帮助!
顺便说一下,这是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.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-09-20 09:17+0200\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"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: .\login\templates\login\login.html:9 .\login\templates\login\login.html:13
#: .\login\templates\login\logout.html:7
msgid "Log in"
msgstr "Anmelden"
#: .\login\templates\login\login.html:17
msgid "Need an account?"
msgstr "Noch kein Account?"
#: .\login\templates\login\login.html:17
#: .\login\templates\login\register.html:13
msgid "Sign up"
msgstr "Registrieren"
#: .\login\templates\login\logout.html:5
msgid "You have been logged out!"
msgstr "Sie wurden abgemeldet"
#: .\login\templates\login\logout.html:7
msgid "Home"
msgstr "Start"
#: .\login\templates\login\profile.html:16
msgid "Profile Info"
msgstr "Profil Info"
#: .\login\templates\login\profile.html:21
msgid "Update"
msgstr "Aktualisieren"
#: .\login\templates\login\register.html:9
msgid "Join today"
msgstr "Heute beitreten"
#: .\login\templates\login\register.html:17
msgid "Already have an account?"
msgstr "Sie haben bereits einen Account?"
#: .\login\templates\login\register.html:17
msgid "Sign in"
msgstr "Anmelden"
答案 0 :(得分:0)
几乎可以肯定问题出在django.po
的{{1}}文件中
搜索未翻译的字符串,然后查看其上方的文本。我记不清它到底是什么样子,但是如果它周围有些模糊,那就是问题所在。只需删除它,然后local/de/LC_MESSAGES/django.po
,它就可以翻译