Get the current language in device Get the current language in device android android

Get the current language in device


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_USLocale.getDefault().getDisplayLanguage()---> EnglishLocale.getDefault().toLanguageTag()     ---> en-US


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")


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());