2

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

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.

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

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 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?

A minimal view:

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

And corresponding template (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>

Relevant settings:

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:

4

2 回答 2

3

呸!我和这个人有同样的问题: https ://code.djangoproject.com/ticket/18492

LOCALE_PATHS元组中缺少尾随逗号。太糟糕了 Django 并没有为此引发错误。

于 2013-08-15T12:37:42.573 回答
0

我有一个类似的问题,我解决了确保每当我更新 django.po 文件时,我都会编译它:

./manage.py compilemessages

翻译是从编译文件 (django.mo) 而不是 .po 文件完成的

  1. 生成翻译文件:./manage.py makemessages -a
  2. 翻译:手动或使用自动翻译等工具
  3. 编译文件:./manage.py compilemessages
  4. 测试以查看更改:不是首先选择默认语言,请确保更改语言。例如更改localhost:8000/en/localhost:8000/fr/localhost:8000/foo/取决于您要查看的语言

我希望这有帮助

于 2018-01-19T16:14:43.930 回答