How can we get the current language selected in the Android device?
- 17The majority of answers here get the language of the application. Given that you can set the default locale of the application in code, the correct answer is the answer given by Sarpe - that gives you the device's locale.Victor Ionescu– Victor Ionescu2015-04-17 08:59:33 +00:00Commented Apr 17, 2015 at 8:59
- @VictorIonescu thank you for the comment. Sarpe's answer is correct answer to fetch device's locale. Please refer it. stackoverflow.com/a/28498119/3762067Varad Mondkar– Varad Mondkar2018-04-04 21:27:23 +00:00Commented Apr 4, 2018 at 21:27
- Does this answer your question? How to identify device language from the App language in Android 13?k4dima– k4dima2023-03-31 18:34:23 +00:00Commented Mar 31, 2023 at 18:34
33 Answers
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 3 Comments
Locale.getDefault().getDisplayLanguage() which returns "English"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")
18 Comments
Locale.getDefault().getLanguage(); to get the usual language code (e.g. "de", "en").Locale.setDefault("ru"), and language in system settings is set to English, then method Locale.getDefault().getLanguage() will return "ru", but not "en". Is there another method of getting real SYSTEM locale/language? I found not documented solution here, but is there more elegant solution?Locale.getDefault().toString() which gives a string that fully identifies the locale, e.g. "en_US".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()); 6 Comments
ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration())getLocales is the way to go. I've updated my answer.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(); 8 Comments
Locale.setDefault() with some other locale beforehand and you get different results.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.
Comments
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
Comments
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
2 Comments
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
Comments
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 ... } Comments
You can try to get locale from system resources:
PackageManager packageManager = context.getPackageManager(); Resources resources = packageManager.getResourcesForApplication("android"); String language = resources.getConfiguration().locale.getLanguage(); 1 Comment
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()) 1 Comment
DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault()).format(Date()).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://learn.microsoft.com/"+langue+"/learn"; } 1 Comment
Please be careful, most of the answers here provides the language of the application. There can be cases where this application can have/set a different language than the device.
To get the actual device languages (Yes, if the user have multiple languages added in the settings, it will return all of it!)
Kotlin:
// 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 Comments
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.
Comments
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.
1 Comment
Basic Kotlin answer:
Locale.getDefault().language 2 Comments
Locale.getDefault().getLanguage() is VM language
Locale.getDefault().getLanguage() It is the language of the current VM instance that is running your app. It is consumed by java classes such as DateFormat etc. You may need to modify this when changing app locale if you use certain java classes. If you have modified this during changing your App locale it is not the same as the language of android.
context.getConfiguration().locale.getLanguage() is Activity language
context.getConfiguration().locale.getLanguage() This is the language set at your activity. In the latest SDK versions following is preferable
context.getConfiguration().getLocales().get(0).getLanguage() Resources.getSystem().getConfiguration().getLocales() gives all locales that user has added at system level
This will give you the first locale that user has set at the system level.
Resources.getSystem().getConfiguration().getLocales().get(0).getLanguage() A great number of users are multilingual so you may want to loop through Locales.
Comments
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 Comments
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())); Comments
There are two languages.
Default language of OS:
Locale.getDefault().getDisplayLanguage(); Current language of Application:
getResources().getConfiguration().locale.getDisplayLanguage();//return string 1 Comment
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
Comments
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; } Comments
you can use .setLanguage(Locale.forLanguageTag(Locale.getDefault().getLanguage())); it is good