LocaleUtils change application & system locale of resources on the fly
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
void changeApplicationLanguage(String code){ | |
updateContextLocale(mBaseContext, getLocale(code)); //change application language | |
updateResourceLocale(Resources.getSystem(),getLocale(code)); //change system resources | |
} |
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
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