我在我的项目中使用多重翻译
为此,我已将设置文件更新为
LANGUAGE_CODE = 'en-us'
gettext = lambda s: s
LANGUAGES = (
('es', gettext('Spanish')),
('en', gettext('English')),
)
LOCALE_PATHS = (
'/mnt/aviesta/pythondev/django/locale',
)
USE_I18N = True
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.request",
)
我的模板文件为:
{% load i18n %}
{% trans "Hello" %}
<p>Already a user <a href="/login/"><b>{% trans "login here" %}</b></a></p>
之后我创建一个与我的应用程序并行的语言环境文件夹,然后在其中创建特定的语言文件夹: django-admin.py makemessages -l es ,它会创建.po文件,然后更新这个.po文件为:
#: customer_reg/customer_register.html:14
msgid "Hello"
msgstr "¡Hola"
#: customer_reg/customer_register.html:17
msgid "login here"
msgstr "ingresa aquí"
最后我编译我的消息 django-admin.py compilemessages
把我的字符串“你好”和“在这里登录”保留在英文中,它们没有被翻译。我不知道为什么会这样?
答案 0 :(得分:1)
您的代码看起来很棒。我唯一没见过的是LOCALE_PATHS
settings.py
Mabye这个答案,我前段时间做过,可以帮到你:Issues with multiple languages
编辑//回答评论
from django.utils import translation
translation.activate('es')