简体   繁体   English

根据语言偏好更改网站版本

[英]Changing websites version based on Language preference

I have a french and an english version of my website:我的网站有法语版和英语版:
*example.com/ is my english version. *example.com/ 是我的英文版本。
*example.com/fr/ is my french version. *example.com/fr/ 是我的法语版本。

So they have the possibility to change the websites version by clicking on a flag,but by default if they arrive on example.com it will be in english.所以他们有可能通过点击一个标志来更改网站版本,但默认情况下,如果他们到达 example.com,它将是英文的。

I would like to give them the version corresponding to their language preference, i detect language preference with :我想给他们提供与他们的语言偏好相对应的版本,我用以下方法检测语言偏好:

var language = window.navigator.userLanguage || window.navigator.language;

But then what code do i use so that when they arrive on the webpage, it will give the version according to the user language preference ?但是,我使用什么代码,以便当他们到达网页时,它会根据用户语言偏好提供版本?

And if the website gives a french version but the user wants english version, still has the possiblity to change manualy.如果网站提供法语版本而用户想要英语版本,仍然可以手动更改。

You'll have to redirect them:您必须重定向它们:

if (window.navigator.language === 'fr') {
    window.location.href = "http://example.com/fr/";
}

Please keep in mind that the window.navigator.language property is implemented differently across browsers and you may have to find your preferred language in an array (like window.navigator.languages).请记住, window.navigator.language属性在不同浏览器中的实现方式不同,您可能需要在数组中找到您喜欢的语言(如 window.navigator.languages)。

A better option would be to check the Accept-Language header on the HTTP request on the server side, and redirect accordingly on the server side.更好的选择是在服务器端检查 HTTP 请求上的Accept-Language标头,并在服务器端相应地重定向。

There are also JavaScript-based internationalization methods, but they don't normally involve redirecting the site.还有基于 JavaScript 的国际化方法,但它们通常不涉及重定向站点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM