如何使用javascript获取浏览器当前的语言环境首选项? [英] How to getting browser current locale preference using javascript?

查看:36
本文介绍了如何使用javascript获取浏览器当前的语言环境首选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何使用 javascript 从 Firefox 和 Google Chrome 获取浏览器文化?注意:这是一个 asp.net 3.5 网络应用程序.

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.

要求是尝试根据浏览器文化设置应用程序的显示文化.我发现其他浏览器的信息很少,但它们似乎不起作用.

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.

我可以使用以下代码片段在 IE 中获取它:

I am able to get it in IE with the following snippet of code:

var browserCulture = this.clientInformation.browserLanguage;

任何信息都会很棒!

推荐答案

navigator 对象(在 IE 上也可以称为 clientInformation 但在没有理由使用这个名字):

The following properties exist on the navigator object (which can also be known as clientInformation on IE but there's no reason ever to use that name):

  • language(非 IE,浏览器安装语言)
  • browserLanguage(IE,浏览器安装语言)
  • userLanguage(IE,用户级操作系统范围的语言设置)
  • systemLanguage(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)

但是!您永远不应该使用任何这些属性!在许多情况下,它们将是错误的语言.

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.

您应该嗅探以决定默认使用哪种语言的正确位置(由普通浏览器 UI 配置)是 Accept-Language 标头在 HTTP 请求中传递到您的服务器.这是您可以从中选择的首选语言的排名列表,如果您使用它,它就是 ASP.NET 用来猜测自动客户端文化的内容.

The correct place you should sniff to decide what language to use by default, as configured by the normal browser UI, is the Accept-Language header 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.

遗憾的是,此属性在 JavaScript 中不可用!

Unfortunately, this property is not available from JavaScript!

您通常做的是使用服务器端解析 Accept-Language 标头并从中选择一种合适的语言来使用.在 ASP.NET 中,您可以从 获取预先排序的列表HttpRequest.UserLanguages 并选择您喜欢的第一个.

What you typically do is use your server side to parse the Accept-Language header and choose a single appropriate language to use from it. In ASP.NET you can get a pre-sorted list from HttpRequest.UserLanguages and pick the first that you like.

然后,您将该语言名称吐出到

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆