How to get the device locale after changing the application locale
Asked Answered
G

4

9

I am changing the application locale based on user choice. Independent of device locale.

using

public void setDefaultLocale(Context context, String locale) {
        Locale appLoc = new Locale(locale);
        Locale.setDefault(appLoc);
        Configuration appConfig = new Configuration();
        appConfig.locale = appLoc;
        context.getResources().updateConfiguration(appConfig,
                context.getResources().getDisplayMetrics());
    }

But I want to know what will be the device locale also.

When I am trying to get this I always getting the locale which I have set to application.

ex: applictaion is in ENGLISH and device is in CHINESE. I am always getting english.

for getting locale using,

option 1.

String locale = context.getResources().getConfiguration().locale.getCountry();

option 2.

String local_country = ((Activity) context).getBaseContext().getResources().getConfiguration().locale.getCountry();

Any help will be highly appreciated!!!

Garman answered 18/1, 2013 at 8:6 Comment(1)
Similar: https://mcmap.net/q/129152/-android-get-device-localeMalocclusion
S
6

I am absolutely unsure how portable this is to different devices:

try {
    Process exec = Runtime.getRuntime().exec(new String[]{"getprop", "persist.sys.language"});
    String locale = new BufferedReader(new InputStreamReader(exec.getInputStream())).readLine();
    exec.destroy();
    Log.e("", "Device locale: "+locale);
} catch (IOException e) {
    e.printStackTrace();
}

And if you want the country part: persist.sys.country

Squilgee answered 18/1, 2013 at 11:23 Comment(1)
On Android Api(+21) instead of using persist.sys.language use persist.sys.localeOff
F
7

I asked something similar and found this answer, sorry if it is late:

To find the system locale use:

defaultLocale = Resources.getSystem().getConfiguration().locale;

It gets the system locale, no matter which default locale is set for the app/activity.

Flammable answered 14/10, 2013 at 21:0 Comment(0)
S
6

I am absolutely unsure how portable this is to different devices:

try {
    Process exec = Runtime.getRuntime().exec(new String[]{"getprop", "persist.sys.language"});
    String locale = new BufferedReader(new InputStreamReader(exec.getInputStream())).readLine();
    exec.destroy();
    Log.e("", "Device locale: "+locale);
} catch (IOException e) {
    e.printStackTrace();
}

And if you want the country part: persist.sys.country

Squilgee answered 18/1, 2013 at 11:23 Comment(1)
On Android Api(+21) instead of using persist.sys.language use persist.sys.localeOff
A
1

If you need to support below Android 7.0 - Nougat (API level 24) you can use something like this:

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
// ToDo: Adjust this method once the target API is 24 or higher!
private Locale getLocale()
{
  if(Build.VERSION.SDK_INT >= 24)
  {
    return Resources.getSystem().getConfiguration().getLocales().get(0);
  }

  else
  {
    return Resources.getSystem().getConfiguration().locale;
  }
}

Note the annotation regarding deprecation and NewApi. Adjust this method once you target API 24 or higher. Inspired by this answer.

Anneliese answered 21/10, 2020 at 7:1 Comment(0)
R
0

for those whom are using Daniel Fekete 's solution which is :

Process exec = Runtime.getRuntime().exec(new String[]{"getprop", "persist.sys.language"});
String locale = new BufferedReader(new InputStreamReader(exec.getInputStream())).readLine();
exec.destroy();

Be aware of that locale might be EMPTY on latest Android Api(+21)! instead of using persist.sys.language use persist.sys.locale

exec = Runtime.getRuntime().exec(new String[]{"getprop", "persist.sys.locale"});
locale = new BufferedReader(new InputStreamReader(exec.getInputStream())).readLine();
exec.destroy();
Revealment answered 21/2, 2016 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.