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.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);
|
|
|
|
|
2020-11-13 23:06:57 +00:00
|
|
|
currentLocale = TextUtils.isEmpty(lang) ? defaultLocale :
|
2021-01-02 02:54:32 +00:00
|
|
|
(lang.contains("_") ? new Locale(lang.split("_")[0], lang.split("_")[1]) : new Locale(lang));
|
2020-07-01 17:08:28 +00:00
|
|
|
Locale.setDefault(currentLocale);
|
|
|
|
|
|
|
|
final Resources res = baseContext.getResources();
|
|
|
|
final Configuration config = res.getConfiguration();
|
|
|
|
|
|
|
|
config.locale = currentLocale;
|
2021-01-02 02:54:32 +00:00
|
|
|
config.setLocale(currentLocale);
|
|
|
|
config.setLayoutDirection(currentLocale);
|
2020-07-01 17:08:28 +00:00
|
|
|
|
|
|
|
res.updateConfiguration(config, res.getDisplayMetrics());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Locale getCurrentLocale() {
|
|
|
|
return currentLocale;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateConfig(final ContextThemeWrapper wrapper) {
|
2021-01-02 02:54:32 +00:00
|
|
|
if (currentLocale != null) {
|
2020-07-01 17:08:28 +00:00
|
|
|
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-09-02 17:02:31 +00:00
|
|
|
final String appLanguageSettings = Utils.settingsHelper.getString(Constants.APP_LANGUAGE);
|
2020-09-10 11:44:25 +00:00
|
|
|
if (TextUtils.isEmpty(appLanguageSettings)) return null;
|
2020-07-01 17:08:28 +00:00
|
|
|
|
2020-09-02 17:02:31 +00:00
|
|
|
final int appLanguageIndex = Integer.parseInt(appLanguageSettings);
|
2020-07-01 17:08:28 +00:00
|
|
|
if (appLanguageIndex == 1) return "en";
|
|
|
|
if (appLanguageIndex == 2) return "fr";
|
|
|
|
if (appLanguageIndex == 3) return "es";
|
2020-11-13 23:06:57 +00:00
|
|
|
if (appLanguageIndex == 4) return "zh_CN";
|
2020-07-01 17:08:28 +00:00
|
|
|
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-09-16 00:18:23 +00:00
|
|
|
if (appLanguageIndex == 11) return "fa";
|
|
|
|
if (appLanguageIndex == 12) return "mk";
|
2020-09-26 16:36:45 +00:00
|
|
|
if (appLanguageIndex == 13) return "vi";
|
2020-11-13 23:06:57 +00:00
|
|
|
if (appLanguageIndex == 14) return "zh_TW";
|
2020-11-30 17:43:33 +00:00
|
|
|
if (appLanguageIndex == 15) return "ca";
|
|
|
|
if (appLanguageIndex == 16) return "ru";
|
2020-12-13 00:01:23 +00:00
|
|
|
if (appLanguageIndex == 17) return "hi";
|
|
|
|
if (appLanguageIndex == 18) return "nl";
|
2020-12-30 18:54:39 +00:00
|
|
|
if (appLanguageIndex == 19) return "sk";
|
2020-12-31 16:24:40 +00:00
|
|
|
if (appLanguageIndex == 20) return "ja";
|
2020-07-01 17:08:28 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|