在Chrome中检索浏览器语言 [英] Retrieving browser language in 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 asnavigator.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 theAccept-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屋!