/ i18n / setlang /只获得pt-br

时间:2015-04-08 14:06:51

标签: python django

我在这里需要一些帮助,整个上午一直试图弄清楚...我需要为这个网站进行国际化,我有设置和所有内容双重检查,po / mo文件正确在locale文件夹中,但每次我点击"英语"在表单上它实际上获得了pt-br,如cmd服务器日志所示,并保持在同一页面上,而不进行翻译!

我不明白为什么它只会得到pt-br,我已经搜遍了谷歌,甚至在这里,但我仍然无法获得解决方案!

在我使用(最后一个代码段)测试的视图中,它也只获得了pt-br!我输入浏览器" localhost / set_language / en",它重定向到主页,但在cmd服务器日志中,它显示它仍然是pt-br!

我运行了debuggs,它正确地输入了' if!#/ p>

我已经没有想法为什么它只是获得pt-br!我刚刚开始学习django,所以如果有人能帮我解决这个问题,我真的很感激!

p.s:我测试的浏览器的语言设置为英语!

这是表单代码:



{% load i18n %}

<ul>
    <li>
    	<form name="setLangEnglish" method="post" action="/i18n/setlang/">{% csrf_token %}
        	<input name="next" type="hidden" value="/" />
        	<input type="hidden" name="language" value="en"/>
        	<input class="language-switcher" type="submit" alt="English" name="submit" value="English"/>
    	</form>
    </li>
    <li>
    	<form name="setLangPortuguese" method="post" action="/i18n/setlang/">{% csrf_token %}
    		<input name="next" type="hidden" value="/" />
    		<input type="hidden" name="language" value="pt-br"/>
    		<input class="language-switcher" type="submit" alt="Portugues" name="submit" value="Portugues"/>
    	</form>
    </li>
</ul>
&#13;
&#13;
&#13;

这里是settings.py:

&#13;
&#13;
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'apps.urls'

WSGI_APPLICATION = 'apps.wsgi.application'

LOCALE_PATHS = (os.path.join(PROJECT_DIR.parent, 'locale/'))


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

   
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'pt-br'

LANGUAGES = (
    ('en', u'Inglês'),
    ('pt-br', u'Português'),
    ('es', u'Espanhol'),
)

TIME_ZONE = 'America/Sao_Paulo'

USE_I18N = True

USE_L10N = True

USE_TZ = False
&#13;
&#13;
&#13;

这是url.py:

&#13;
&#13;
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.views.generic import TemplateView

from django.conf.urls.i18n import i18n_patterns

urlpatterns = patterns('',   
    url(r'^i18n/', include('django.conf.urls.i18n')),
    url(r'^$', 'apps.noticia.views.home', name='home'),
    url(r'^set_language/(?P<language>.+?)/$', 'apps.noticia.views.set_language', name='set_language'),
    

    #url de terceiros
    url(r'^grappelli/', include('grappelli.urls')), 
    url(r'^admin/', include(admin.site.urls)),
    url(r'^ckeditor/', include('ckeditor.urls')),
)

urlpatterns += i18n_patterns('',
    url(r'^institucional/', include('apps.institucional.urls', namespace='institucional')),
    url(r'^ingresso/', include('apps.ingresso.urls', namespace='ingresso')),
    url(r'^ensino/', include('apps.ensino.urls', namespace='ensino')),
    url(r'^nucleos/', include('apps.nucleo.urls', namespace='nucleo')),
    url(r'^pesquisa/', include('apps.pesquisa.urls', namespace='pesquisa')),
    url(r'^extensao/', include('apps.extensao.urls', namespace='extensao')),
    url(r'^editais/', include('apps.edital.urls', namespace='edital')),
    url(r'^noticias/', include('apps.noticia.urls', namespace='noticia')),
    url(r'^eventos/', include('apps.evento.urls', namespace='evento')),
    url(r'^contato/', include('apps.contato.urls', namespace='contato')),
)
&#13;
&#13;
&#13;

这是我正在测试的视图:

&#13;
&#13;
def set_language(request, language):

    from django.utils.http import is_safe_url
    from django import http

    response = http.HttpResponseRedirect(reverse('home'))

    lang_code = language
    if lang_code and check_for_language(lang_code):
        print 'asdsadsa'
        if hasattr(request, 'session'):
            print 'asddds'
            request.session['django_language'] = lang_code
            translation.activate(lang_code)
        else:
            response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)

    return response
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您应该使用正确的语言会话密钥(django.utils.translation.LANGUAGE_SESSION_KEY)因为此密钥用于从LocaleMiddleware中的会话中获取所有未来http请求的当前语言代码。

此外,您的html表单不正确,因为他们在POST数据中发送语言代码,但您的url + view声明期望语言代码直接位于url内。这是不同的方法。为什么不按原样使用django setlang视图?也许你有问题让它发挥作用,不是吗?

P.S。如果它们是真实的,我建议你删除你的数据库凭证。