繁体   English   中英

Django 翻译在我的情况下不起作用

[英]Django translation not working in my case

我为问这个感到难过,因为它被问了很多次:

django - 如何使翻译工作?

Django 国际化和翻译问题

如何以正确的方式设置 Django 翻译?

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'),
)

网址.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,我仍然会看到英文字符串。

创建 .po 文件后,还有两个步骤:

  1. 翻译 .po 文件中的消息
  2. 运行./manage.py compilemessages将您的 .po 文件编译为 .mo 文件

引用Django 翻译文档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. 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.

我不确定这是否重要,但在我的国际化应用程序中,我有这个:

LANGUAGES = (
    ("nl", "Nederlands"),
    ("en", "English"),
    )   

即,没有_()围绕字符串。 您的代码片段没有显示您如何定义_文档说您必须将其别名为ugettext_lazy() 也许它会有所作为。

如果您将源代码提供给一个最小的 Django 应用程序来演示您的问题(例如,在 github 上),那么更容易为您提供帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM