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


当前回答

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

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

其他回答

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

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

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

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

希望能有所帮助。

基本的Kotlin答案:

Locale.getDefault().language

如果你想为居住在印度说印地语的用户做特定的任务,那么使用下面的If条件

if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
 //Block executed only for the users resides in India who speaks Hindi 
}

其他人对设备语言给出了很好的答案,

如果您希望使用应用程序语言,最简单的方法是在strings.xml文件中添加一个app_lang键,并为每个语言指定lang。

这样,如果你的应用程序的默认语言与设备语言不同,你可以选择将其作为服务的参数发送。