set language within a django view

后端 未结 8 2093
暖寄归人
暖寄归人 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:29

    If just want to get the translated strings for a language for whatever reason, you can use override as a decorator like this:

    from django.utils import translation
    from django.utils.translation import ugettext as _
    
    with translation.override(language):
        welcome = _('welcome')
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题