Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get Default language of browser in Django

Tags:

python

django

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 thhat are activated in browser from this request.META['HTTP_ACCEPT_LANGUAGE'] . But how I can get that language which is set as default .

like image 960
n.imp Avatar asked Mar 18 '15 07:03

n.imp


People also ask

How do I get current language in Django?

Functions of particular interest are django. utils. translation. get_language() which returns the language used in the current thread.

How does Django discover language preferences?

This process relies on the GNU gettext toolset. Once this is done, Django takes care of translating web apps on the fly in each available language, according to users' language preferences.


1 Answers

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

like image 133
Nwawel A Iroume Avatar answered Sep 20 '22 18:09

Nwawel A Iroume