Reputation: 807
I want to fetch default language of browser. I have tried some code for this but nothing works for me correctly .
I can get an array of all languages that are activated in browser from this request.META['HTTP_ACCEPT_LANGUAGE']
. But how I can get that language which is set as default.
Upvotes: 10
Views: 9992
Reputation: 1
Basically, the 1st browser language which the code below gets is used to display the browser UI(Default) but not 100%. *I experimented with Google Chrome, Microsoft Edge, Firefox and Opera.
# The 1st language
# ↓↓
request.META['HTTP_ACCEPT_LANGUAGE'] # en,fr;q=0.9,ja;q=0.8
For example, if you set English
(1st) which is This language is used to display the Google Chrome UI(Default), French
(2nd) and Japanese
(3rd) on Google Chrome as shown below:
Then, the code below returns these languages in the order as above:
# The 1st language
# ↓↓
request.META['HTTP_ACCEPT_LANGUAGE'] # en,fr;q=0.9,ja;q=0.8
Next, if you set French
(1st), English
(2nd) which is This language is used to display the Google Chrome UI(Default) and Japanese
(3rd) on Google Chrome as shown below:
Then, the code below returns these languages in the order as above:
# The 1st language
# ↓↓
request.META['HTTP_ACCEPT_LANGUAGE'] # fr,en;q=0.9,ja;q=0.8
In addition, if you want to automatically detect and apply the 1st browser language to your django website, set LocaleMiddleware to MIDDLEWARE between SessionMiddleware and CommonMiddleware as shown below. *You can see my answer explaining it in details:
# "settings.py"
MIDDLEWARE = [
...
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware", # Here
"django.middleware.common.CommonMiddleware",
...
]
And, you can get browser languages on frontend in JavaScript as shown below:
<script>
console.log(navigator.languages); // ["fr", "en", "ja"]
console.log(navigator.language); // fr
</script>
Upvotes: 0
Reputation: 7242
Django 1.10 just change about this:
?: (1_10.W001) The MIDDLEWARE_CLASSES setting is deprecated in Django 1.10 and the MIDDLEWARE setting takes precedence. Since you've set MIDDLEWARE, the value of MIDDLEWARE_CLASSES is ignored.
Now do this instead:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
.......
]
Upvotes: 1
Reputation: 1379
add the middleware django.middleware.locale.LocaleMiddleware
in setting.py
following the middleware order as
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
# other middleware ...
)
and use request.LANGUAGE_CODE
to get the browser language.
source http://www.janosgyerik.com/
I used it to solve that problem in a project
Upvotes: 9
Reputation: 66
You can access current language through request.LANGUAGE_CODE as described here: https://docs.djangoproject.com/en/1.7/ref/templates/api/#django-core-context-processors-i18n
Upvotes: 2