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


当前回答

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

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

其他回答

这个解决方案对我很有效。这将返回android设备的语言(不是应用程序的本地语言)

String locale = getApplicationContext().getResources().getConfiguration().locale.getLanguage();

这将返回"en"或"de"或"fr"或任何你的设备语言设置。

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

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

科特林:

// 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 

你可以用这个

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

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

我检查了我的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

Locale.getDefault().getLanguage()是虚拟机语言

Locale.getDefault().getLanguage()

它是当前运行应用程序的虚拟机实例的语言。它由java类(如DateFormat等)使用。如果您使用某些java类,则在更改应用程序区域设置时可能需要修改此选项。如果你在改变你的App语言环境时修改了这个,它与android的语言是不一样的。

context.getConfiguration().locale.getLanguage()是活动语言

context.getConfiguration().locale.getLanguage()

这是您活动中设置的语言。 在最新的SDK版本中,以下是可取的

context.getConfiguration().getLocales().get(0).getLanguage()

Resources.getSystem().getConfiguration().getLocales()给出用户在系统级添加的所有地区

这将为您提供用户在系统级别设置的第一个区域设置。

Resources.getSystem().getConfiguration().getLocales().get(0).getLanguage()

大量的用户是多语言的,所以你可能想要循环使用locale。