ChatGPT解决这个技术问题 Extra ChatGPT

Get the current language in device

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

The 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.
@VictorIonescu thank you for the comment. Sarpe's answer is correct answer to fetch device's locale. Please refer it. stackoverflow.com/a/28498119/3762067

M
Michał Sułek

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

Thanks, nice with a list of possibilities. Could you please add "Locale.getDefault().toString()", as suggested in a comment by "Tom".
I'm confused. Did you just copied the answer from DeRagan?
Sorry about that. It is true i added it in there. I should have left a note it came from here. I made the mistake trying to help people find the answer they need quicker. Because yours obviously is the better answer. But of course yours will never come to the top, because people don't see it as often as the top one. Is it okay if we add it in there with a note that it came from this answer?
Hello again. I don't prefer that solution. Currently DeRagan's answer is shown in first place and mine is shown as second. For a researcher, it shouldn't be so hard to check second answer in the question page. It would be better if @pinki changed accepted answer. But it seems like pinki last seen 3 months ago.
there is also Locale.getDefault().getDisplayLanguage() which returns "English"
S
SwiftiSwift

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


getDisplayLanguage() will localise the language. If you're interested in just getting the ISO code (e.g. for if or switch statements) use 'Locale.getDefault().getISO3Language();'
getISO3Language() returns things like "deu" for Deutschland (germany) instead of de ...
You can use Locale.getDefault().getLanguage(); to get the usual language code (e.g. "de", "en").
@DeRagan, This will not always gives you language for your device, but for your app only. For example, if I call 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?
I prefer Locale.getDefault().toString() which gives a string that fully identifies the locale, e.g. "en_US".
S
Sarpe

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

getConfiguration().locale is currently deprecated, use getConfiguration().getLocales().get(0) instead
This is the correct answer to this question. other answers get local of the app, not the device.
I don't know if this existed when the answer was written, but the support library has backwards compatible versions of this, there is no need to write backwards compatibility yourself. ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration())
As @Thorbear pointed out, now using the new support library method getLocales is the way to go. I've updated my answer.
This is right answer. Others return the language of application not of system.
J
Johan Pelgrim

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

this is not true. They are different. The first can change if the user switches the Locale. The second is the one that is pre-installed on the phone. It never changes no matter what the user does.
Comment by gregm might be false. See answer by airewyre.
@gregm's comment is wrong. Just try it out yourself and you will see, that Locale.getDefault() will change when you change the language in settings.
Although @gregm's comment is wrong, he still is right those two lines do not need to return the same locale. Just call Locale.setDefault() with some other locale beforehand and you get different results.
S
Suragch

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.


O
Opsenas

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


S
Simon K. Gerges

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


if (Locale.ENGLISH.equals(Locale.getDefault().getLanguage())) { ...}
@ClementMartino your suggestion is OK in case of Pre-defined Locales like ENGLISH, but for other locales like Arabic, we have to parse it using the lang code
M
M.Khouli

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


A
Arjun

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.


C
Community

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 ...
}

s
stefan.nsk

You can try to get locale from system resources:

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

This is THE answer in case you've overridden any part of the configuration
C
CoolMind

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

If you want to apply user's local date format, use DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault()).format(Date()).
G
Grafritz Design
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";
}

It would be better if you could add more instructions to the answer, rather than just pasting the code.
V
Vadim Kotov

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.


B
Baby Groot

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

V
Vadim Kotov

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

S
S.R
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()));

h
htafoya

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.


I can't believe there's no framework support for giving you the language the AssetManager is currently using. In fact, the String format helpers in Resources.java use the default locale[0] so if the string from the AssetManager comes from a locale that does not match the device's default locale (because your app does not support it but supports e.g. the next on the list) then there's a bug.
r
rink.attendant.6

There are two languages.

Default language of OS:

Locale.getDefault().getDisplayLanguage();

Current language of Application:

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

thats not true, if you change locale in your app even first give you changed value and not system value
S
Shamsul Arefin Sajib
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


a
andrea.rinaldi

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.


n
nbk

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 

S
Sandeep Dixit

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.


V
Vadim Kotov
if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
    // your code here
}

Please add some details about why this answers the question - just code snippet will not help readers to understand the solution
Locale.getDefault().getDisplayName() gives the current language of your device. and we are checking whether Device language is Hindi or not .. If current Device language is Hindi, then do what you want in the block.
This is hard coding. That is not how it should be handled. Whatever difference want to have across languages you need to do it via properties in the values-LL folders.
b
biyoalp

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


A
Abhishek Garg

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;
}

C
Caner

Basic Kotlin answer:

Locale.getDefault().language

According to my tests, you do not need checking version control.
D
Divyaadz

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 
}