1. from django.utils import translation
    2. def home_index(request):
    3. return redirect('home', lang='zh-hans')
    4. def home(request):
    5. ua = request.META.get('HTTP_USER_AGENT')
    6. accept = request.META.get('HTTP_ACCEPT')
    7. # 从请求META中获取
    8. accept_lang = request.META.get('HTTP_ACCEPT_LANGUAGE')
    9. # 从请求中获取
    10. lang_a = translation.get_language_from_request(request)
    11. # 从路径中获取
    12. lang_path = translation.get_language_from_path(request.path)
    13. # 激活语言
    14. translation.activate(lang_a)
    15. return render(request, 'home.html')