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


当前回答

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


}

Log.i("AppLanguage: ",     LocalUtils.getAppLanguage());
Log.i("DeviceLanguage: ",  LocalUtils.getDeviceLanguage());
Log.i("isDeviceEnglish: ", String.valueOf(LocalUtils.isDeviceEnglish()));
Log.i("isAppEnglish: ",    String.valueOf(LocalUtils.isAppEnglish()));

其他回答

Locale.getDefault().getDisplayLanguage()

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

Locale.getDefault().getLanguage()

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

两种方法都返回String

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

如果你想检查当前语言,请使用@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())

我检查了我的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
Locale.getDefault().toLanguageTag()     ---> en-US

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

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String locale = ims.getLocale();