set language within a django view

后端 未结 8 2117
暖寄归人
暖寄归人 2020-11-30 20:45

background: The view is called when a payment service pings back a payment outcome behind the scenes - afterwhich I need to send an email in the right langu

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 21:33

    If you are using django 1.10 or higher, there's a new syntax for custom middleware:

    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
    

提交回复
热议问题