Reputation: 295
How can I get a list of the languages which the user has added to their device? In the example below, the user has gone to device settings and added two languages. How can I programmatically get this list?
Thank you!
Upvotes: 5
Views: 3865
Reputation: 85
Kotlin version. This is almost the same as suggested by (Shubham Agarwal Bhewanewala). Only with sorting and first capital letter.
val language = ArrayList<String>()
val llc = ConfigurationCompat.getLocales(Resources.getSystem().configuration)
for (i in 0 until llc.size()) {
language.add(llc[i].displayLanguage.substring(0,1).uppercase(Locale.getDefault()) + llc[i].displayLanguage.substring(1)
}
val set: Set<String> = HashSet(language)
language.clear()
language.addAll(set)
language.sort()
It will turn out like this: English French Russian Ukrainian and so on
Upvotes: 0
Reputation: 1119
LocaleListCompat llc = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());
for (int i=0; i<llc.size(); i++){
System.out.println(llc.get(i).getDisplayLanguage());
}
This has backward compatiblity with versions. And llc
will have all selected language by the user.
If you want full list of supported languages you will get it using Locale
for (Locale locale : Locale.getAvailableLocales()) {
System.out.println("HELLO " + locale.getDisplayLanguage());
}
Upvotes: 7
Reputation: 2049
For languages selected by user -
private fun getSystemLocale(): String {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return LocaleList.getDefault().get(0).getLanguage();
} else {
return Locale.getDefault.getLanguage();
}
}
If you want to get the selected language of your device with below options:
Locale.getDefault().getDisplayLanguage();
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
Upvotes: 4
Reputation: 20167
On Android 7.0 (API level 24) and above, you should call LocaleList.getDefault()
. On earlier versions of Android, there was only an option to pick one language, which you can get via Locale.getDefault()
.
Upvotes: 6
Reputation: 433
Here is the API to get all the available locales of your device.
public static Locale[] getAvailableLocales () For more information please check this public link : http://developer.android.com/reference/java/util/Locale.html#getAvailableLocales()
Upvotes: -1