Skip to content
This repository has been archived by the owner on Jul 22, 2021. It is now read-only.

Commit

Permalink
Settings: Re-designed language switch
Browse files Browse the repository at this point in the history
Signed-off-by: sunilpaulmathew <sunil.kde@gmail.com>
  • Loading branch information
sunilpaulmathew committed May 31, 2021
1 parent 873fb45 commit aa0a818
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 109 deletions.
137 changes: 57 additions & 80 deletions app/src/main/java/com/smartpack/scriptmanager/utils/AppSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,98 +66,68 @@ public static void setLanguage(Context context) {
R.array.app_language), (dialogInterface, i) -> {
switch (i) {
case 0:
if (!Utils.languageDefault(context)) {
Utils.saveBoolean("use_en", false, context);
Utils.saveBoolean("use_ko", false, context);
Utils.saveBoolean("use_in", false, context);
Utils.saveBoolean("use_am", false, context);
Utils.saveBoolean("use_el", false, context);
Utils.saveBoolean("use_pt", false, context);
Utils.saveBoolean("use_ru", false, context);
if (!Utils.getLanguage(context).equals(java.util.Locale.getDefault().getLanguage())) {
Utils.saveString("appLanguage", java.util.Locale.getDefault().getLanguage(), context);
Utils.restartApp(context);
}
break;
case 1:
if (!Utils.getBoolean("use_en", false, context)) {
Utils.saveBoolean("use_en", true, context);
Utils.saveBoolean("use_ko", false, context);
Utils.saveBoolean("use_in", false, context);
Utils.saveBoolean("use_am", false, context);
Utils.saveBoolean("use_el", false, context);
Utils.saveBoolean("use_pt", false, context);
Utils.saveBoolean("use_ru", false, context);
if (!Utils.getLanguage(context).equals("en_US")) {
Utils.saveString("appLanguage", "en_US", context);
Utils.restartApp(context);
}
break;
case 2:
if (!Utils.getBoolean("use_ko", false, context)) {
Utils.saveBoolean("use_en", false, context);
Utils.saveBoolean("use_ko", true, context);
Utils.saveBoolean("use_in", false, context);
Utils.saveBoolean("use_am", false, context);
Utils.saveBoolean("use_el", false, context);
Utils.saveBoolean("use_pt", false, context);
Utils.saveBoolean("use_ru", false, context);
if (!Utils.getLanguage(context).equals("ko")) {
Utils.saveString("appLanguage", "ko", context);
Utils.restartApp(context);
}
break;
case 3:
if (!Utils.getBoolean("use_am", false, context)) {
Utils.saveBoolean("use_en", false, context);
Utils.saveBoolean("use_ko", false, context);
Utils.saveBoolean("use_in", false, context);
Utils.saveBoolean("use_am", true, context);
Utils.saveBoolean("use_el", false, context);
Utils.saveBoolean("use_pt", false, context);
Utils.saveBoolean("use_ru", false, context);
if (!Utils.getLanguage(context).equals("am")) {
Utils.saveString("appLanguage", "am", context);
Utils.restartApp(context);
}
break;
case 4:
if (!Utils.getBoolean("use_el", false, context)) {
Utils.saveBoolean("use_en", false, context);
Utils.saveBoolean("use_ko", false, context);
Utils.saveBoolean("use_in", false, context);
Utils.saveBoolean("use_am", false, context);
Utils.saveBoolean("use_el", true, context);
Utils.saveBoolean("use_pt", false, context);
Utils.saveBoolean("use_ru", false, context);
if (!Utils.getLanguage(context).equals("el")) {
Utils.saveString("appLanguage", "el", context);
Utils.restartApp(context);
}
break;
case 5:
if (!Utils.getBoolean("use_in", false, context)) {
Utils.saveBoolean("use_en", false, context);
Utils.saveBoolean("use_ko", false, context);
Utils.saveBoolean("use_in", true, context);
Utils.saveBoolean("use_am", false, context);
Utils.saveBoolean("use_el", false, context);
Utils.saveBoolean("use_pt", false, context);
Utils.saveBoolean("use_ru", false, context);
if (!Utils.getLanguage(context).equals("in")) {
Utils.saveString("appLanguage", "in", context);
Utils.restartApp(context);
}
break;
case 6:
if (!Utils.getBoolean("use_pt", false, context)) {
Utils.saveBoolean("use_en", false, context);
Utils.saveBoolean("use_ko", false, context);
Utils.saveBoolean("use_in", false, context);
Utils.saveBoolean("use_am", false, context);
Utils.saveBoolean("use_el", false, context);
Utils.saveBoolean("use_pt", true, context);
Utils.saveBoolean("use_ru", false, context);
if (!Utils.getLanguage(context).equals("pt")) {
Utils.saveString("appLanguage", "pt", context);
Utils.restartApp(context);
}
break;
case 7:
if (!Utils.getBoolean("use_ru", false, context)) {
Utils.saveBoolean("use_en", false, context);
Utils.saveBoolean("use_ko", false, context);
Utils.saveBoolean("use_in", false, context);
Utils.saveBoolean("use_am", false, context);
Utils.saveBoolean("use_el", false, context);
Utils.saveBoolean("use_pt", false, context);
Utils.saveBoolean("use_ru", true, context);
if (!Utils.getLanguage(context).equals("ru")) {
Utils.saveString("appLanguage", "ru", context);
Utils.restartApp(context);
}
break;
case 8:
if (!Utils.getLanguage(context).equals("pl")) {
Utils.saveString("appLanguage", "pl", context);
Utils.restartApp(context);
}
break;
case 9:
if (!Utils.getLanguage(context).equals("zh")) {
Utils.saveString("appLanguage", "zh", context);
Utils.restartApp(context);
}
break;
case 10:
if (!Utils.getLanguage(context).equals("uk")) {
Utils.saveString("appLanguage", "uk", context);
Utils.restartApp(context);
}
break;
Expand All @@ -167,22 +137,29 @@ public static void setLanguage(Context context) {
}

public static String getLanguage(Context context) {
if (Utils.getBoolean("use_english", false, context)) {
return context.getString(R.string.language_en);
} else if (Utils.getBoolean("use_korean", false, context)) {
return context.getString(R.string.language_ko);
} else if (Utils.getBoolean("use_am", false, context)) {
return context.getString(R.string.language_am);
} else if (Utils.getBoolean("use_el", false, context)) {
return context.getString(R.string.language_el);
}else if (Utils.getBoolean("use_in", false, context)) {
return context.getString(R.string.language_in);
} else if (Utils.getBoolean("use_pt", false, context)) {
return context.getString(R.string.language_pt);
} else if (Utils.getBoolean("use_ru", false, context)) {
return context.getString(R.string.language_ru);
} else {
return context.getString(R.string.language_default);
switch (Utils.getLanguage(context)) {
case "en_US":
return context.getString(R.string.language_en);
case "ko":
return context.getString(R.string.language_ko);
case "am":
return context.getString(R.string.language_am);
case "el":
return context.getString(R.string.language_el);
case "pt":
return context.getString(R.string.language_pt);
case "ru":
return context.getString(R.string.language_ru);
case "in":
return context.getString(R.string.language_in);
case "uk":
return context.getString(R.string.language_uk);
case "zh":
return context.getString(R.string.language_zh);
case "pl":
return context.getString(R.string.language_pl);
default:
return context.getString(R.string.language_default) + " (" + java.util.Locale.getDefault().getLanguage() + ")";
}
}

Expand Down
42 changes: 13 additions & 29 deletions app/src/main/java/com/smartpack/scriptmanager/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ public static void saveBoolean(String name, boolean value, Context context) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(name, value).apply();
}

public static String getString(String name, String defaults, Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getString(name, defaults);
}

public static void saveString(String name, String value, Context context) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(name, value).apply();
}

/*
* The following code is partly taken from https://github.com/Grarak/KernelAdiutor
* Ref: https://github.com/Grarak/KernelAdiutor/blob/master/app/src/main/java/com/grarak/kerneladiutor/utils/Utils.java
Expand Down Expand Up @@ -468,38 +476,14 @@ public static void restartApp(Context context) {
context.startActivity(intent);
}

public static boolean languageDefault(Context context) {
return !getBoolean("use_en", false, context)
&& !getBoolean("use_ko", false, context)
&& !getBoolean("use_in", false, context)
&& !getBoolean("use_am", false, context)
&& !getBoolean("use_el", false, context)
&& !getBoolean("use_pt", false, context)
&& !getBoolean("use_ru", false, context);
}

public static String getLang(Context context) {
if (getBoolean("use_en", false, context)) {
return "en_US";
} else if (getBoolean("use_ko", false, context)) {
return "ko";
} else if (getBoolean("use_in", false, context)) {
return "in";
} else if (getBoolean("use_am", false, context)) {
return "am";
} else if (getBoolean("use_el", false, context)) {
return "el";
} else if (getBoolean("use_pt", false, context)) {
return "pt";
} else if (getBoolean("use_ru", false, context)) {
return "ru";
} else {
return java.util.Locale.getDefault().getLanguage();
}
public static String getLanguage(Context context) {
return getString("appLanguage", java.util.Locale.getDefault().getLanguage(),
context);
}

public static void setLanguage(Context context) {
Locale myLocale = new Locale(getLang(context));
Locale myLocale = new Locale(getString("appLanguage", java.util.Locale.getDefault()
.getLanguage(), context));
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<item>@string/language_in</item>
<item>@string/language_pt</item>
<item>@string/language_ru</item>
<item>@string/language_pl</item>
<item>@string/language_zh</item>
<item>@string/language_uk</item>
</string-array>

<string-array name="file_picker">
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
<string name="language_en" translatable="false">English</string>
<string name="language_in" translatable="false">bahasa Indonesia</string>
<string name="language_ko" translatable="false">한국어</string>
<string name="language_pl" translatable="false">Polskie</string>
<string name="language_pt" translatable="false">Português</string>
<string name="language_ru" translatable="false">русский</string>
<string name="language_uk" translatable="false">Українська</string>
<string name="language_zh" translatable="false">中文(简体)</string>
<string name="late_start">Late Start Mode</string>
<string name="late_start_message">%s will be executed in late_start service!</string>
<string name="licence">Licence</string>
Expand Down

0 comments on commit aa0a818

Please sign in to comment.