Javascript 如何使用javascript获取浏览器当前的语言环境首选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2678230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to getting browser current locale preference using javascript?
提问by The Sheek Geek
Does anyone know how to obtain the browser culture from Firefox and Google Chrome using javascript? Note: This is an asp.net 3.5 web application.
有谁知道如何使用 javascript 从 Firefox 和 Google Chrome 获取浏览器文化?注意:这是一个 asp.net 3.5 Web 应用程序。
The requirement is to try and set the application's display culture based on the browser culture. I have found very few bits and pieces of information for the other browsers but they do not seem to work.
要求是尝试根据浏览器文化设置应用程序的显示文化。我发现其他浏览器的信息很少,但它们似乎不起作用。
I am able to get it in IE with the following snippet of code:
我可以使用以下代码片段在 IE 中获取它:
var browserCulture = this.clientInformation.browserLanguage;
Any info would be great!
任何信息都会很棒!
回答by bobince
The following properties exist on the navigatorobject (which can also be known as clientInformationon IE but there's no reason ever to use that name):
navigator对象上存在以下属性(clientInformation在 IE 上也可以称为它,但没有理由使用该名称):
language(non-IE, browser install language)browserLanguage(IE, browser install language)userLanguage(IE, user-level OS-wide language setting)systemLanguage(IE, OS installation language)
language(非 IE,浏览器安装语言)browserLanguage(IE,浏览器安装语言)userLanguage(IE,用户级操作系统范围的语言设置)systemLanguage(IE、OS安装语言)
But! You should never use any of these properties!They will be the wrong language in many cases.
但!你永远不应该使用这些属性中的任何一个!在许多情况下,它们将是错误的语言。
None of them reflect the language settings the user actually gets to configure in the browser's ‘preferred languages' UI, and they are difficult-to-impossible for users to change. You will cause big frustration by using any of these values without an additional easy manual way to switch languages.
它们都没有反映用户在浏览器的“首选语言”用户界面中实际配置的语言设置,并且用户很难更改它们。如果使用这些值中的任何一个,而没有额外的简单的手动切换语言的方法,您将造成很大的挫败感。
The correct place you should sniff to decide what language to use by default, as configured by the normal browser UI, is the Accept-Languageheader passed to your server in the HTTP request. This is a ranked list of preferred languages from which you can pick, and it's what ASP.NET uses to guess an automatic client Culture, if you use that.
您应该嗅探以决定默认使用哪种语言的正确位置(由普通浏览器 UI 配置)是Accept-Language在 HTTP 请求中传递给您的服务器的标头。这是您可以从中选择的首选语言的排名列表,如果您使用它,它就是 ASP.NET 用来猜测自动客户端文化的内容。
Unfortunately, this property is not available from JavaScript!
不幸的是,此属性在 JavaScript 中不可用!
What you typically do is use your server side to parse the Accept-Languageheader and choose a single appropriate language to use from it. In ASP.NET you can get a pre-sorted list from HttpRequest.UserLanguagesand pick the first that you like.
您通常做的是使用您的服务器端来解析Accept-Language标头并从中选择一种合适的语言来使用。在 ASP.NET 中,您可以从HttpRequest.UserLanguages获取预先排序的列表并选择您喜欢的第一个列表。
You then spit that language name out into a <script>element to pass the language information to the client side.
然后,您将该语言名称吐出到一个<script>元素中,以将语言信息传递给客户端。
回答by Sologoub
Try this:
尝试这个:
var l_lang;
if (navigator.userLanguage) // Explorer
l_lang = navigator.userLanguage;
else if (navigator.language) // FF
l_lang = navigator.language;
else
l_lang = "en";
Source: http://forums.digitalpoint.com/showthread.php?t=631706
回答by Mats
navigator.languagesis an array and contains all the selected languages in order. And only works for Chrome and Firefox.
navigator.languages是一个数组,按顺序包含所有选定的语言。并且仅适用于 Chrome 和 Firefox。
It is not the same as navigator.languageand by that I mean that navigator.languagedoes not necessarily match navigator.languages[0].
它是不一样的navigator.language,并通过我的意思是navigator.language不需要匹配navigator.languages[0]。
Just to be clear.
只是要清楚。
回答by Prithivi Thiyagu
To get locale in IE you need to write the server side code. I tried with servlet and got the accept language of the IE. It shows the languages which are selected by the user.Call the servlet in your client side and get the locale.
要在 IE 中获取语言环境,您需要编写服务器端代码。我尝试使用 servlet 并获得了 IE 的接受语言。它显示了用户选择的语言。在客户端调用 servlet 并获取语言环境。
String name=request.getHeader("accept-language");
AcceptClass ac=new AcceptClass();
ac.setAccLang(name);
Gson gs=new Gson();
String json = gs.toJson(ac);
response.setContentType("application/json");
response.getWriter().write(json);

