Django不正确地翻译网站 [英] Django not translating the site properly

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

问题描述

在这个花费太多的时间之后,StackOverflow是为了抢救。



我配置了我的settings.py如下:

  
TIME_ZONE ='欧洲/柏林'

LANGUAGE_CODE ='de'

LANGUAGES =(
('en',u'English') ,
('de',u'German'),
('fr',u'French'),


USE_I18N = True

USE_L10N = True

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


TEMPLATE_CONTEXT_PROCESSORS =(
'django.contrib.auth.context_processors.auth',
'django.core。 context_processors.debug',
'django.core.context_processors.i18n',
'djang o.core.context_processors.request',
'django.core.context_processors.static',
'django.contrib.messages.context_processors.messages',

..

在我的 base.html 我有一个表单如下:

 < form action =/ i18n / setlang /method =post> 
{%csrf_token%}
< input name =nexttype =hiddenvalue =//>
< select name =language>
{%get_language_info_list for LANGUAGES as language%}
{%for language in languages%}
< option value ={{language.code}}> {{language.name_local }}({{language.code}})< / option>
{%endfor%}
< / select>
< input type =submitvalue =Go/>
< / form>

我的 urls.py

  urlpatterns = patterns('',
url(r'^ i18n /',include('django.conf.urls。 i18n')),
url(r'^ $','MainApp.views.index'),#root

在相同的 base.html 文件中,我顶部 {%load i18n%} / code>并且在正文中,我有一个样本 {%trans这是标题。 %} 。在运行服务器之前,我做了:

  django-admin.py makemessages -l de 
django-admin.py makemessages -l fr

上面的示例文本由 makemessages ,我提供了 msgstr 的相应翻译。之后,我做了 django-admin.py compilemessages。命令运行得很好,并在相应的区域设置文件夹中生成了.mo文件。



我运行服务器,表单不起作用。从另一个StackOverflow文章中,我暗示删除了我所做的#,模糊行。我究竟做错了什么?



谢谢!

解决方案

你应该把<$ c $在您的MIDDLEWARE_CLASSES中 SessionMiddleware 之后,c> LocaleMiddleware

  MIDDLEWARE_CLASSES =(
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware' ,
....

中间件类的顺序很重要。 LocaleMiddleware 使用会话数据来检测用户语言,因此必须在 SessionMiddleware 之后。在这里的文档中也提到了 https://docs.djangoproject.com/en/1.3/topics/i18n/deployment/#how-django-discovers-language-preference



让我们希望这对你有用!


After spending too many hours on this, StackOverflow is for the rescue.

I configured my settings.py as below:

...
TIME_ZONE = 'Europe/Berlin'

LANGUAGE_CODE = 'de'

LANGUAGES = (
  ('en', u'English'),
  ('de', u'German'),
  ('fr', u'French'),
)

USE_I18N = True

USE_L10N = True

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

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.request',
    'django.core.context_processors.static',
    'django.contrib.messages.context_processors.messages',
)
...

In my base.html file, I have a form as below:

<form action="/i18n/setlang/" method="post">
    {% csrf_token %}
    <input name="next" type="hidden" value="/" />
    <select name="language">
        {% get_language_info_list for LANGUAGES as languages %}
        {% for language in languages %}
            <option value="{{ language.code }}">{{ language.name_local }} ({{ language.code }})</option>
        {% endfor %}
    </select>
    <input type="submit" value="Go" />
</form>

My urls.py:

urlpatterns = patterns('',
    url(r'^i18n/', include('django.conf.urls.i18n')),
    url(r'^$', 'MainApp.views.index'), #root
)

In the same base.html file, I have on top {% load i18n %} and in the body, I have a sample {% trans "This is the title." %}. Before running the server, I did:

django-admin.py makemessages -l de
django-admin.py makemessages -l fr

The sample text above was picked up by makemessages, and I provided the respective translations for msgstr. After that, I did django-admin.py compilemessages. The command ran nicely and generated the .mo files in the respective locale folders.

I run the server and the the form does not work. From a another StackOverflow post, I was hinted to remove the #, fuzzy lines, which I did. What am I doing wrong?

Thanks!

解决方案

You should put the LocaleMiddleware after the SessionMiddleware in your MIDDLEWARE_CLASSES:

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
....
)

The order of middleware classes is important. The LocaleMiddleware uses session data to detect the user language, so it must come after the SessionMiddleware. It is also mentioned in the docs here https://docs.djangoproject.com/en/1.3/topics/i18n/deployment/#how-django-discovers-language-preference

Let's hope this works for you!

这篇关于Django不正确地翻译网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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