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


当前回答

请注意,这里的大多数答案提供了应用程序的语言。在某些情况下,这个应用程序可以拥有/设置与设备不同的语言。

获取实际的设备语言(是的,如果用户在设置中添加了多种语言,它将返回所有的语言!)

科特林:

// Will return something like ["en_US", "de_DE"]
val deviceLanguages: LocaleListCompat = ConfigurationCompat.getLocales(Resources.getSystem().configuration)
// Will return the actual language in use, like "en" or "de". The first language in the above code will be the default language
val currentActiveDeviceLanguage = languages.get(0).language 

其他回答

以上答案没有区分简体中文和繁体中文。 Locale.getDefault(). tostring()工作,返回“zh_CN”,“zh_TW”,“en_US”等。

参考:https://developer.android.com/reference/java/util/Locale.html, ISO 639-1是旧的。

再加上约翰·佩尔格里姆的回答

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

and

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 ...
}

您可以使用此代码找出键盘电流

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String locale = ims.getLocale();
if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
    // your code here
}

基本的Kotlin答案:

Locale.getDefault().language