Skip to content

Instantly share code, notes, and snippets.

@robUx4
Created February 12, 2015 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robUx4/96b5a39446efbd2667a4 to your computer and use it in GitHub Desktop.
Save robUx4/96b5a39446efbd2667a4 to your computer and use it in GitHub Desktop.
Force Android user language
public static void setUILanguage(Context context, String uiLang) {
if (!TextUtils.isEmpty(uiLang) && context!=null) {
int sep = uiLang.indexOf("-r");
final Locale setLocale;
if (sep==-1)
setLocale = new Locale(uiLang);
else
setLocale = new Locale(uiLang.substring(0,sep), uiLang.substring(sep+2));
final Resources res = context.getResources();
// locale might be null, see http://crashes.to/s/b2264e30371
if (null!=res.getConfiguration() && (null==res.getConfiguration().locale || !res.getConfiguration().locale.equals(setLocale))) {
//TouiteurLog.i(false, "configuring resources "+res+" language:"+config2.locale+" from "+res.getConfiguration().locale);
Configuration configUpdate = new Configuration(res.getConfiguration());
configUpdate.locale = setLocale;
try {
Locale.setDefault(setLocale);
res.updateConfiguration(configUpdate, null);
Resources.getSystem().updateConfiguration(configUpdate, null);
} catch (Throwable e) {
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment