在Chrome中检索浏览器语言 [英] Retrieving browser language in chrome

查看:136
本文介绍了在Chrome中检索浏览器语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力让momentjs正确地检测浏览器语言并本地化时间显示。 (根据



我在猜测我的chrome安装错误看起来很可能是因为正确的语言在 navigator.languages 中),或者选择浏览器语言的方法不太正确?

是否有其他方法可以用来获取以外的语言window.navigator.userLanguage || window.navigator.language

解决方案

W3C草案指出:


  • navigator.language 是用户的首选语言或合理的语言。这不一定与 navigator.languages [0]

  • navigator.languages 列出用户的首选语言,首选最优先的语言。它预期与 Accept-Language 标头具有相同的值。


通过在VM中快速实验的样子, navigator.languages [0] 导致最准确的结果, navigator.language 是操作系统的语言。



看起来好像en-GB你是获取是指用户的语言,而en-NZ是指用户语言环境 & date / time formatting)。



由于这仍然是一份草稿,因此您得到的结果可能随实施而改变。


I've been trying to get momentjs to correctly detect the browser language and localise the time displays. (as per Locale detection with Moment.js)

But I have been having a bit of trouble getting the correct locale out of chrome.

It seems like chrome has the correct list of languages configured for my browser in navigator.languages but navigator.language is returning something completely different.

I'm guessing either I have chrome setup wrong (which doesn't seem likely given the correct languages are in navigator.languages) or the method for selecting the browser language isn't quite right?

Is there a different way I should be using to get the language other than window.navigator.userLanguage || window.navigator.language ?

解决方案

The W3C draft states that:

  • navigator.language is the user's "preferred language" or a "plausible language". This is not necessarily the same as navigator.languages[0].
  • navigator.languages lists the "user's preferred languages, with the most preferred language first". It is expected to have the same value as the Accept-Language header.

By the looks of quickly experimenting in a VM, navigator.languages[0] leads to the most accurate results, navigator.language being the language of the OS.

It looks as if the "en-GB" you are getting refers to the language of the user, whereas the "en-NZ" refers to the user locale (number & date/time formatting).

Since this is still a draft, the results you get may change with implementation.

这篇关于在Chrome中检索浏览器语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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