Skip to content

Commit b09587b

Browse files
committed
修正使用 Locale 类的 equals 方法对比语种会导致结果不对的问题
1 parent 67dc602 commit b09587b

6 files changed

Lines changed: 50 additions & 21 deletions

File tree

README-en.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ MultiLanguages.isSystemLanguage(Context context);
105105

106106
// Compare if two languages are the same (e.g., Simplified and Traditional Chinese, American and British English)
107107
MultiLanguages.equalsLanguage(Locale locale1, Locale locale2);
108-
// Compare if two languages are from the same region (e.g., Simplified Chinese in Mainland China, Traditional Chinese in Taiwan)
109-
MultiLanguages.equalsCountry(Locale locale1, Locale locale2);
108+
// Compare whether the two languages are of the same language and from the same place (for example: simplified Chinese used in Chinese mainland and traditional Chinese used in Taiwan, China).
109+
MultiLanguages.equalsLanguageAndCountry(Locale locale1, Locale locale2);
110+
// Notes: If it is to determine whether two Locale objects are the same, please use the equalsLanguageAndCountry method for judgment instead of the equalsLanguage method
111+
// Also, please do not use the equals method that comes with the Locale class to make a judgment, as it will compare more information and may lead to a judgment error.
112+
// Github issue : https://github.com/getActivity/MultiLanguages/issues/63
110113

111114
// Get a String in a specific language
112115
MultiLanguages.getLanguageString(Context context, Locale locale, int stringId);

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,13 @@ MultiLanguages.getSystemLanguage(Context context);
103103
// 是否跟随系统的语种
104104
MultiLanguages.isSystemLanguage(Context context);
105105

106-
// 对比两个语言是否是同一个语种(比如:中文有简体和繁体,英语有美式和英式)
106+
// 对比两个语言是否同一个语种(比如:中文有简体和繁体,英语有美式和英式)
107107
MultiLanguages.equalsLanguage(Locale locale1, Locale locale2);
108-
// 对比两个语言是否是同一个地方的(比如:中国大陆用的中文简体,中国台湾用的中文繁体)
109-
MultiLanguages.equalsCountry(Locale locale1, Locale locale2);
108+
// 对比两个语言是否同一个语种并且同一个地方的(比如:中国大陆用的中文简体,中国台湾用的中文繁体)
109+
MultiLanguages.equalsLanguageAndCountry(Locale locale1, Locale locale2);
110+
// 注意事项:如果是为了判断两个 Locale 对象是否相同,请使用 equalsLanguageAndCountry 方法来判断,而不是 equalsLanguage 方法,
111+
// 另外请不要用 Locale 类中自带的 equals 方法来判断,因为它会比较更多的信息,会导致判断错误。
112+
// Github 相关问题 issue:https://github.com/getActivity/MultiLanguages/issues/63
110113

111114
// 获取某个语种下的 String
112115
MultiLanguages.getLanguageString(Context context, Locale locale, int stringId);

app/src/main/java/com/hjq/language/demo/MainActivity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ protected void onCreate(Bundle savedInstanceState) {
6262
radioGroup.check(R.id.rb_main_language_auto);
6363
} else {
6464
Locale locale = MultiLanguages.getAppLanguage(this);
65-
if (LocaleContract.getSimplifiedChineseLocale().equals(locale)) {
65+
if (MultiLanguages.equalsLanguageAndCountry(locale, LocaleContract.getSimplifiedChineseLocale())) {
6666
radioGroup.check(R.id.rb_main_language_cn);
67-
} else if (LocaleContract.getTraditionalChineseLocale().equals(locale)) {
67+
} else if (MultiLanguages.equalsLanguageAndCountry(locale, LocaleContract.getTraditionalChineseLocale())) {
6868
radioGroup.check(R.id.rb_main_language_tw);
69-
} else if (LocaleContract.getEnglishLocale().equals(locale)) {
69+
} else if (MultiLanguages.equalsLanguageAndCountry(locale, LocaleContract.getEnglishLocale())) {
7070
radioGroup.check(R.id.rb_main_language_en);
71-
} else if (LocaleContract.getArabicLocale().equals(locale)) {
71+
} else if (MultiLanguages.equalsLanguageAndCountry(locale, LocaleContract.getArabicLocale())) {
7272
radioGroup.check(R.id.rb_main_language_ar);
7373
} else {
7474
radioGroup.check(R.id.rb_main_language_auto);

library/src/main/java/com/hjq/language/LanguagesUtils.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.res.Resources;
77
import android.os.Build;
88
import android.os.LocaleList;
9+
import android.text.TextUtils;
910
import java.util.Locale;
1011

1112
/**
@@ -41,7 +42,7 @@ static void setLocale(Configuration config, Locale locale) {
4142
config.setLocales(localeList);
4243
// 修复在 Android 13 的联想平板上调用 setLocales 或者 setLocale 修改语种无效的问题
4344
// Github issue 地址:https://github.com/getActivity/MultiLanguages/pull/62
44-
if (!locale.equals(config.getLocales().get(0))) {
45+
if (!equalsLanguageAndCountry(locale, config.getLocales().get(0))) {
4546
config.locale = locale;
4647
}
4748
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
@@ -144,4 +145,20 @@ static Resources generateLanguageResources(Context context, Locale locale) {
144145
}
145146
return new Resources(context.getAssets(), context.getResources().getDisplayMetrics(), config);
146147
}
148+
149+
/**
150+
* 对比两个语言是否同一个语种(比如:中文有简体和繁体,但是它们都属于同一个语种)
151+
*/
152+
public static boolean equalsLanguage(Locale locale1, Locale locale2) {
153+
return TextUtils.equals(locale1.getLanguage(), locale2.getLanguage());
154+
}
155+
156+
/**
157+
* 对比两个语言是否同一个语种并且同一个地方的(比如:中国大陆用的中文简体,中国台湾用的中文繁体)
158+
*/
159+
public static boolean equalsLanguageAndCountry(Locale locale1, Locale locale2) {
160+
// Github issue 地址:https://github.com/getActivity/MultiLanguages/issues/63
161+
return TextUtils.equals(locale1.getLanguage(), locale2.getLanguage()) &&
162+
TextUtils.equals(locale1.getCountry(), locale2.getCountry());
163+
}
147164
}

library/src/main/java/com/hjq/language/LocaleChangeReceiver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void onReceive(Context context, Intent intent) {
5151
}
5252

5353
Locale latestSystemLocale = MultiLanguages.getSystemLanguage(mApplication);
54-
if (MultiLanguages.equalsCountry(latestSystemLocale, sSystemLanguage)) {
54+
if (MultiLanguages.equalsLanguageAndCountry(latestSystemLocale, sSystemLanguage)) {
5555
return;
5656
}
5757

library/src/main/java/com/hjq/language/MultiLanguages.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public boolean queueIdle() {
7777
*/
7878
public static Context attach(Context context) {
7979
Locale locale = getAppLanguage(context);
80-
if (LanguagesUtils.getLocale(context).equals(locale)) {
80+
if (equalsLanguageAndCountry(locale, LanguagesUtils.getLocale(context))) {
8181
return context;
8282
}
8383
return LanguagesUtils.attachLanguages(context, locale);
@@ -99,12 +99,12 @@ public static void updateAppLanguage(Context context, Resources resources) {
9999
}
100100
Locale locale = getAppLanguage(context);
101101
Configuration config = resources.getConfiguration();
102-
if (!locale.equals(LanguagesUtils.getLocale(config))
102+
if (!LanguagesUtils.equalsLanguageAndCountry(locale, LanguagesUtils.getLocale(config))
103103
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
104104
&& TextUtils.getLayoutDirectionFromLocale(locale) != config.getLayoutDirection())) {
105105
LanguagesUtils.updateLanguages(resources, locale);
106106
}
107-
if (!locale.equals(LanguagesUtils.getDefaultLocale())) {
107+
if (!LanguagesUtils.equalsLanguageAndCountry(locale, LanguagesUtils.getDefaultLocale())) {
108108
// Github issue 地址:https://github.com/getActivity/MultiLanguages/issues/59
109109
LanguagesUtils.setDefaultLocale(context);
110110
}
@@ -136,7 +136,7 @@ public static boolean setAppLanguage(Context context, Locale newLocale) {
136136
// 1. 调用此 API 会自动重启 Activity,而框架是将重启操作放到了外层给开发者去重启
137137
// 2. 上面说了,调用此 API 会重启 Activity,重启也就算了,还顺带闪了一下,这个不能忍
138138
LanguagesConfig.saveAppLanguageSetting(context, newLocale);
139-
if (LanguagesUtils.getLocale(context).equals(newLocale)) {
139+
if (LanguagesUtils.equalsLanguageAndCountry(newLocale, LanguagesUtils.getLocale(context))) {
140140
return false;
141141
}
142142

@@ -177,7 +177,7 @@ public static boolean isSystemLanguage(Context context) {
177177
*/
178178
public static boolean clearAppLanguage(Context context) {
179179
LanguagesConfig.clearLanguageSetting(context);
180-
if (LanguagesUtils.getLocale(context).equals(getSystemLanguage(sApplication))) {
180+
if (LanguagesUtils.equalsLanguageAndCountry(getSystemLanguage(sApplication), LanguagesUtils.getLocale(context))) {
181181
return false;
182182
}
183183

@@ -198,18 +198,24 @@ public static void setDefaultLanguage(Locale locale) {
198198
}
199199

200200
/**
201-
* 对比两个语言是否是同一个语种(比如:中文有简体和繁体,但是它们都属于同一个语种)
201+
* 对比两个语言是否同一个语种(比如:中文有简体和繁体,但是它们都属于同一个语种)
202202
*/
203203
public static boolean equalsLanguage(Locale locale1, Locale locale2) {
204-
return TextUtils.equals(locale1.getLanguage(), locale2.getLanguage());
204+
return LanguagesUtils.equalsLanguage(locale1, locale2);
205205
}
206206

207207
/**
208-
* 对比两个语言是否是同一个地方的(比如:中国大陆用的中文简体,中国台湾用的中文繁体)
208+
* @deprecated 该 API 已经过时,随时会被删除,请尽早迁移到 {@link #equalsLanguageAndCountry(Locale, Locale)}
209209
*/
210210
public static boolean equalsCountry(Locale locale1, Locale locale2) {
211-
return equalsLanguage(locale1, locale2) &&
212-
TextUtils.equals(locale1.getCountry(), locale2.getCountry());
211+
return equalsLanguageAndCountry(locale1, locale2);
212+
}
213+
214+
/**
215+
* 对比两个语言是否同一个语种并且同一个地方的(比如:中国大陆用的中文简体,中国台湾用的中文繁体)
216+
*/
217+
public static boolean equalsLanguageAndCountry(Locale locale1, Locale locale2) {
218+
return LanguagesUtils.equalsLanguageAndCountry(locale1, locale2);
213219
}
214220

215221
/**

0 commit comments

Comments
 (0)