Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 551
Aleksejs Mjaliks Avatar asked Dec 31 '11 20:12

Aleksejs Mjaliks


People also ask

How can I get current locale language in Android?

You can use Locale. getDefault(). getLanguage(); to get the usual language code (e.g. "de", "en").

How do I get current app language?

This example demonstrate about how to Get the current language in Android device. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I change the default locale in Android?

Configuration config = new Configuration(); config. locale = selectedLocale; // set accordingly // eg. if Hindi then selectedLocale = new Locale("hi"); Locale. setDefault(selectedLocale); // has no effect Resources res = getApplicationContext().


2 Answers

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.

like image 93
Aleksejs Mjaliks Avatar answered Sep 27 '22 23:09

Aleksejs Mjaliks


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

like image 43
sotcha Avatar answered Sep 27 '22 23:09

sotcha