在django视图中设置语言

背景:支付服务在幕后回拨支付结果时调用该视图,之后我需要以正确的语言发送电子邮件以确认支付等。 我可以从支付服务器的请求中获取语言代码,并希望将其与Django的i18n系统一起使用,以确定将哪些语言发送到我的电子邮件中。

所以我需要从视图中设置我的Django应用程序的语言。 然后做我的模板渲染和电子邮件一起去。

设置request.session['django_language'] = lang只影响下一个视图,当我testing。

有没有其他办法可以做到这一点?

干杯,

家伙

引用来自Django的语言环境中间件( django.middleware.locale.LocaleMiddleware )的部分:

 from django.utils import translation class LocaleMiddleware(object): """ This is a very simple middleware that parses a request and decides what translation object to install in the current thread context. This allows pages to be dynamically translated to the language the user desires (if the language is available, of course). """ def process_request(self, request): language = translation.get_language_from_request(request) translation.activate(language) request.LANGUAGE_CODE = translation.get_language() 

translation.activate(language)translation.activate(language)是重要的一点。

请务必在process_response中添加停用,否则您将遇到不同线程的问题。

 from django.utils import translation class LocaleMiddleware(object): """ This is a very simple middleware that parses a request and decides what translation object to install in the current thread context. This allows pages to be dynamically translated to the language the user desires (if the language is available, of course). """ def process_request(self, request): language = translation.get_language_from_request(request) translation.activate(language) request.LANGUAGE_CODE = translation.get_language() def process_response(self, request, response): translation.deactivate() return response 

有时候,您希望为给定视图强制使用某种语言,但让浏览器语言设置select其余视图的语言。 我还没有想出如何改变视图代码中的语言,但是你可以通过实现一个简单的中间件来实现

lang_based_on_url_middleware.py:

 from django.utils import translation # Dictionary of urls that should use special language regardless of language set in browser # key = url # val = language code special_cases = { '/this/is/some/url/' : 'dk', '/his/is/another/special/case' : 'de', } class LangBasedOnUrlMiddleware(object): def process_request(self, request): if request.path_info in special_cases: lang = special_cases[request.path_info] translation.activate(lang) request.LANGUAGE_CODE = lang 

在settings.py中:

 MIDDLEWARE_CLASSES = ( ... 'django.middleware.locale.LocaleMiddleware', 'inner.lang_based_on_url_middleware.LangBasedOnUrlMiddleware', # remember that the order of LocaleMiddleware and LangBasedOnUrlMiddleware matters ... ) 

不是一个优雅的解决scheme,但工作。

request.LANGUAGE_CODE如果LocaleMiddleware激活

如果只是想为某种语言得到一个语言的翻译后的string,你可以像下面这样使用override作为装饰器 :

 from django.utils import translation from django.utils.translation import ugettext as _ with translation.override(language): welcome = _('welcome') 

如果您使用的是django 1.10或更高版本,那么定制中间件有一个新的语法:

 from django.utils import translation class LocaleMiddleware(object): def __init__(self, get_response): self.get_response = get_response def __call__(self, request): language_code = 'en' #TODO, your logic translation.activate(language_code) response = self.get_response(request) translation.deactivate() return response