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

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

问题描述

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



要求是根据浏览器文化尝试设置应用程序的显示区域。我发现其他浏览器的信息很少,但他们似乎并没有工作。

我能够得到它在IE浏览器下面的片段code:

  var browserCulture = this.clientInformation.browserLanguage; 

任何信息都会很棒!

navigator 对象(也可以称为 clientInformation IE浏览器,但没有任何理由使用该名称):


  • language (非IE,浏览器安装语言)

  • browserLanguage
  • userLanguage (IE,用户级操作系统范围内的语言设置) c $ c>(IE,OS安装语言)


    但是! 你不应该使用任何这些属性!在很多情况下,它们将是错误的语言。



    它们都不反映用户的语言设置实际上可以在浏览器的首选语言界面中进行配置,用户很难做出改变。您将使用任何这些值,而无需额外的简单的手动方法来切换语言,从而造成很大的挫折。



    您应该嗅探的正确位置以决定默认使用哪种语言,正常的浏览器UI配置,是 Accept-Language 头在HTTP请求中传递给你的服务器。这是您可以选择的首选语言的排名列表,这是ASP.NET用来猜测一个自动客户端文化,如果你使用它。



    不幸的是,这个属性不可用于JavaScript!



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

    然后把这个语言名字吐出到< script> / code>元素将语言信息传递给客户端。


    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.

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

    var browserCulture = this.clientInformation.browserLanguage;
    

    Any info would be great!

    解决方案

    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 (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.

    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.

    Unfortunately, this property is not available from JavaScript!

    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.

    You then spit that language name out into a <script> element to pass the language information to the client side.

    这篇关于如何获取浏览器当前的语言环境偏好使用JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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