语言环境在 Android 中返回空字符串
Locale is returning empty string in Android
我正在开发一个 android 应用程序,我需要根据用户的 Locale 国家(地区)进行一定的更改。
我找到了一个官方 link 并且我试图获得一个 Locale 但它返回一个空字符串。
这是我试过的代码,但我得到的所有字符串都是空字符串:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String localeCountry = LocaleList.getDefault().get(0).getDisplayCountry();
String localeISO3Country = LocaleList.getDefault().get(0).getISO3Country();
String locale = getResources().getConfiguration().getLocales().get(0).getCountry();
String locale1 = getResources().getConfiguration().getLocales().get(0).getDisplayCountry();
String locale2 = getResources().getConfiguration().getLocales().get(0).getDisplayVariant();
}else {
Locale current = getResources().getConfiguration().locale;
String currentCc = getResources().getConfiguration().locale.getCountry();
String iso3Country = getResources().getConfiguration().locale.getISO3Country();
String cCode = Locale.getDefault().getCountry();
}
我也尝试了 toString()
方法,但它返回给我的是唯一的语言,但根据文档,它应该返回包括语言和国家/地区在内的值。
这是我的代码:
String toString = getResources().getConfiguration().getLocales().toString();
in Log it printed only language:
[en]
我是不是漏掉了什么?
我来不及回答这个问题了,但我发布了我的答案,希望对其他人有所帮助。
根据文档,如果您的国家/地区语言不支持,那么您的 locale.toString 方法将返回 null 或空字符串。
这里是官方网站link
我发现这对我来说是一个可行的解决方法..
可能会有帮助..
String lCode = Locale.getDefault().getLanguage();
String cCode = Locale.getDefault().getCountry();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
Locale deviceLocales = Resources.getSystem().getConfiguration().getLocales().get(0);
lCode = deviceLocales.getLanguage();
cCode = deviceLocales.getCountry();
String serverCode = lCode+"-"+cCode;
Log.e(UserAPI.TAG,"Bigger Than Nougat code "+serverCode);
}
我正在开发一个 android 应用程序,我需要根据用户的 Locale 国家(地区)进行一定的更改。
我找到了一个官方 link 并且我试图获得一个 Locale 但它返回一个空字符串。
这是我试过的代码,但我得到的所有字符串都是空字符串:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String localeCountry = LocaleList.getDefault().get(0).getDisplayCountry();
String localeISO3Country = LocaleList.getDefault().get(0).getISO3Country();
String locale = getResources().getConfiguration().getLocales().get(0).getCountry();
String locale1 = getResources().getConfiguration().getLocales().get(0).getDisplayCountry();
String locale2 = getResources().getConfiguration().getLocales().get(0).getDisplayVariant();
}else {
Locale current = getResources().getConfiguration().locale;
String currentCc = getResources().getConfiguration().locale.getCountry();
String iso3Country = getResources().getConfiguration().locale.getISO3Country();
String cCode = Locale.getDefault().getCountry();
}
我也尝试了 toString()
方法,但它返回给我的是唯一的语言,但根据文档,它应该返回包括语言和国家/地区在内的值。
这是我的代码:
String toString = getResources().getConfiguration().getLocales().toString();
in Log it printed only language:
[en]
我是不是漏掉了什么?
我来不及回答这个问题了,但我发布了我的答案,希望对其他人有所帮助。
根据文档,如果您的国家/地区语言不支持,那么您的 locale.toString 方法将返回 null 或空字符串。
这里是官方网站link
我发现这对我来说是一个可行的解决方法.. 可能会有帮助..
String lCode = Locale.getDefault().getLanguage();
String cCode = Locale.getDefault().getCountry();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
Locale deviceLocales = Resources.getSystem().getConfiguration().getLocales().get(0);
lCode = deviceLocales.getLanguage();
cCode = deviceLocales.getCountry();
String serverCode = lCode+"-"+cCode;
Log.e(UserAPI.TAG,"Bigger Than Nougat code "+serverCode);
}