Django:仅翻译一个元素 [英] Django: Only a single element gets translated

查看:90
本文介绍了Django:仅翻译一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 2.6上将Django站点从1.2移植到1.5时,我遇到了国际化问题.

Porting a Django site from 1.2 to 1.5 on Python 2.6 i ran into problems with internationalization.

更奇怪的是,在整个站点中只翻译了一个字符串(嗯,差不多,当我测试时,日期过滤器可以翻译长月份名称).位于同一模板中的其他字符串不会得到翻译,所有翻译都位于单个po/mo文件中.所有翻译都在那里,经过Poedit验证,并由manage.py compilemessages编译.

The wierd thing is that only one string gets translated in the entire site (Well, almost, the date filter could translate long month names when I tested). Other strings located in the same template doesn't get translated, and all translations are located in a single po/mo file. All translations are there, verified with Poedit and compiled with manage.py compilemessages.

单个翻译字符串的原因是它与管理站点中的字符串匹配.

The reason for the single translated string was that it matched a string in the admin site.

在尝试使其正常工作时,我清除了LOCALE_PATH,重新启动了开发服务器(manage.py runserver),清除了所有浏览器缓存(即使该网站的元数据禁用了兑现),瞧瞧元素仍在翻译.我通过在此之后再次添加相同的文本来验证了这一点,并且仍然可以翻译该文本,因此不涉及客户端缓存.

While trying things to get it to work I cleared the LOCALE_PATH, restarted the dev server (manage.py runserver), cleared any browser cache (even though meta-data for the site disables cashing), lo and behold the element is still translated. I verified this by adding the same text again after, and it still gets translated, so no client side caching is involved.

语言切换按预期方式工作,并且唯一翻译的元素已更改为默认语言,{{ LANGUAGE_CODE }}确认了这一点. 我已经尝试清除会话数据和Django缓存(开发服务器似乎未使用它).

Language switching works as expected and the only translated element is changed to the default language, {{ LANGUAGE_CODE }} confirms this. I've tried clearing the session data and django cache (which doesn't seem to be used by the dev server).

有人可以猜猜这里发生了什么吗?没有任何调试标志可以获取更广泛的日志记录吗?

Can someone guess what's going on here? Isn't there any debug flags to get more extensive logging or something?

最小视图:

def locale_test(request):
    locale = request.GET.get('l', None)
    if locale:
        translation.activate(locale)
    di = {"foobar": _("foobar")}
    return render_to_response('locale_test.html',di, context_instance=RequestContext(request))

以及相应的模板(locale_test.html):

{% load i18n %}
<p>Language: {{ LANGUAGE_CODE }}</p>
<p>Matching string from admin site that gets translated correctly: {% trans "Log out" %}</p>
<p>Translated in template: {% trans "Foobar" %}</p>
<p>Translated in view: {{ foobar }}</p>

相关设置:

USE_I18N = True

USE_L10N = True

LANGUAGES = (
    ('en', 'English'),
    ('foo', 'Fooo'),
)

LANGUAGE_CODE = 'en'

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',

 )

 TEMPLATE_CONTEXT_PROCESSORS = (
      'django.contrib.auth.context_processors.auth',
      'django.core.context_processors.i18n',
      'django.core.context_processors.request',
     )
LOCALE_PATHS = ('/path/to/my/locale',)

作为参考,这些问题对我没有帮助:

For reference, these questions didn't help me:

  • django internationalization and translations issue
  • Django not translating the site properly
  • Django i18n does not work

推荐答案

Gah!我和这个家伙碰到了同样的问题: https://code.djangoproject.com/ticket/18492

Gah! I got bit by the same issue as this guy : https://code.djangoproject.com/ticket/18492

LOCALE_PATHS元组中缺少尾部逗号.太糟糕了,Django不会为此引发错误.

Namely that a trailing comma was missing in the LOCALE_PATHS tuple. Too bad Django doesn't raise an error for that.

这篇关于Django:仅翻译一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆