-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLanguageUtils.java
More file actions
210 lines (188 loc) · 7.5 KB
/
LanguageUtils.java
File metadata and controls
210 lines (188 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.xzy.utils.language;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import androidx.annotation.NonNull;
import com.xzy.utils.common.Utils;
import com.xzy.utils.sp.SpUtils;
import java.util.List;
import java.util.Locale;
/**
* 语言工具类。
* 来自 https://github.com/Blankj/AndroidUtilCode
*
* @author xzy
*/
@SuppressWarnings("unused")
public class LanguageUtils {
private static final String KEY_LOCALE = "KEY_LOCALE";
private static final String VALUE_FOLLOW_SYSTEM = "VALUE_FOLLOW_SYSTEM";
private LanguageUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Apply the system language.
*
* @param activityClz The class of activity will be started after apply system language.
*/
public static void applySystemLanguage(final Class<? extends Activity> activityClz) {
applyLanguage(Resources.getSystem().getConfiguration().locale, activityClz, true);
}
/**
* Apply the system language.
*
* @param activityClassName The full class name of activity will be started after apply system language.
*/
public static void applySystemLanguage(final String activityClassName) {
applyLanguage(Resources.getSystem().getConfiguration().locale, activityClassName, true);
}
/**
* Apply the language.
*
* @param locale The language of locale.
* @param activityClz The class of activity will be started after apply system language.
* It will start the launcher activity if the class is null.
*/
public static void applyLanguage(@NonNull final Locale locale,
final Class<? extends Activity> activityClz) {
applyLanguage(locale, activityClz, false);
}
/**
* Apply the language.
*
* @param locale The language of locale.
* @param activityClassName The class of activity will be started after apply system language.
* It will start the launcher activity if the class name is null.
*/
public static void applyLanguage(@NonNull final Locale locale,
final String activityClassName) {
applyLanguage(locale, activityClassName, false);
}
private static void applyLanguage(@NonNull final Locale locale,
final Class<? extends Activity> activityClz,
final boolean isFollowSystem) {
if (activityClz == null) {
applyLanguage(locale, "", isFollowSystem);
return;
}
applyLanguage(locale, activityClz.getName(), isFollowSystem);
}
private static void applyLanguage(@NonNull final Locale locale,
final String activityClassName,
final boolean isFollowSystem) {
if (isFollowSystem) {
SpUtils.getInstance().put(KEY_LOCALE, VALUE_FOLLOW_SYSTEM);
} else {
String localLanguage = locale.getLanguage();
String localCountry = locale.getCountry();
SpUtils.getInstance().put(KEY_LOCALE, localLanguage + "$" + localCountry);
}
updateLanguage(Utils.getApp(), locale);
Intent intent = new Intent();
String realActivityClassName
= TextUtils.isEmpty(activityClassName) ? getLauncherActivity() : activityClassName;
intent.setComponent(new ComponentName(Utils.getApp(), realActivityClassName));
intent.addFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK
);
Utils.getApp().startActivity(intent);
}
public static void applyLanguage(@NonNull final Activity activity) {
final String spLocale = SpUtils.getInstance().getString(KEY_LOCALE);
if (TextUtils.isEmpty(spLocale)) {
return;
}
if (VALUE_FOLLOW_SYSTEM.equals(spLocale)) {
Locale sysLocale = Resources.getSystem().getConfiguration().locale;
updateLanguage(Utils.getApp(), sysLocale);
updateLanguage(activity, sysLocale);
return;
}
String[] language_country = spLocale.split("\\$");
if (language_country.length != 2) {
Log.e("LanguageUtils", "The string of " + spLocale + " is not in the " +
"correct " +
"format.");
return;
}
Locale settingLocale = new Locale(language_country[0], language_country[1]);
updateLanguage(Utils.getApp(), settingLocale);
updateLanguage(activity, settingLocale);
}
@SuppressLint("ObsoleteSdkInt")
private static void updateLanguage(Context context, Locale locale) {
Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
Locale contextLocale = config.locale;
if (equals(contextLocale.getLanguage(), locale.getLanguage())
&& equals(contextLocale.getCountry(), locale.getCountry())) {
return;
}
DisplayMetrics dm = resources.getDisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(locale);
context.createConfigurationContext(config);
} else {
config.locale = locale;
}
resources.updateConfiguration(config, dm);
}
private static boolean equals(final CharSequence s1, final CharSequence s2) {
if (s1 == s2) {
return true;
}
int length;
if (s1 != null && s2 != null && (length = s1.length()) == s2.length()) {
if (s1 instanceof String && s2 instanceof String) {
return s1.equals(s2);
} else {
for (int i = 0; i < length; i++) {
if (s1.charAt(i) != s2.charAt(i)) {
return false;
}
}
return true;
}
}
return false;
}
private static String getLauncherActivity() {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setPackage(Utils.getApp().getPackageName());
PackageManager pm = Utils.getApp().getPackageManager();
List<ResolveInfo> info = pm.queryIntentActivities(intent, 0);
ResolveInfo next = info.iterator().next();
if (next != null) {
return next.activityInfo.name;
}
return "no launcher activity";
}
/**
* 获取系统默认的语言 ,兼容 Android 7.0
*
* @return os default language
*/
private static String getDefaultLanguage() {
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = LocaleList.getDefault().get(0);
} else {
locale = Locale.getDefault();
}
/* String language = locale.getLanguage() + "-" + locale.getCountry(); */
return locale.getLanguage();
}
}