如何在Android设备中选择当前语言?


当前回答

如Locale引用中所述,获取语言的最佳方法是:

Locale.getDefault().getLanguage()

该方法根据ISO 639-1标准返回带有语言id的字符串

其他回答

我的解是这样的

@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);
}

然后

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Log.e("Locale", getCurrentLocale().getLanguage());
            } else {
                Log.e("Locale", getCurrentLocale2().toString());
            }

显示——>和

if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
    // your code here
}

如果你使用喷气背包撰写

在可组合功能方面

Context.applicationContext.resources.configuration.locales.get(0).language 
Locale.getDefault().getDisplayLanguage()

会给你语言的书面名称,例如,英语,荷兰语,法语

Locale.getDefault().getLanguage()

会给你语言代码,例如:en, nl, fr

两种方法都返回String

如果你想检查当前语言,请使用@Sarpe (@Thorbear)的答案:

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())