Upon installation of my Android program I check for device locale:
String deviceLocale=Locale.getDefault().getLanguage();
If deviceLocale i
As you are changing the Application language programatically like
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
pContext.getResources().updateConfiguration(config, null);
And
But after that, in other methods, when I check device's locale like this:
String deviceLocale=Locale.getDefault().getLanguage();
I get result as "English". But device's locale is Spanish.
So instead of that use this below code to get the device's Local
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();
}
OUTPUT: Spanish
Check this
Hope this exactly you are looking for.
The best way to design the app is to use a default lang, English in your case, under values/ and you can add addition langs under values-XX/. In this way when a language is not supported Android fallback to your default, so English. Let the OS does the work for you :) However, yes if you change the locale you'll get your last settings, so you have to save that information somewhere.
Although, the answer by @user2944616 is politically correct, that does not give an answer to the actual question posed.
So, if I really had to know the user set system locale, I'd use reflection (do not know any better option ATM):
private String getSystemPropertyValue(String name) {
try {
Class<?> systemProperties = Class.forName("android.os.SystemProperties");
try {
Method get = systemProperties.getMethod("get", String.class, String.class);
if (get == null) {
return "Failure!?";
}
try {
return (String)get.invoke(systemProperties, name, "");
} catch (IllegalAccessException e) {
return "IllegalAccessException";
} catch (IllegalArgumentException e) {
return "IllegalArgumentException";
} catch (InvocationTargetException e) {
return "InvocationTargetException";
}
} catch (NoSuchMethodException e) {
return "SystemProperties.get(String key, String def) method is not found";
}
} catch (ClassNotFoundException e) {
return "SystemProperties class is not found";
}
}
With that given and device locale set to Spanish (United States) / Español (Estados Unidos) following string
<string name="system_locale">System locale: <xliff:g id="profile_name">%1$s</xliff:g>_<xliff:g id="device_name">%2$s</xliff:g></string>
and this code
private static final String SYS_COUNTRY = "persist.sys.country";
private static final String SYS_LANG = "persist.sys.language";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView)findViewById(R.id.locale);
tv.setText(getString(R.string.system_locale, getSystemPropertyValue(SYS_LANG),
getSystemPropertyValue(SYS_COUNTRY)));
}
yields following screen:
If desired, one could also easily construct Locale
instance using the above:
Locale locale = new Locale(getSystemPropertyValue(SYS_LANG),
getSystemPropertyValue(SYS_COUNTRY));
Log.d(TAG, "Language: " + locale.getLanguage());
Log.d(TAG, "Country: " + locale.getCountry());
Log output:
D/GetSystemLocale(5697): Language: es
D/GetSystemLocale(5697): Country: US
Hope this helps.
To get the device locale use this
Locale current = context.getResources().getConfiguration().locale;
and current.toString()
will return you "en" "ar" etc
you should use String deviceLocale= Locale.getDefault().getDisplayLanguage();
to display the language instead of
String deviceLocale=Locale.getDefault().getLanguage();
check out this answer it may help you if above does not. Clickable
You can access global locale by -
defaultLocale = Resources.getSystem().getConfiguration().locale;
Take a look at http://developer.android.com/reference/android/content/res/Resources.html#getSystem() -
Returns a global shared Resources object that provides access to only system resources (no application resources)
Update: As pointed out in comments 'locale' field is deprecated and you need to use getLocales() instead.
defaultLocale = Resources.getSystem().getConfiguration().getLocales().get(0);