Get the current language in device
Solution 1:
I've checked the Locale methods on my Android 4.1.2 device, and the results:
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
Solution 2:
If you want to get the selected language of your device, this might help you:
Locale.getDefault().getDisplayLanguage();
You can use Locale.getDefault().getLanguage();
to get the usual language code (e.g. "de", "en")
Solution 3:
What worked for me was:
Resources.getSystem().getConfiguration().locale;
Resources.getSystem()
returns a global shared Resources object that provides access to only system resources (no application resources), and is not configured for the current screen (can not use dimension units, does not change based on orientation, etc).
Because getConfiguration.locale
has now been deprecated, the preferred way to get the primary locale in Android Nougat is:
Resources.getSystem().getConfiguration().getLocales().get(0);
To guarantee compatibility with the previous Android versions a possible solution would be a simple check:
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = Resources.getSystem().getConfiguration().getLocales().get(0);
} else {
//noinspection deprecation
locale = Resources.getSystem().getConfiguration().locale;
}
Update
Starting with support library 26.1.0
you don't need to check the Android version as it offers a convenient method backward compatible getLocales()
.
Simply call:
ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());
Solution 4:
You can 'extract' the language from the current locale. You can extract the locale via the standard Java API, or by using the Android Context. For instance, the two lines below are equivalent:
String locale = context.getResources().getConfiguration().locale.getDisplayName();
String locale = java.util.Locale.getDefault().getDisplayName();