当用户点击按钮时,我想要翻译一段文字。我运行了以下代码:
mkdir project/locale
django-admin makemessages -l fr
django-admin compilemessages
在我的HTML中,我有以下内容:
<h5 class="slim">{% trans "This is an example" %}</h5>
只有在我的html中执行此操作时,我的翻译才有效:
{% language 'fr' %}
<h5 class="slim">{% trans "This is an example" %}</h5>
{% endlanguage %}
我尝试将settings.py中的语言更改为&#39; fr&#39;来自&#39; en&#39;并摆脱{%language&#39; fr&#39; %}和{%endlanguage%},但是当我这样做时翻译不起作用。
以下是我的settings.py中的内容:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware', # ADDED TO FILE FOR TRANSLATION
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
LANGUAGE_CODE = 'en'
LANGUAGES = (
('en', _('English')),
('fr', _('French')),
('de', _('German')),
)
以下是我的.po档案中的内容:
msgid "This is an example"
msgstr "Ceci est une example"