Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting the browser language in Python Flask

Tags:

python

flask

I need to get the browser language with Python Flask. I have tried to use:

request.headers.get('languages')

This however returns None.

I tried whoer.net and it returned it-IT for my browser language. How can I detect the browser language with Python Flask ?

like image 813
PythonFun Avatar asked Dec 10 '22 23:12

PythonFun


1 Answers

Check out Werkzeug's LanguageAccept data structure, or just try something like this to get a best match:

supported_languages = ["en", "nl", "it"]
lang = request.accept_languages.best_match(supported_languages)

If you need more than that, then check out Flask-Babel.

like image 95
Pieter Ennes Avatar answered Jan 07 '23 10:01

Pieter Ennes