[android] Get the current language in device

How can we get the current language selected in the Android device?

This question is related to android localization

The answer is


public class LocalUtils {

    private static final String LANGUAGE_CODE_ENGLISH = "en";


    // returns application language eg: en || fa ...
    public static String getAppLanguage() {
        return Locale.getDefault().getLanguage();
    }

    // returns device language eg: en || fa ...
    public static String getDeviceLanguage() {
        return ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0).getLanguage();
    }

    public static boolean isDeviceEnglish() {
        return getDeviceLanguage().equals(new Locale(LANGUAGE_CODE_ENGLISH).getLanguage());
    }

    public static boolean isAppEnglish() {
        return getAppLanguage().equals(new Locale(LANGUAGE_CODE_ENGLISH).getLanguage());
    }


}

Log.i("AppLanguage: ",     LocalUtils.getAppLanguage());
Log.i("DeviceLanguage: ",  LocalUtils.getDeviceLanguage());
Log.i("isDeviceEnglish: ", String.valueOf(LocalUtils.isDeviceEnglish()));
Log.i("isAppEnglish: ",    String.valueOf(LocalUtils.isAppEnglish()));

here is code to get device country. Compatible with all versions of android even oreo.

Solution: if user does not have sim card than get country he is used during phone setup , or current language selection.

public static String getDeviceCountry(Context context) {
    String deviceCountryCode = null;

    final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        if(tm != null) {
            deviceCountryCode = tm.getNetworkCountryIso();
        }

    if (deviceCountryCode != null && deviceCountryCode.length() <=3) {
        deviceCountryCode = deviceCountryCode.toUpperCase();
    }
    else {
        deviceCountryCode = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0).getCountry().toUpperCase();
    }

  //  Log.d("countryCode","  : " + deviceCountryCode );
    return deviceCountryCode;
}

To add to Johan Pelgrim's answer

context.getResources().getConfiguration().locale
Locale.getDefault()

are equivalent because android.text.format.DateFormat class uses both interchangeably, e.g.

private static String zeroPad(int inValue, int inMinDigits) {
    return String.format(Locale.getDefault(), "%0" + inMinDigits + "d", inValue);
}

and

public static boolean is24HourFormat(Context context) {
    String value = Settings.System.getString(context.getContentResolver(),
            Settings.System.TIME_12_24);

    if (value == null) {
        Locale locale = context.getResources().getConfiguration().locale;

    // ... snip the rest ...
}

You can use this

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

when "xx" is any language code like "en", "fr", "sp", "ar" .... and so on


My solution is like this

@SuppressWarnings("deprecation")
public String getCurrentLocale2() {
    return Resources.getSystem().getConfiguration().locale.getLanguage();
}

@TargetApi(Build.VERSION_CODES.N)
public Locale getCurrentLocale() {
    getResources();
    return Resources.getSystem().getConfiguration().getLocales().get(0);
}

and then

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Log.e("Locale", getCurrentLocale().getLanguage());
            } else {
                Log.e("Locale", getCurrentLocale2().toString());
            }

shown ---> en


public void GetDefaultLanguage( ) {
    try {
        String langue = Locale.getDefault().toString(); //        ---> en_US
        /*
        Log.i("TAG", Locale.getDefault().getLanguage() ); //       ---> en
        Log.i("TAG", Locale.getDefault().getISO3Language()  ); //  ---> eng
        Log.i("TAG", Locale.getDefault().getCountry()  ); //       ---> US
        Log.i("TAG", Locale.getDefault().getISO3Country()  ); //   ---> USA
        Log.i("TAG", Locale.getDefault().getDisplayCountry() ); // ---> United States
        Log.i("TAG", Locale.getDefault().getDisplayName() ); //    ---> English (United States)
        Log.i("TAG", Locale.getDefault().toString()   ); //        ---> en_US
        Log.i("TAG", Locale.getDefault().getDisplayLanguage() ); //---> English 
        */

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            langue = Locale.getDefault().toLanguageTag(); //     ---> en-US
            url_Api = getUrlMicrosoftLearn(langue);
            Log.i("TAG", url_Api );
            Log.i("TAG", langue );
        }else{
            langue = langue.replace("_","-"); //     ---> en-US
            url_Api = getUrlMicrosoftLearn(langue);
            Log.i("TAG", url_Api );
            Log.i("TAG", langue );
        }
    }catch (Exception ex) {
        Log.i("TAG", "Exception:GetDefaultLanguage()", ex);
    }
}

public String getUrlMicrosoftLearn(String langue) {
    return "https://docs.microsoft.com/"+langue+"/learn";
}

You can try to get locale from system resources:

PackageManager packageManager = context.getPackageManager();
Resources resources = packageManager.getResourcesForApplication("android");
String language = resources.getConfiguration().locale.getLanguage();

To save others time and/or confusion I wanted to share that I have tried the two alternatives proposed by Johan Pelgrim above and on my device they are equivalent - whether or not the default location is changed.

So my device's default setting is English(United Kindom) and in this state as expected both Strings in Johan's answer give the same result. If I then change the locale in the phone settings (say to italiano(Italia)) and re-run then both Strings in Johan's answer give the locale as italiano(Italia).

Therefore I believe Johan's original post to be correct and gregm's comment to be incorrect.


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

This solution worked for me. This will return you the android device's language (not the app's local language)

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

This will return "en" or "de" or "fr" or whatever your device language is set to.


If you want to do specific task for users resides in India who speaks Hindi then use below if condition

if(Locale.getDefault().getDisplayName().equals("?????? (????)")){
 //Block executed only for the users resides in India who speaks Hindi 
}

The correct way of getting the language of your device is the following:

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

Hope it helps.


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

If you want to check a current language, use the answer of @Sarpe (@Thorbear):

val language = ConfigurationCompat.getLocales(Resources.getSystem().configuration)?.get(0)?.language
// Check here the language.
val format = if (language == "ru") "d MMMM yyyy ?." else "d MMMM yyyy"
val longDateFormat = SimpleDateFormat(format, Locale.getDefault())

There are two languages.

Default language of OS:

Locale.getDefault().getDisplayLanguage();

Current language of Application:

getResources().getConfiguration().locale.getDisplayLanguage();//return string

if API level is 24 or above, use LocaleList.getDefault().get(0).getLanguage() else use Locale.getDefault.getLanguage()

private fun getSystemLocale() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    LocaleList.getDefault().get(0).language
} else {
    Locale.getDefault().language
}

reference: https://developer.android.com/guide/topics/resources/multilingual-support


Locale.getDefault().getDisplayLanguage()

will give you Written name for the Language, for example, English, Dutch, French

Locale.getDefault().getLanguage()

will give you language code, for instance: en, nl, fr

Both methods return String


Answers above don't distinguish between simple chinese and traditinal chinese. Locale.getDefault().toString() works which returns "zh_CN", "zh_TW", "en_US" and etc.

References to : https://developer.android.com/reference/java/util/Locale.html, ISO 639-1 is OLD.


if(Locale.getDefault().getDisplayName().equals("?????? (????)")){
    // your code here
}

As described in Locale reference the best way to get language is:

Locale.getDefault().getLanguage()

this method returns string with language id according to ISO 639-1 standart


You can use this code to find out keyboard current

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

If you choose a language you can't type this Greek may be helpful.

getDisplayLanguage().toString() = English
getLanguage().toString() = en 
getISO3Language().toString() = eng
getDisplayLanguage()) = English
getLanguage() = en
getISO3Language() = eng

Now try it with Greek

getDisplayLanguage().toString() = ????????
getLanguage().toString() = el
getISO3Language().toString() = ell
getDisplayLanguage()) = ????????
getLanguage() = el
getISO3Language() = ell

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

The others have given good answers for the device language,

if you wish the app language the easiest way to do it is by adding an app_lang key to your strings.xml file, and specify the lang for each of the langs as well.

That way, if your app's default language is different from the device language, you can chose to send that as parameter for your services.