Skip to content

Instantly share code, notes, and snippets.

@ozodrukh
Last active August 29, 2015 14:18
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 ozodrukh/c2d5eadd09793869850a to your computer and use it in GitHub Desktop.
Save ozodrukh/c2d5eadd09793869850a to your computer and use it in GitHub Desktop.
LocaleUtils change application & system locale of resources on the fly
void changeApplicationLanguage(String code){
updateContextLocale(mBaseContext, getLocale(code)); //change application language
updateResourceLocale(Resources.getSystem(),getLocale(code)); //change system resources
}
package uz.uza.news.utils;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import java.util.Locale;
public final class LocaleUtils {
public static Configuration updateContextLocale(Context context, Locale locale){
final Resources resources = context.getResources();
return updateResourceLocale(resources, locale, resources.getConfiguration());
}
public static Configuration updateResourceLocale(Resources resources, Locale locale){
return updateResourceLocale(resources, locale, resources.getConfiguration());
}
public static Configuration updateResourceLocale(Resources resources, Locale locale,
Configuration configuration){
if(locale == null){
throw new IllegalArgumentException("Application language cannot be null");
}
Locale.setDefault(locale);
final Configuration newConfiguration = new Configuration(configuration);
newConfiguration.locale = locale;
// if api allows update layout direction let's do it
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
newConfiguration.setLayoutDirection(newConfiguration.locale);
}
resources.updateConfiguration(newConfiguration, resources.getDisplayMetrics());
return newConfiguration;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment