Get the current language in device
AndroidLocalizationAndroid Problem Overview
How can we get the current language selected in the Android device?
Android Solutions
Solution 1 - Android
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 - Android
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 - Android
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 - Android
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();
Solution 5 - Android
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.
Solution 6 - Android
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
Solution 7 - Android
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
Solution 8 - Android
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
Solution 9 - Android
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.
Solution 10 - Android
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 ...
}
Solution 11 - Android
You can try to get locale from system resources:
PackageManager packageManager = context.getPackageManager();
Resources resources = packageManager.getResourcesForApplication("android");
String language = resources.getConfiguration().locale.getLanguage();
Solution 12 - Android
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())
Solution 13 - Android
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.
Solution 14 - Android
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";
}
Solution 15 - Android
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();
Solution 16 - Android
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
Solution 17 - Android
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()));
Solution 18 - Android
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.
Solution 19 - Android
There are two languages.
Default language of OS:
Locale.getDefault().getDisplayLanguage();
Current language of Application:
getResources().getConfiguration().locale.getDisplayLanguage();//return string
Solution 20 - Android
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
Solution 21 - Android
if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
// your code here
}
Solution 22 - Android
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.
Solution 23 - Android
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
Solution 24 - Android
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
Solution 25 - Android
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;
}
Solution 26 - Android
Basic Kotlin answer:
Locale.getDefault().language
Solution 27 - Android
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
}