您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Django视图中设置语言

在Django视图中设置语言

引用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)是重要的一点。

Go 2022/1/1 18:15:06 有571人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶