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.
It's on your app configuration, so you can get it with :
this is different from
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
You can use the code below. For example, the functions presented below may be placed inside the class extended the Application class.
And then wherever in the main code we can write:
In your case, you, shortly, may use
getAppLanguage()
and then check the public variableAPP_LANG
to obtain what language is currently used.My own solution is to add to
strings.xml
key-value pairlocale=<locale code>
, thuscontext.getResources().getString(R.string.locale)
will return locale code specific for used locale.The following code works for me:
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.
I got the following on the national languages I support:
There is a list of methods that give the same locale information in different formats:
Use this method.