2020-07-01 17:08:28 +00:00
|
|
|
package awais.instagrabber.utils;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.view.ContextThemeWrapper;
|
|
|
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
// taken from my app TESV Console Codes
|
|
|
|
public final class LocaleUtils {
|
|
|
|
private static Locale defaultLocale, currentLocale;
|
|
|
|
|
|
|
|
public static void setLocale(Context baseContext) {
|
|
|
|
if (defaultLocale == null) defaultLocale = Locale.getDefault();
|
|
|
|
|
|
|
|
if (baseContext instanceof ContextThemeWrapper)
|
|
|
|
baseContext = ((ContextThemeWrapper) baseContext).getBaseContext();
|
|
|
|
|
|
|
|
final String lang = LocaleUtils.getCorrespondingLanguageCode(baseContext);
|
|
|
|
|
|
|
|
currentLocale = Utils.isEmpty(lang) ? defaultLocale : new Locale(lang);
|
|
|
|
Locale.setDefault(currentLocale);
|
|
|
|
|
|
|
|
final Resources res = baseContext.getResources();
|
|
|
|
final Configuration config = res.getConfiguration();
|
|
|
|
|
|
|
|
config.locale = currentLocale;
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
|
|
|
config.setLocale(currentLocale);
|
|
|
|
config.setLayoutDirection(currentLocale);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.updateConfiguration(config, res.getDisplayMetrics());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Locale getCurrentLocale() {
|
|
|
|
return currentLocale;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateConfig(final ContextThemeWrapper wrapper) {
|
|
|
|
if (currentLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
|
|
|
final Configuration configuration = new Configuration();
|
|
|
|
configuration.locale = currentLocale;
|
|
|
|
configuration.setLocale(currentLocale);
|
|
|
|
wrapper.applyOverrideConfiguration(configuration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
private static String getCorrespondingLanguageCode(final Context baseContext) {
|
|
|
|
if (Utils.settingsHelper == null)
|
|
|
|
Utils.settingsHelper = new SettingsHelper(baseContext);
|
|
|
|
|
2020-08-30 06:47:04 +00:00
|
|
|
final int appLanguageIndex = Integer.parseInt(Utils.settingsHelper.getString(Constants.APP_LANGUAGE));
|
2020-07-01 17:08:28 +00:00
|
|
|
|
|
|
|
if (appLanguageIndex == 1) return "en";
|
|
|
|
if (appLanguageIndex == 2) return "fr";
|
|
|
|
if (appLanguageIndex == 3) return "es";
|
|
|
|
if (appLanguageIndex == 4) return "zh";
|
|
|
|
if (appLanguageIndex == 5) return "in";
|
|
|
|
if (appLanguageIndex == 6) return "it";
|
2020-07-27 15:29:13 +00:00
|
|
|
if (appLanguageIndex == 7) return "de";
|
|
|
|
if (appLanguageIndex == 8) return "pl";
|
2020-08-04 21:59:36 +00:00
|
|
|
if (appLanguageIndex == 9) return "tr";
|
2020-08-12 18:38:07 +00:00
|
|
|
if (appLanguageIndex == 10) return "pt";
|
2020-07-01 17:08:28 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|