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.
You can use Locale. getDefault(). getLanguage(); to get the usual language code (e.g. "de", "en").
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.
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().
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With