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


当前回答

如果API级别为24或以上,则使用LocaleList.getDefault().get(0).getLanguage() 否则使用Locale.getDefault.getLanguage()

private fun getSystemLocale() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    LocaleList.getDefault().get(0).language
} else {
    Locale.getDefault().language
}

参考:https://developer.android.com/guide/topics/resources/multilingual-support

其他回答

对我有用的是:

Resources.getSystem().getConfiguration().locale;

Resources. getsystem()返回一个全局共享资源对象,该对象仅提供对系统资源(不包括应用程序资源)的访问,并且没有为当前屏幕配置(不能使用维度单位,不会根据方向改变等等)。

因为getConfiguration。locale现在已经被弃用,在Android牛轧糖中获取主locale的首选方法是:

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());
public void GetDefaultLanguage( ) {
    try {
        String langue = Locale.getDefault().toString(); //        ---> en_US
        /*
        Log.i("TAG", Locale.getDefault().getLanguage() ); //       ---> en
        Log.i("TAG", Locale.getDefault().getISO3Language()  ); //  ---> eng
        Log.i("TAG", Locale.getDefault().getCountry()  ); //       ---> US
        Log.i("TAG", Locale.getDefault().getISO3Country()  ); //   ---> USA
        Log.i("TAG", Locale.getDefault().getDisplayCountry() ); // ---> United States
        Log.i("TAG", Locale.getDefault().getDisplayName() ); //    ---> English (United States)
        Log.i("TAG", Locale.getDefault().toString()   ); //        ---> en_US
        Log.i("TAG", Locale.getDefault().getDisplayLanguage() ); //---> English 
        */

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            langue = Locale.getDefault().toLanguageTag(); //     ---> en-US
            url_Api = getUrlMicrosoftLearn(langue);
            Log.i("TAG", url_Api );
            Log.i("TAG", langue );
        }else{
            langue = langue.replace("_","-"); //     ---> en-US
            url_Api = getUrlMicrosoftLearn(langue);
            Log.i("TAG", url_Api );
            Log.i("TAG", langue );
        }
    }catch (Exception ex) {
        Log.i("TAG", "Exception:GetDefaultLanguage()", ex);
    }
}

public String getUrlMicrosoftLearn(String langue) {
    return "https://learn.microsoft.com/"+langue+"/learn";
}

如果API级别为24或以上,则使用LocaleList.getDefault().get(0).getLanguage() 否则使用Locale.getDefault.getLanguage()

private fun getSystemLocale() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    LocaleList.getDefault().get(0).language
} else {
    Locale.getDefault().language
}

参考:https://developer.android.com/guide/topics/resources/multilingual-support

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

Locale.getDefault().getLanguage()

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

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

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