Created
February 12, 2015 10:01
-
-
Save robUx4/96b5a39446efbd2667a4 to your computer and use it in GitHub Desktop.
Force Android user language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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