How to get language (locale) currently Android app displays?

36,534

Solution 1

My own solution is to add to strings.xml key-value pair locale=<locale code>, thus context.getResources().getString(R.string.locale) will return locale code specific for used locale.

Solution 2

It's on your app configuration, so you can get it with :

getResources().getConfiguration().locale

this is different from

Locale.getDefault()

and shows the Locale that the app uses which can be different.

It can be different because the developer can change it by updating the app configuration, check : Resources.updateConfiguration

Solution 3

The following code works for me:

@TargetApi(Build.VERSION_CODES.M)
private String getCurrentDefaultLocaleStr(Context context) {
    Locale locale = context.getResources().getConfiguration().locale;
    return locale.getDefault().toString();
}

Note: There is a slight change on how you should get locale on Build.VERSION_CODES.N. For N and above, the code should look like the code block below; however, I just do it for M and below like in the above code block and it is compatible for N and above also.

    // Build.VERSION_CODES.N
    Locale locale = context.getResources().getConfiguration().getLocales().get(0);

I got the following on the national languages I support:

English:             en 
Traditional Chinese: zh_TW_#Hant 
Simplified Chinese:  zh_CN_#Hans
Spanish:             es_ES
French:              fr_FR
Japanese:            ja_JP
German:              de_DE

There is a list of methods that give the same locale information in different formats:

locale.getDefault().getCountry();
locale.getDefault().getISO3Language();
locale.getDefault().getISO3Country();
locale.getDefault().getDisplayCountry();
locale.getDefault().getDisplayName();
locale.getDefault().getDisplayLanguage();
locale.getDefault().getLanguage();
locale.getDefault().toString();

Solution 4

In Kotlin, you can add the following extension function to your project:

fun Context.getCurrentLocale(): Locale {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
        this.resources.configuration.locales.get(0);
    } else{
        this.resources.configuration.locale;
    }
}

And use it without any warnings:

val locale = applicationContext.getCurrentLocale()

Solution 5

You can use the code below. For example, the functions presented below may be placed inside the class extended the Application class.

public class MyApplication extends Application {
    ...
    public static String APP_LANG;
    private Context ctx = getBaseContext();
    private Resources res = ctx.getResources();
    public SharedPreferences settingPrefs;
    ...

    public void restoreAppLanguage(){
    /**
    *Use this method to store the app language with other preferences.
    *This makes it possible to use the language set before, at any time, whenever 
    *the app will started.
    */
        settingPrefs = getSharedPreferences("ConfigData", MODE_PRIVATE);
        String lang = settingPrefs.getString("AppLanguage", "");
        if(!settingPrefs.getAll().isEmpty() && lang.length() == 2){
            Locale myLocale;
            myLocale = new Locale(lang);
            Locale.setDefault(myLocale);
            Configuration config = new Configuration();
            config.locale = myLocale;
            res.updateConfiguration( config, res.getDisplayMetrics() );
        }
    }

    public void storeAppLanguage(int lang) {
    /**
    *Store app language on demand
    */
        settingPrefs = getSharedPreferences("ConfigData", MODE_PRIVATE);
        Editor ed = settingPrefs.edit();

        Locale myLocale;
        myLocale = new Locale(lang);
        Locale.setDefault(myLocale);
        Configuration config = new Configuration();
        config.locale = myLocale;
        res.updateConfiguration( config, res.getDisplayMetrics() );
        ed.putString("AppLanguage", lang);

        ed.commit();
    }

    public void setAppLanguage(String lang){
    /**
    *Use this method together with getAppLanguage() to set and then restore
    *language, whereever you need, for example, the specifically localized
    *resources.
    */
        Locale myLocale;
        myLocale = new Locale(lang);
        Locale.setDefault(myLocale);
        Configuration config = new Configuration();
        config.locale = myLocale;
        res.updateConfiguration( config, res.getDisplayMetrics() );
    }

    public void getAppLanguage(){
    /**
    *Use this method to obtain the current app language name
    */
        settingPrefs = getSharedPreferences("ConfigData",MODE_PRIVATE);
        String lang = settingPrefs.getString("AppLanguage", "");
        if(!settingPrefs.getAll().isEmpty() && lang.length() == 2){
            APP_LANG = lang;
        }
        else APP_LANG = Locale.getDefault().getLanguage();
    }
}

And then wherever in the main code we can write:

public class MainActivity extends ActionBarActivity {
    ...
    MyApplication app;
    ... 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        app = (MyApplication)getApplicationContext();
        ...
        if(settingPrefs.getAll().isEmpty()){
            //Create language preference if it is not
            app.storeAppLanguage(Locale.getDefault().getLanguage());
        }

        //Restore previously used language
        app.restoreAppLanguage();
        ...
    }
    ...
    void SomethingToDo(){
        String lang = "en"; //"fr", "de", "es", "it", "ru" etc.
        ...
        app.getAppLanguage();
        app.setAppLanguage(lang);
        ...
        //do anything
        ...
        app.setAppLanguage(app.APP_LANG);
    }
    ...
}

In your case, you, shortly, may use getAppLanguage() and then check the public variable APP_LANG to obtain what language is currently used.

Share:
36,534
Aleksejs Mjaliks
Author by

Aleksejs Mjaliks

Sometimes I code. Sometimes I tweet. Sometime I write.

Updated on August 21, 2021

Comments

  • Aleksejs Mjaliks
    Aleksejs Mjaliks almost 3 years

    How to get know language (locale) currently Android app uses to display texts to user?

    I know I can use Locale.getDefault() to get default OS locale. But it may differ from locale used by app to display text and other resources, if this locale isn't supported by app.


    I need to determine language (locale) displayed by the app, thus the app can pass language to the server, so it can localise returned results.