Django翻译:设置LANGUAGE_CODE正常工作-不接受语言吗? [英] Django translations: setting of LANGUAGE_CODE working - accept-languages not?

查看:45
本文介绍了Django翻译:设置LANGUAGE_CODE正常工作-不接受语言吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的一个Django小项目修复翻译.取消Django 2.2.12的版本.

I'm trying to fix translations for a little Django project of mine. Unsing Django 2.2.12.

当前状态是,我可以通过将LANGUAGE_CODE设置为不同的值来切换翻译.但是我想通过浏览器的接受语言来切换翻译-似乎不起作用.我的怀疑是,由于某种原因,中间件配置不正确.

Current status is, that I can switch translations by setting LANGUAGE_CODE to different values. But I would like to switch translations by means of accept-language of browser - which does not seem to work. My suspicion is, that for some reason the middleware is not configured correctly.

这是我为中间件设置的:

This is what I have set for middlewares:

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'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',
]

还有语言:

LANGUAGES = (
('de', 'German'),
('tr', 'Turkish'),
('en', 'English'),
('it', 'Italian'),
('fr', 'French'),
)

从文档中可以得出结论,如果LANGUAGE_CODE的设置更改了翻译(对于我来说就是这种情况),那么确定正确翻译的所有其他方法都将失败(包括cookie).因为在最后一步中评估了LANGUAGE_CODE.

From the documentation I conclude, that if setting of LANGUAGE_CODE changes the translation - as is the case for me - then all other means of determining the proper translation have failed (including cookies). Because LANGUAGE_CODE is evaluated in the last step.

我想知道我该如何检查中间件在做的事情是正确的-以及为什么它似乎忽略了接受语言.对此的任何提示都将受到高度赞赏!

I would like to know how I could check it the middleware is doing it's thing correclty - and why it seems to ignore accept-language. Any hints on this are highly appreciated!

推荐答案

使用 settings.USE_I18N = True ,关键代码在 django.utils.translation.trans_real.get_language_from_request()中功能由 LocaleMiddleware.process_request()调用.优先级从高到低依次找到language_code.

With settings.USE_I18N=True, the key codes are in django.utils.translation.trans_real.get_language_from_request() function which is called by LocaleMiddleware.process_request(). language_code is found out with priority from high to low as below.

首先,如果urlconf由i18n_patterns函数处理,它将遵循路径中的显式language_code信息.

firstly, if urlconf is handled by i18n_patterns function, it will respect the explicit language_code info in path.

lang_code = get_language_from_path(request.path_info) 

如果不是或无效,作为尊敬的用户,请尝试将language_code从request.session中删除,该代码可以在某个位置进行设置,通常,客户可以将{language:'en'}发布到django.views.i18n.set_language(request)的网址路径中.

if not or invalid, as respecting user, then try getting language_code out of request.session which could be set it somewhere, and usually client could post {language:'en'} to the url path of django.views.i18n.set_language(request).

lang_code = request.session.get(LANGUAGE_SESSION_KEY) # LANGUAGE_SESSION_KEY = '_language'

如果不是无效的,请尝试从request.cookies中删除language_code作为尊重客户端.

if not or invalid, then try getting language_code out of request.cookies as respecting client.

lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME) # LANGUAGE_COOKIE_NAME = 'django_language'

如果不是无效的,则尝试从ACCEPT_LANGUAGE中获取第一个有效的language_code作为尊重的浏览器.

if not or invalid, then try getting the first valid language_code out of ACCEPT_LANGUAGE as respecting browser.

accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '')  # usually it has a lot of language info

如果无效,否则:

lang_code = get_supported_language_variant(settings.LANGUAGE_CODE)

因此,您可以打印所有相关信息以找出答案.

So, you can print all the related info to figure it out.

这篇关于Django翻译:设置LANGUAGE_CODE正常工作-不接受语言吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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