+ 3
Regarding html language
I have a website and want to change the language (the content is in English but want to change it accordingly) according to the browser language... How to do so?
2 Respostas
+ 7
+ 3
There are a lot of ways to do that.
Solution 1:
If you have a lot of JavaScript usage in your website, you could use a JavaScript library like i18next to represent and handle the translations. I used i18next for a single page application that used a framework similar to AngularJS.
https://www.i18next.com/
Solution 2:
Another option if your website is mostly static content is to make a separate version of the site for each language. You could use a separate subdomain for each language such as en.yoursite.com and fr.yoursite.com. If you redirect only from www.yoursite.com and not from any of the language-specific subdomains, that could prevent a redirect loop and is likely good for SEO.
Detecting the preferred language of the browser is the easiest part of the problem to solve. The server could check the "Accept-Language" HTTP header of the request to find the preferred language out of the ones you're supporting. After selecting one, you could do the redirect for solution 2 with a redirect HTTP response or get JavaScript to set i18next to it. More details on Accept-Language are at: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language
Consider letting the user override the automatic language selection too. A redirect after 5 seconds of notice on a page saying they'll be redirected shortly sounds good. That should prevent frustration for someone on a public computer configured to prefer a language other than the visitor's true preference.
It will help if you're fluent in both languages you want to support or have a friend who is because integrating with something like Google Translate generally isn't worth it. A lot of people already know how to use Google Translate so you baking its functionality into the site doesn't help a lot of people.