T
T
tommmm2018-01-29 13:07:51
JavaScript
tommmm, 2018-01-29 13:07:51

How to implement a simple language change on the site?

My knowledge was not enough to solve the problem, and I could not find the answer in Google.
There is a business card site with a small number of html pages (~12-14 per version). "site.ru" (main) and "site.ru/en" (English).
You need to determine the user's language (by browser language) and redirect to the desired link. At the same time, the site has the ability to switch the language through the link. Once clicked, this state is written to the cookie.
Thanks for any help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MyMac, 2018-01-29
@tommmm

If pure html, then you can define it using JS:

<script type="text/javascript">
var userLang = navigator.language || navigator.userLanguage; 
window.location = "https://my.site.ru/" + userLang;
</script>

But this is not the most reliable way. It’s more correct to watch Accept-Language on the backend.
Another beautiful solution came across through the Google API:
https://github.com/dansingerman/jQuery-Browser-Lan...

A
Andrey Tsvetkov, 2018-01-29
@yellow79

navigator.language
Returns the language in the browser

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question