from django.utils import translation
def home_index(request):
return redirect('home', lang='zh-hans')
def home(request):
ua = request.META.get('HTTP_USER_AGENT')
accept = request.META.get('HTTP_ACCEPT')
# 从请求META中获取
accept_lang = request.META.get('HTTP_ACCEPT_LANGUAGE')
# 从请求中获取
lang_a = translation.get_language_from_request(request)
# 从路径中获取
lang_path = translation.get_language_from_path(request.path)
# 激活语言
translation.activate(lang_a)
return render(request, 'home.html')