我们如何才能在Android设备中选择当前语言?
如果您想获得设备的选定语言,这可能对您有所帮助:
Locale.getDefault().getDisplayLanguage();
上面的答案不区分简单的中文和繁体中文。 qazxsw poi的作品返回“zh_CN”,“zh_TW”,“en_US”等。
参考:Locale.getDefault().toString()
,ISO 639-1是旧的。
如果API级别为24或更高,请使用https://developer.android.com/reference/java/util/Locale.html否则使用LocaleList.getDefault().get(0).getLanguage()
Locale.getDefault.getLanguage()
参考:private fun getSystemLocale(): String {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return LocaleList.getDefault().get(0).getLanguage();
} else {
return Locale.getDefault.getLanguage();
}
}
有两种语言。
OS的默认语言:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String locale = ims.getLocale();
目前的应用语言:
Locale.getDefault().getDisplayLanguage();
如果您选择的语言无法输入,希腊语可能会有所帮助。
getResources().getConfiguration().locale.getDisplayLanguage();//return string
现在尝试用希腊语
getDisplayLanguage().toString() = English
getLanguage().toString() = en
getISO3Language().toString() = eng
getDisplayLanguage()) = English
getLanguage() = en
getISO3Language() = eng
getDisplayLanguage().toString() = Ελληνικά
getLanguage().toString() = el
getISO3Language().toString() = ell
getDisplayLanguage()) = Ελληνικά
getLanguage() = el
getISO3Language() = ell
获取设备语言的正确方法如下:
if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
// your code here
}
希望能帮助到你。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return context.getResources().getConfiguration().getLocales().get(0);
} else {
return context.getResources().getConfiguration().locale;
}
将为您提供语言的Locale.getDefault().getDisplayLanguage()
名称,例如Written
English, Dutch, French
会给你Locale.getDefault().getLanguage()
,例如:language code
两种方法都返回String
en, nl, fr
public class LocalUtils {
private static final String LANGUAGE_CODE_ENGLISH = "en";
// returns application language eg: en || fa ...
public static String getAppLanguage() {
return Locale.getDefault().getLanguage();
}
// returns device language eg: en || fa ...
public static String getDeviceLanguage() {
return ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0).getLanguage();
}
public static boolean isDeviceEnglish() {
return getDeviceLanguage().equals(new Locale(LANGUAGE_CODE_ENGLISH).getLanguage());
}
public static boolean isAppEnglish() {
return getAppLanguage().equals(new Locale(LANGUAGE_CODE_ENGLISH).getLanguage());
}
}
这个解决方案对我有用。这将返回Android设备的语言(不是应用程序的本地语言)
Log.i("AppLanguage: ", LocalUtils.getAppLanguage());
Log.i("DeviceLanguage: ", LocalUtils.getDeviceLanguage());
Log.i("isDeviceEnglish: ", String.valueOf(LocalUtils.isDeviceEnglish()));
Log.i("isAppEnglish: ", String.valueOf(LocalUtils.isAppEnglish()));
这将返回“en”或“de”或“fr”或您设备语言设置的任何内容。
我在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
如果要查看当前语言,请使用@Sarpe(@Thorbear)的答案:
String locale = getApplicationContext().getResources().getConfiguration().locale.getLanguage();
其他人已经为设备语言提供了很好的答案,
如果你希望应用程序语言最简单的方法是在你的val language = ConfigurationCompat.getLocales(Resources.getSystem().configuration)?.get(0)?.language
// Check here the language.
val format = if (language == "ru") "d MMMM yyyy г." else "d MMMM yyyy"
val longDateFormat = SimpleDateFormat(format, Locale.getDefault())
文件中添加一个app_lang
密钥,并为每个langs指定lang。
这样,如果您的应用的默认语言与设备语言不同,您可以选择将其作为服务的参数发送。
我的解决方案是这样的
strings.xml
然后
@SuppressWarnings("deprecation")
public String getCurrentLocale2() {
return Resources.getSystem().getConfiguration().locale.getLanguage();
}
@TargetApi(Build.VERSION_CODES.N)
public Locale getCurrentLocale() {
getResources();
return Resources.getSystem().getConfiguration().getLocales().get(0);
}
显示--->和
这是获取设备国家/地区的代码。兼容所有版本的android甚至oreo。
解决方案:如果用户没有SIM卡而不是国家,则在手机设置或当前语言选择期间使用SIM卡。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.e("Locale", getCurrentLocale().getLanguage());
} else {
Log.e("Locale", getCurrentLocale2().toString());
}
如果你想为那些说印地语的印度居住的用户做特定的任务,那么在条件下使用以下
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;
}
对我有用的是:
Resources.getSystem().getConfiguration().locale;
Resource.getSystem()返回一个全局共享的Resources对象,该对象仅提供对系统资源的访问(没有应用程序资源),并且没有为当前屏幕配置(不能使用维度单位,不会根据方向更改等)。
由于getConfiguration.locale
现已被弃用,因此在Android Nougat中获取主要语言环境的首选方法是:
Resources.getSystem().getConfiguration().getLocales().get(0);
为了保证与以前的Android版本的兼容性,可能的解决方案是一个简单的检查:
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = Resources.getSystem().getConfiguration().getLocales().get(0);
} else {
//noinspection deprecation
locale = Resources.getSystem().getConfiguration().locale;
}
更新
从支持库26.1.0
开始,您不需要检查Android版本,因为它提供了一种方便的方法向后兼容getLocales()
。
只需致电:
ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());
您可以从当前语言环境中“提取”语言。您可以通过标准Java API或使用Android Context来提取语言环境。例如,下面两行是等价的:
String locale = context.getResources().getConfiguration().locale.getDisplayName();
String locale = java.util.Locale.getDefault().getDisplayName();
为了节省其他时间和/或混淆,我想分享一下,我已经尝试了Johan Pelgrim提出的两个替代方案,在我的设备上它们是等效的 - 无论默认位置是否改变。
所以我的设备的默认设置是英语(United Kindom),并且在这种状态下如预期的那样,Johan的答案中的两个字符串给出了相同的结果。如果我然后更改手机设置中的语言环境(比如意大利语(意大利语))并重新运行,则Johan的答案中的两个字符串都将语言环境设置为italiano(Italia)。
因此,我认为约翰的原始帖子是正确的,格雷姆的评论是不正确的。
如Locale reference所述,获得语言的最佳方式是:
Locale.getDefault().getLanguage()
此方法根据ISO 639-1 standart返回具有语言ID的字符串
你可以用它
boolean isLang = Locale.getDefault().getLanguage().equals("xx");
当“xx”是任何语言代码,如“en”,“fr”,“sp”,“ar”....等等
context.getResources().getConfiguration().locale
Locale.getDefault()
是等价的,因为android.text.format.DateFormat
类可以互换使用,例如
private static String zeroPad(int inValue, int inMinDigits) {
return String.format(Locale.getDefault(), "%0" + inMinDigits + "d", inValue);
}
和
public static boolean is24HourFormat(Context context) {
String value = Settings.System.getString(context.getContentResolver(),
Settings.System.TIME_12_24);
if (value == null) {
Locale locale = context.getResources().getConfiguration().locale;
// ... snip the rest ...
}
您可以尝试从系统资源获取区域设置:
PackageManager packageManager = context.getPackageManager();
Resources resources = packageManager.getResourcesForApplication("android");
String language = resources.getConfiguration().locale.getLanguage();