获取android中用户的语言 [英] Get the language of user in android
问题描述
http://web.archiveorange.com/archive/v/fwvde0wN3xcViMtADw6x
在 androids 的 webview 中,navigator.language 属性似乎总是en".那么,获取用户语言的最佳方法是什么?用本机java代码获取它并通过javascript将其倒入webview?还是其他更好的方法?
It seems that navigator.language property is always "en" in webview on androids. Then, what is the best way to get the language of user? Get it in native java code and pour it into webview by javascript? or any other better way?
推荐答案
我发现这个问题的解决方法是通过webview的settings来设置user agent:
The solution I found to this problem is to set the user agent through the webview's settings:
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
settings.setUserAgentString(Locale.getDefault().getLanguage());
在您的网络内容中,您可以通过以下方式检索它:
In your webcontent you then retrieve it through:
var userLang = navigator.userAgent;
这应该只用于显示本地内容的 webview.
This should only be used for a webview displaying local content.
这篇关于获取android中用户的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!