10

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.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
n.imp
  • 787
  • 4
  • 11
  • 29

4 Answers4

9

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

Nwawel A Iroume
  • 1,249
  • 3
  • 21
  • 42
2

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

  • I think `request.LANGUAGE_CODE` gives you the value of language from settings not browser . – n.imp Mar 18 '15 at 07:19
  • if you use `django.template.context_processors.i18n` `request.LANGUAGE_CODE` is determined by browsers `Accept-Language` header as described here [https://docs.djangoproject.com/en/1.7/topics/i18n/](https://docs.djangoproject.com/en/1.7/topics/i18n/) – Rafał Korkosz Mar 19 '15 at 09:49
  • request.LANGUAGE_CODE is the activated language within Django. It's not necessarily equal to the browser's default HTTP_ACCEPT_LANGUAGE. – Simon Steinberger Dec 30 '18 at 08:09
1

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',
    .......
]
Gregory
  • 6,514
  • 4
  • 28
  • 26
0

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:

enter image description here

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:

enter image description here

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>
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129