如何在Android设备中选择当前语言?
当前回答
Locale.getDefault().getDisplayLanguage()
会给你语言的书面名称,例如,英语,荷兰语,法语
Locale.getDefault().getLanguage()
会给你语言代码,例如:en, nl, fr
两种方法都返回String
其他回答
Locale.getDefault().getLanguage()是虚拟机语言
Locale.getDefault().getLanguage()
它是当前运行应用程序的虚拟机实例的语言。它由java类(如DateFormat等)使用。如果您使用某些java类,则在更改应用程序区域设置时可能需要修改此选项。如果你在改变你的App语言环境时修改了这个,它与android的语言是不一样的。
context.getConfiguration().locale.getLanguage()是活动语言
context.getConfiguration().locale.getLanguage()
这是您活动中设置的语言。 在最新的SDK版本中,以下是可取的
context.getConfiguration().getLocales().get(0).getLanguage()
Resources.getSystem().getConfiguration().getLocales()给出用户在系统级添加的所有地区
这将为您提供用户在系统级别设置的第一个区域设置。
Resources.getSystem().getConfiguration().getLocales().get(0).getLanguage()
大量的用户是多语言的,所以你可能想要循环使用locale。
下面是获取设备国家的代码。兼容所有版本的android甚至奥利奥。
解决方案:如果用户没有sim卡,那么在手机设置或当前语言选择时获取他所使用的国家。
public static String getDeviceCountry(Context context) {
String deviceCountryCode = null;
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if(tm != null) {
deviceCountryCode = tm.getNetworkCountryIso();
}
if (deviceCountryCode != null && deviceCountryCode.length() <=3) {
deviceCountryCode = deviceCountryCode.toUpperCase();
}
else {
deviceCountryCode = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0).getCountry().toUpperCase();
}
// Log.d("countryCode"," : " + deviceCountryCode );
return deviceCountryCode;
}
Locale.getDefault().getDisplayLanguage()
会给你语言的书面名称,例如,英语,荷兰语,法语
Locale.getDefault().getLanguage()
会给你语言代码,例如:en, nl, fr
两种方法都返回String
您可以从当前区域“提取”语言。你可以通过标准的Java API或者使用Android Context来提取语言环境。例如,下面这两行是等价的:
String locale = context.getResources().getConfiguration().locale.getDisplayName();
String locale = java.util.Locale.getDefault().getDisplayName();
我检查了我的Android 4.1.2设备上的Locale方法,结果是:
Locale.getDefault().getLanguage() ---> en
Locale.getDefault().getISO3Language() ---> eng
Locale.getDefault().getCountry() ---> US
Locale.getDefault().getISO3Country() ---> USA
Locale.getDefault().getDisplayCountry() ---> United States
Locale.getDefault().getDisplayName() ---> English (United States)
Locale.getDefault().toString() ---> en_US
Locale.getDefault().getDisplayLanguage()---> English
Locale.getDefault().toLanguageTag() ---> en-US
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用