W
W
weranda2022-02-18 12:44:39
Flask
weranda, 2022-02-18 12:44:39

How to determine the user's language in Python?

There is a site on Flask. The site is multilingual. How can you determine the user's language without JS?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
H
HemulGM, 2022-02-18
@HemulGM

No way, flask is a backend. His area of ​​responsibility is only you server. The server is your machine. Python does not have access to the client browser without js.
You can only read headers and cookies.

V
Vindicar, 2022-02-18
@Vindicar

The browser sends Accept-Language headers , you can also rely on geolocation.
But this should only be used as a way to determine the default language. Provide a way for the user to explicitly change the language.

D
Dr. Bacon, 2022-02-18
@bacon

Django defines it this way, everything for Flask already has something similar, well, or repeat it yourself

First, it looks for the language prefix in the requested URL. This is only performed when you are using the i18n_patterns function in your root URLconf. See Internationalization: in URL patterns for more information about the language prefix and how to internationalize URL patterns.
Failing that, it looks for a cookie.
The name of the cookie used is set by the LANGUAGE_COOKIE_NAME setting. (The default name is django_language.)
Failing that, it looks at the Accept-Language HTTP header. This header is sent by your browser and tells the server which language(s) you prefer, in order by priority. Django tries each language in the header until it finds one with available translations.
Failing that, it uses the global LANGUAGE_CODE settin

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question