我很难问这个,因为它被问了很多次:
django - how to make translation work?
django internationalization and translations issue
How to setup up Django translation in the correct way?
http://askbot.org/en/question/8948/weve-edited-djangopo-files-but-translations-do-not-work-why/
我想要英语(默认)和斯洛文尼亚语。我的设置是这样的:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
)
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Belgrade'
USE_I18N = True
USE_L10N = True
USE_TZ = True
from django.utils.translation import ugettext_lazy as _
LANGUAGES = (
('si', _('Slovenian')),
('en-us', _('English')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
Urls.py:
urlpatterns = i18n_patterns('',
url(r'^', include('analytics.urls')),
url(r'^login', RedirectView.as_view(url='/admin/login', permanent=False)),
url(r'^admin/', include(admin.site.urls)),
)
模板:
<div class="time_range">{% trans "Time range:" %}</div>
我将消息编译成.po文件,现在根据文档,人们期望这开始工作。但对我来说没有运气。如果我使用/ si /前缀访问url,我仍然会看到英文字符串。
答案 0 :(得分:4)
创建.po文件后,还有两个步骤:
./manage.py compilemessages
将.po文件编译为.mo文件引用Django translation documentation,After you create your message file – and each time you make changes to it – you’ll need to compile it into a more efficient form, for use by gettext. Do this with the django-admin compilemessages utility.
答案 1 :(得分:1)
我不确定这是否重要,但在我的国际化申请中我有这个:
LANGUAGES = (
("nl", "Nederlands"),
("en", "English"),
)
即,字符串周围没有_()
。您的代码片段没有显示您如何定义_
,the documentation表示您必须将其别名为ugettext_lazy()
。也许它会有所不同。
如果您将源代码提供给演示您的问题的最小Django应用程序(例如,在github上),那么还可以更容易地帮助您。