RetroMusicPlayer/app/src/main/java/code/name/monkey/retromusic/LanguageContextWrapper.java

39 lines
1.1 KiB
Java
Raw Normal View History

2020-04-24 23:41:14 +05:30
package code.name.monkey.retromusic;
import android.content.Context;
2020-06-07 00:27:28 +05:30
import android.content.ContextWrapper;
2020-04-24 23:41:14 +05:30
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.LocaleList;
import com.google.android.gms.common.annotation.KeepName;
2020-10-06 14:16:04 +05:30
import java.util.Locale;
2020-04-24 23:41:14 +05:30
import code.name.monkey.appthemehelper.util.VersionUtils;
2020-06-07 00:27:28 +05:30
public class LanguageContextWrapper extends ContextWrapper {
2020-04-24 23:41:14 +05:30
2020-10-06 14:16:04 +05:30
public LanguageContextWrapper(Context base) {
super(base);
}
2020-04-24 23:41:14 +05:30
@KeepName
2020-10-06 14:16:04 +05:30
public static LanguageContextWrapper wrap(Context context, Locale newLocale) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
2020-04-24 23:41:14 +05:30
2020-10-06 14:16:04 +05:30
if (VersionUtils.INSTANCE.hasNougatMR()) {
configuration.setLocale(newLocale);
2020-04-24 23:41:14 +05:30
2020-10-06 14:16:04 +05:30
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
} else {
configuration.setLocale(newLocale);
2020-04-24 23:41:14 +05:30
}
context = context.createConfigurationContext(configuration);
2020-10-06 14:16:04 +05:30
return new LanguageContextWrapper(context);
}
}