Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LocaleRule implements TestRule {
- private final Locale[] mLocales;
- private Locale mDeviceLocale;
- public LocaleRule(Locale... locales) {
- mLocales = locales;
- }
- @Override
- public Statement apply(final Statement base, Description description) {
- return new Statement() {
- @Override
- public void evaluate() throws Throwable {
- try {
- if (mLocales != null) {
- mDeviceLocale = Locale.getDefault();
- for (Locale locale : mLocales) {
- setLocale(locale);
- base.evaluate();
- }
- }
- } finally {
- if (mDeviceLocale != null) {
- setLocale(mDeviceLocale);
- }
- }
- }
- };
- }
- private void setLocale(Locale locale) {
- Resources resources = InstrumentationRegistry
- .getTargetContext()
- .getResources();
- Locale.setDefault(locale);
- Configuration config = resources.getConfiguration();
- config.setLocale(locale);
- DisplayMetrics displayMetrics = resources.getDisplayMetrics();
- resources.updateConfiguration(config, displayMetrics);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment