javascript 如何检测用户的语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18557494/
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 detect user's language
提问by Super Hornet
I'm going to detect user's keyboard language by javascript. I got a input (type of text) which should be filled with Persian characters. So, I need to alarm users change their keyboard language if their language is not Persian.
我将通过 javascript 检测用户的键盘语言。我得到了一个输入(文本类型),它应该用波斯字符填充。因此,如果用户的语言不是波斯语,我需要提醒用户更改他们的键盘语言。
What I have found is this:
我发现的是这样的:
window.navigator.language;
which detects browser's language which is always En-us.
它检测浏览器的语言,它总是 En-us。
How could I solve that?
我怎么能解决这个问题?
回答by Rahul Tripathi
You can try like this:-
你可以这样试试:-
var language = window.navigator.userLanguage || window.navigator.language;
alert(language);
navigator.userLanguageworks for IE
navigator.userLanguage适用于 IE
window.navigator.languageworks for firefox/opera/safari
window.navigator.language适用于 firefox/opera/safari
Also check out this Thread
也看看这个线程