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


当前回答

如果你想要获得你的设备所选择的语言,这可能会帮助你:

Locale.getDefault().getDisplayLanguage();

你可以使用Locale.getDefault().getLanguage();要获得常用的语言代码(例如:“德”、“en”)

其他回答

你可以尝试从系统资源中获取locale:

PackageManager packageManager = context.getPackageManager();
Resources resources = packageManager.getResourcesForApplication("android");
String language = resources.getConfiguration().locale.getLanguage();

获取设备语言的正确方法如下:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    return context.getResources().getConfiguration().getLocales().get(0);
} else {
    return context.getResources().getConfiguration().locale;
}

希望能有所帮助。

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

你可以用这个

boolean isLang = Locale.getDefault().getLanguage().equals("xx");

当“xx”是任何语言代码,如“en”,“fr”,“sp”,“ar”....等等