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

Commit aa0a818

Browse files
Settings: Re-designed language switch
Signed-off-by: sunilpaulmathew <[email protected]>
1 parent 873fb45 commit aa0a818

File tree

4 files changed

+76
-109
lines changed

4 files changed

+76
-109
lines changed

app/src/main/java/com/smartpack/scriptmanager/utils/AppSettings.java

+57-80
Original file line numberDiff line numberDiff line change
@@ -66,98 +66,68 @@ public static void setLanguage(Context context) {
6666
R.array.app_language), (dialogInterface, i) -> {
6767
switch (i) {
6868
case 0:
69-
if (!Utils.languageDefault(context)) {
70-
Utils.saveBoolean("use_en", false, context);
71-
Utils.saveBoolean("use_ko", false, context);
72-
Utils.saveBoolean("use_in", false, context);
73-
Utils.saveBoolean("use_am", false, context);
74-
Utils.saveBoolean("use_el", false, context);
75-
Utils.saveBoolean("use_pt", false, context);
76-
Utils.saveBoolean("use_ru", false, context);
69+
if (!Utils.getLanguage(context).equals(java.util.Locale.getDefault().getLanguage())) {
70+
Utils.saveString("appLanguage", java.util.Locale.getDefault().getLanguage(), context);
7771
Utils.restartApp(context);
7872
}
7973
break;
8074
case 1:
81-
if (!Utils.getBoolean("use_en", false, context)) {
82-
Utils.saveBoolean("use_en", true, context);
83-
Utils.saveBoolean("use_ko", false, context);
84-
Utils.saveBoolean("use_in", false, context);
85-
Utils.saveBoolean("use_am", false, context);
86-
Utils.saveBoolean("use_el", false, context);
87-
Utils.saveBoolean("use_pt", false, context);
88-
Utils.saveBoolean("use_ru", false, context);
75+
if (!Utils.getLanguage(context).equals("en_US")) {
76+
Utils.saveString("appLanguage", "en_US", context);
8977
Utils.restartApp(context);
9078
}
9179
break;
9280
case 2:
93-
if (!Utils.getBoolean("use_ko", false, context)) {
94-
Utils.saveBoolean("use_en", false, context);
95-
Utils.saveBoolean("use_ko", true, context);
96-
Utils.saveBoolean("use_in", false, context);
97-
Utils.saveBoolean("use_am", false, context);
98-
Utils.saveBoolean("use_el", false, context);
99-
Utils.saveBoolean("use_pt", false, context);
100-
Utils.saveBoolean("use_ru", false, context);
81+
if (!Utils.getLanguage(context).equals("ko")) {
82+
Utils.saveString("appLanguage", "ko", context);
10183
Utils.restartApp(context);
10284
}
10385
break;
10486
case 3:
105-
if (!Utils.getBoolean("use_am", false, context)) {
106-
Utils.saveBoolean("use_en", false, context);
107-
Utils.saveBoolean("use_ko", false, context);
108-
Utils.saveBoolean("use_in", false, context);
109-
Utils.saveBoolean("use_am", true, context);
110-
Utils.saveBoolean("use_el", false, context);
111-
Utils.saveBoolean("use_pt", false, context);
112-
Utils.saveBoolean("use_ru", false, context);
87+
if (!Utils.getLanguage(context).equals("am")) {
88+
Utils.saveString("appLanguage", "am", context);
11389
Utils.restartApp(context);
11490
}
11591
break;
11692
case 4:
117-
if (!Utils.getBoolean("use_el", false, context)) {
118-
Utils.saveBoolean("use_en", false, context);
119-
Utils.saveBoolean("use_ko", false, context);
120-
Utils.saveBoolean("use_in", false, context);
121-
Utils.saveBoolean("use_am", false, context);
122-
Utils.saveBoolean("use_el", true, context);
123-
Utils.saveBoolean("use_pt", false, context);
124-
Utils.saveBoolean("use_ru", false, context);
93+
if (!Utils.getLanguage(context).equals("el")) {
94+
Utils.saveString("appLanguage", "el", context);
12595
Utils.restartApp(context);
12696
}
12797
break;
12898
case 5:
129-
if (!Utils.getBoolean("use_in", false, context)) {
130-
Utils.saveBoolean("use_en", false, context);
131-
Utils.saveBoolean("use_ko", false, context);
132-
Utils.saveBoolean("use_in", true, context);
133-
Utils.saveBoolean("use_am", false, context);
134-
Utils.saveBoolean("use_el", false, context);
135-
Utils.saveBoolean("use_pt", false, context);
136-
Utils.saveBoolean("use_ru", false, context);
99+
if (!Utils.getLanguage(context).equals("in")) {
100+
Utils.saveString("appLanguage", "in", context);
137101
Utils.restartApp(context);
138102
}
139103
break;
140104
case 6:
141-
if (!Utils.getBoolean("use_pt", false, context)) {
142-
Utils.saveBoolean("use_en", false, context);
143-
Utils.saveBoolean("use_ko", false, context);
144-
Utils.saveBoolean("use_in", false, context);
145-
Utils.saveBoolean("use_am", false, context);
146-
Utils.saveBoolean("use_el", false, context);
147-
Utils.saveBoolean("use_pt", true, context);
148-
Utils.saveBoolean("use_ru", false, context);
105+
if (!Utils.getLanguage(context).equals("pt")) {
106+
Utils.saveString("appLanguage", "pt", context);
149107
Utils.restartApp(context);
150108
}
151109
break;
152110
case 7:
153-
if (!Utils.getBoolean("use_ru", false, context)) {
154-
Utils.saveBoolean("use_en", false, context);
155-
Utils.saveBoolean("use_ko", false, context);
156-
Utils.saveBoolean("use_in", false, context);
157-
Utils.saveBoolean("use_am", false, context);
158-
Utils.saveBoolean("use_el", false, context);
159-
Utils.saveBoolean("use_pt", false, context);
160-
Utils.saveBoolean("use_ru", true, context);
111+
if (!Utils.getLanguage(context).equals("ru")) {
112+
Utils.saveString("appLanguage", "ru", context);
113+
Utils.restartApp(context);
114+
}
115+
break;
116+
case 8:
117+
if (!Utils.getLanguage(context).equals("pl")) {
118+
Utils.saveString("appLanguage", "pl", context);
119+
Utils.restartApp(context);
120+
}
121+
break;
122+
case 9:
123+
if (!Utils.getLanguage(context).equals("zh")) {
124+
Utils.saveString("appLanguage", "zh", context);
125+
Utils.restartApp(context);
126+
}
127+
break;
128+
case 10:
129+
if (!Utils.getLanguage(context).equals("uk")) {
130+
Utils.saveString("appLanguage", "uk", context);
161131
Utils.restartApp(context);
162132
}
163133
break;
@@ -167,22 +137,29 @@ public static void setLanguage(Context context) {
167137
}
168138

169139
public static String getLanguage(Context context) {
170-
if (Utils.getBoolean("use_english", false, context)) {
171-
return context.getString(R.string.language_en);
172-
} else if (Utils.getBoolean("use_korean", false, context)) {
173-
return context.getString(R.string.language_ko);
174-
} else if (Utils.getBoolean("use_am", false, context)) {
175-
return context.getString(R.string.language_am);
176-
} else if (Utils.getBoolean("use_el", false, context)) {
177-
return context.getString(R.string.language_el);
178-
}else if (Utils.getBoolean("use_in", false, context)) {
179-
return context.getString(R.string.language_in);
180-
} else if (Utils.getBoolean("use_pt", false, context)) {
181-
return context.getString(R.string.language_pt);
182-
} else if (Utils.getBoolean("use_ru", false, context)) {
183-
return context.getString(R.string.language_ru);
184-
} else {
185-
return context.getString(R.string.language_default);
140+
switch (Utils.getLanguage(context)) {
141+
case "en_US":
142+
return context.getString(R.string.language_en);
143+
case "ko":
144+
return context.getString(R.string.language_ko);
145+
case "am":
146+
return context.getString(R.string.language_am);
147+
case "el":
148+
return context.getString(R.string.language_el);
149+
case "pt":
150+
return context.getString(R.string.language_pt);
151+
case "ru":
152+
return context.getString(R.string.language_ru);
153+
case "in":
154+
return context.getString(R.string.language_in);
155+
case "uk":
156+
return context.getString(R.string.language_uk);
157+
case "zh":
158+
return context.getString(R.string.language_zh);
159+
case "pl":
160+
return context.getString(R.string.language_pl);
161+
default:
162+
return context.getString(R.string.language_default) + " (" + java.util.Locale.getDefault().getLanguage() + ")";
186163
}
187164
}
188165

app/src/main/java/com/smartpack/scriptmanager/utils/Utils.java

+13-29
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ public static void saveBoolean(String name, boolean value, Context context) {
203203
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(name, value).apply();
204204
}
205205

206+
public static String getString(String name, String defaults, Context context) {
207+
return PreferenceManager.getDefaultSharedPreferences(context).getString(name, defaults);
208+
}
209+
210+
public static void saveString(String name, String value, Context context) {
211+
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(name, value).apply();
212+
}
213+
206214
/*
207215
* The following code is partly taken from https://github.com/Grarak/KernelAdiutor
208216
* Ref: https://github.com/Grarak/KernelAdiutor/blob/master/app/src/main/java/com/grarak/kerneladiutor/utils/Utils.java
@@ -468,38 +476,14 @@ public static void restartApp(Context context) {
468476
context.startActivity(intent);
469477
}
470478

471-
public static boolean languageDefault(Context context) {
472-
return !getBoolean("use_en", false, context)
473-
&& !getBoolean("use_ko", false, context)
474-
&& !getBoolean("use_in", false, context)
475-
&& !getBoolean("use_am", false, context)
476-
&& !getBoolean("use_el", false, context)
477-
&& !getBoolean("use_pt", false, context)
478-
&& !getBoolean("use_ru", false, context);
479-
}
480-
481-
public static String getLang(Context context) {
482-
if (getBoolean("use_en", false, context)) {
483-
return "en_US";
484-
} else if (getBoolean("use_ko", false, context)) {
485-
return "ko";
486-
} else if (getBoolean("use_in", false, context)) {
487-
return "in";
488-
} else if (getBoolean("use_am", false, context)) {
489-
return "am";
490-
} else if (getBoolean("use_el", false, context)) {
491-
return "el";
492-
} else if (getBoolean("use_pt", false, context)) {
493-
return "pt";
494-
} else if (getBoolean("use_ru", false, context)) {
495-
return "ru";
496-
} else {
497-
return java.util.Locale.getDefault().getLanguage();
498-
}
479+
public static String getLanguage(Context context) {
480+
return getString("appLanguage", java.util.Locale.getDefault().getLanguage(),
481+
context);
499482
}
500483

501484
public static void setLanguage(Context context) {
502-
Locale myLocale = new Locale(getLang(context));
485+
Locale myLocale = new Locale(getString("appLanguage", java.util.Locale.getDefault()
486+
.getLanguage(), context));
503487
Resources res = context.getResources();
504488
DisplayMetrics dm = res.getDisplayMetrics();
505489
Configuration conf = res.getConfiguration();

app/src/main/res/values/arrays.xml

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<item>@string/language_in</item>
1717
<item>@string/language_pt</item>
1818
<item>@string/language_ru</item>
19+
<item>@string/language_pl</item>
20+
<item>@string/language_zh</item>
21+
<item>@string/language_uk</item>
1922
</string-array>
2023

2124
<string-array name="file_picker">

app/src/main/res/values/strings.xml

+3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
<string name="language_en" translatable="false">English</string>
2727
<string name="language_in" translatable="false">bahasa Indonesia</string>
2828
<string name="language_ko" translatable="false">한국어</string>
29+
<string name="language_pl" translatable="false">Polskie</string>
2930
<string name="language_pt" translatable="false">Português</string>
3031
<string name="language_ru" translatable="false">русский</string>
32+
<string name="language_uk" translatable="false">Українська</string>
33+
<string name="language_zh" translatable="false">中文(简体)</string>
3134
<string name="late_start">Late Start Mode</string>
3235
<string name="late_start_message">%s will be executed in late_start service!</string>
3336
<string name="licence">Licence</string>

0 commit comments

Comments
 (0)