Skip to content

Commit b47f7be

Browse files
committed
修复在设置中切换语种后重启应用语种不准确的问题
优化框架内部中的一些方法命名、代码逻辑、代码注释
1 parent 701110a commit b47f7be

File tree

10 files changed

+70
-52
lines changed

10 files changed

+70
-52
lines changed

HelpDoc.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public final class MainActivity extends Activity {
173173

174174
mWebView.setWebViewClient(new LanguagesViewClient());
175175
mWebView.setWebChromeClient(new WebChromeClient());
176-
mWebView.loadUrl("https://developer.android.google.cn/kotlin", generateLanguageRequestHeader());
176+
mWebView.loadUrl("https://developer.android.google.cn/kotlin", generateLanguageRequestHeader(this));
177177
}
178178

179179
public static class LanguagesViewClient extends WebViewClient {
@@ -194,7 +194,7 @@ public final class MainActivity extends Activity {
194194
// 如果这是跳链接操作
195195
case "http":
196196
case "https":
197-
view.loadUrl(url, generateLanguageRequestHeader());
197+
view.loadUrl(url, generateLanguageRequestHeader(view.getContext()));
198198
break;
199199
default:
200200
break;
@@ -207,11 +207,11 @@ public final class MainActivity extends Activity {
207207
* 给 WebView 请求头添加语种环境
208208
*/
209209
@NonNull
210-
public static Map<String, String> generateLanguageRequestHeader() {
210+
public static Map<String, String> generateLanguageRequestHeader(Context context) {
211211
Map<String, String> map = new HashMap<>(1);
212212
// Android 13 上面语种失效的问题解决方案
213213
// https://developer.android.google.cn/about/versions/13/features/app-languages?hl=zh-cn#consider-header
214-
map.put("Accept-Language", String.valueOf(MultiLanguages.getAppLanguage()));
214+
map.put("Accept-Language", String.valueOf(MultiLanguages.getAppLanguage(context)));
215215
return map;
216216
}
217217
}

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
* 项目地址:[Github](https://github.com/getActivity/MultiLanguages)
44

5-
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/MultiLanguages/releases/download/9.2/MultiLanguages.apk)
5+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/MultiLanguages/releases/download/9.3/MultiLanguages.apk)
66

77
![](picture/demo_code.png)
88

@@ -37,7 +37,7 @@ dependencyResolutionManagement {
3737
```groovy
3838
dependencies {
3939
// 语种切换框架:https://github.com/getActivity/MultiLanguages
40-
implementation 'com.github.getActivity:MultiLanguages:9.2'
40+
implementation 'com.github.getActivity:MultiLanguages:9.3'
4141
}
4242
```
4343

@@ -89,7 +89,7 @@ protected void attachBaseContext(Context newBase) {
8989
MultiLanguages.setAppLanguage(Context context, Locale locale);
9090

9191
// 获取当前的语种
92-
MultiLanguages.getAppLanguage();
92+
MultiLanguages.getAppLanguage(Context context);
9393

9494
// 跟随系统语种(返回 true 表示需要重启 App)
9595
MultiLanguages.clearAppLanguage(Context context);
@@ -110,8 +110,8 @@ MultiLanguages.equalsCountry(Locale locale1, Locale locale2);
110110

111111
// 获取某个语种下的 String
112112
MultiLanguages.getLanguageString(Context context, Locale locale, int stringId);
113-
// 获取某个语种下的 Resources 对象
114-
MultiLanguages.getLanguageResources(Context context, Locale locale);
113+
// 生成某个语种下的 Resources 对象
114+
MultiLanguages.generateLanguageResources(Context context, Locale locale);
115115

116116
// 更新 Context 的语种
117117
MultiLanguages.updateAppLanguage(Context context);

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
applicationId "com.hjq.language.demo"
99
minSdkVersion 16
1010
targetSdkVersion 34
11-
versionCode 902
12-
versionName "9.2"
11+
versionCode 930
12+
versionName "9.3"
1313
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1414
}
1515

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hjq.language.demo;
22

33
import android.annotation.TargetApi;
4+
import android.content.Context;
45
import android.content.Intent;
56
import android.net.Uri;
67
import android.os.Build;
@@ -30,6 +31,7 @@ public final class MainActivity extends BaseActivity
3031
implements RadioGroup.OnCheckedChangeListener, OnTitleBarListener {
3132

3233
private WebView mWebView;
34+
private TextView mSystemLanguageView;
3335

3436
@Override
3537
protected void onCreate(Bundle savedInstanceState) {
@@ -45,18 +47,19 @@ protected void onCreate(Bundle savedInstanceState) {
4547

4648
mWebView.setWebViewClient(new LanguagesViewClient());
4749
mWebView.setWebChromeClient(new WebChromeClient());
48-
mWebView.loadUrl("https://developer.android.google.cn/kotlin", generateLanguageRequestHeader());
50+
mWebView.loadUrl("https://developer.android.google.cn/kotlin", generateLanguageRequestHeader(this));
4951

5052
//((TextView) findViewById(R.id.tv_language_activity)).setText(this.getResources().getString(R.string.current_language));
5153
((TextView) findViewById(R.id.tv_main_language_application)).setText(
5254
getApplication().getResources().getString(R.string.current_language));
53-
((TextView) findViewById(R.id.tv_main_language_system)).setText(
54-
MultiLanguages.getLanguageString(this, MultiLanguages.getSystemLanguage(this), R.string.current_language));
55+
mSystemLanguageView = findViewById(R.id.tv_main_language_system);
56+
mSystemLanguageView.setText(MultiLanguages.getLanguageString(this,
57+
MultiLanguages.getSystemLanguage(this), R.string.current_language));
5558

5659
if (MultiLanguages.isSystemLanguage(this)) {
5760
radioGroup.check(R.id.rb_main_language_auto);
5861
} else {
59-
Locale locale = MultiLanguages.getAppLanguage();
62+
Locale locale = MultiLanguages.getAppLanguage(this);
6063
if (LocaleContract.getSimplifiedChineseLocale().equals(locale)) {
6164
radioGroup.check(R.id.rb_main_language_cn);
6265
} else if (LocaleContract.getTraditionalChineseLocale().equals(locale)) {
@@ -114,6 +117,12 @@ public void onResume() {
114117
super.onResume();
115118
mWebView.onResume();
116119
mWebView.resumeTimers();
120+
121+
if (mSystemLanguageView == null) {
122+
return;
123+
}
124+
mSystemLanguageView.setText(MultiLanguages.getLanguageString(this,
125+
MultiLanguages.getSystemLanguage(this), R.string.current_language));
117126
}
118127

119128
@Override
@@ -143,7 +152,7 @@ protected void onDestroy() {
143152
@Override
144153
public void onTitleClick(TitleBar titleBar) {
145154
Intent intent = new Intent(Intent.ACTION_VIEW);
146-
intent.setData(Uri.parse("https://github.com/getActivity/MultiLanguages"));
155+
intent.setData(Uri.parse(titleBar.getTitle().toString()));
147156
startActivity(intent);
148157
}
149158

@@ -165,7 +174,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
165174
// 如果这是跳链接操作
166175
case "http":
167176
case "https":
168-
view.loadUrl(url, generateLanguageRequestHeader());
177+
view.loadUrl(url, generateLanguageRequestHeader(view.getContext()));
169178
break;
170179
default:
171180
break;
@@ -178,11 +187,11 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
178187
* 给 WebView 请求头添加语种环境
179188
*/
180189
@NonNull
181-
public static Map<String, String> generateLanguageRequestHeader() {
190+
public static Map<String, String> generateLanguageRequestHeader(Context context) {
182191
Map<String, String> map = new HashMap<>(1);
183192
// Android 13 上面语种失效的问题解决方案
184193
// https://developer.android.google.cn/about/versions/13/features/app-languages?hl=zh-cn#consider-header
185-
map.put("Accept-Language", String.valueOf(MultiLanguages.getAppLanguage()));
194+
map.put("Accept-Language", String.valueOf(MultiLanguages.getAppLanguage(context)));
186195
return map;
187196
}
188197
}

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
android:layout_width="wrap_content"
1515
android:layout_height="wrap_content"
1616
app:leftIcon="@null"
17-
app:title="@string/app_name" />
17+
app:title="https://github.com/getActivity/MultiLanguages" />
1818

1919
<com.hjq.language.demo.LanguagesWebView
2020
android:id="@+id/wv_main_web"

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ android {
55

66
defaultConfig {
77
minSdkVersion 14
8-
versionCode 902
9-
versionName "9.2"
8+
versionCode 930
9+
versionName "9.3"
1010
}
1111

1212
android.libraryVariants.configureEach { variant ->

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ final class ConfigurationObserver implements ComponentCallbacks {
1616
* 注册系统语种变化监听
1717
*/
1818
static void register(Application application) {
19-
ConfigurationObserver configurationObserver = new ConfigurationObserver();
19+
ConfigurationObserver configurationObserver = new ConfigurationObserver(application);
2020
application.registerComponentCallbacks(configurationObserver);
2121
}
2222

23+
private final Application mApplication;
24+
25+
private ConfigurationObserver(Application application) {
26+
mApplication = application;
27+
}
28+
2329
/**
2430
* 手机的配置发生了变化
2531
*/
@@ -29,11 +35,11 @@ public void onConfigurationChanged(Configuration newConfig) {
2935
return;
3036
}
3137
// 如果当前是跟随系统语种,就则不往下执行
32-
if (MultiLanguages.isSystemLanguage(MultiLanguages.getApplication())) {
38+
if (MultiLanguages.isSystemLanguage(mApplication)) {
3339
return;
3440
}
3541
// 更新 Application 的配置,否则会出现横竖屏切换之后 Application 的 orientation 没有随之变化的问题
36-
LanguagesUtils.updateConfigurationChanged(MultiLanguages.getApplication(), newConfig);
42+
LanguagesUtils.updateConfigurationChanged(mApplication, newConfig, MultiLanguages.getAppLanguage(mApplication));
3743
}
3844

3945
@Override

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,19 @@ static void updateLanguages(Resources resources, Locale locale) {
102102
/**
103103
* 更新手机配置信息变化
104104
*/
105-
static void updateConfigurationChanged(Context context, Configuration newConfig) {
105+
static void updateConfigurationChanged(Context context, Configuration newConfig, Locale appLanguage) {
106106
Configuration config = new Configuration(newConfig);
107107
// 绑定当前语种到这个新的配置对象中
108-
setLocale(config, LanguagesConfig.readAppLanguageSetting(context));
108+
setLocale(config, appLanguage);
109109
Resources resources = context.getResources();
110110
// 更新上下文的配置信息
111111
resources.updateConfiguration(config, resources.getDisplayMetrics());
112112
}
113113

114114
/**
115-
* 获取某个语种下的 Resources 对象
115+
* 生成某个语种下的 Resources 对象
116116
*/
117-
static Resources getLanguageResources(Context context, Locale locale) {
117+
static Resources generateLanguageResources(Context context, Locale locale) {
118118
Configuration config = new Configuration();
119119
setLocale(config, locale);
120120
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ final class LocaleChangeReceiver extends BroadcastReceiver {
2121
static void register(Application application) {
2222
sSystemLanguage = LanguagesUtils.getSystemLocale(application);
2323
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
24-
application.registerReceiver(new LocaleChangeReceiver(), filter);
24+
application.registerReceiver(new LocaleChangeReceiver(application), filter);
25+
}
26+
27+
private final Application mApplication;
28+
29+
public LocaleChangeReceiver(Application application) {
30+
mApplication = application;
2531
}
2632

2733
@Override
@@ -44,7 +50,7 @@ public void onReceive(Context context, Intent intent) {
4450
return;
4551
}
4652

47-
Locale latestSystemLocale = MultiLanguages.getSystemLanguage(MultiLanguages.getApplication());
53+
Locale latestSystemLocale = MultiLanguages.getSystemLanguage(mApplication);
4854
if (MultiLanguages.equalsCountry(latestSystemLocale, sSystemLanguage)) {
4955
return;
5056
}
@@ -59,8 +65,8 @@ public void notifySystemLocaleChange(Locale oldLocale, Locale newLocale) {
5965
sSystemLanguage = newLocale;
6066

6167
// 如果当前的语种是跟随系统变化的,那么就需要重置一下当前 App 的语种
62-
if (LanguagesConfig.isSystemLanguage(MultiLanguages.getApplication())) {
63-
LanguagesConfig.clearLanguageSetting(MultiLanguages.getApplication());
68+
if (LanguagesConfig.isSystemLanguage(mApplication)) {
69+
LanguagesConfig.clearLanguageSetting(mApplication);
6470
}
6571

6672
OnLanguageListener listener = MultiLanguages.getOnLanguagesListener();

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static void init(final Application application, boolean inject) {
5555
// Github issue:https://github.com/getActivity/MultiLanguages/issues/37
5656
localeManager.setApplicationLocales(LocaleList.getEmptyLocaleList());
5757
} else {
58-
localeManager.setApplicationLocales(new LocaleList(getAppLanguage()));
58+
localeManager.setApplicationLocales(new LocaleList(getAppLanguage(application)));
5959
}
6060
}
6161
}
@@ -75,7 +75,7 @@ public boolean queueIdle() {
7575
* 在上下文的子类中重写 attachBaseContext 方法(用于更新 Context 的语种)
7676
*/
7777
public static Context attach(Context context) {
78-
Locale locale = LanguagesConfig.readAppLanguageSetting(context);
78+
Locale locale = getAppLanguage(context);
7979
if (LanguagesUtils.getLocale(context).equals(locale)) {
8080
return context;
8181
}
@@ -86,27 +86,31 @@ public static Context attach(Context context) {
8686
* 更新 Context 的语种
8787
*/
8888
public static void updateAppLanguage(Context context) {
89-
updateAppLanguage(context.getResources());
89+
updateAppLanguage(context, context.getResources());
9090
}
9191

9292
/**
9393
* 更新 Resources 的语种
9494
*/
95-
public static void updateAppLanguage(Resources resources) {
95+
public static void updateAppLanguage(Context context, Resources resources) {
9696
if (resources == null) {
9797
return;
9898
}
99-
if (LanguagesUtils.getLocale(resources.getConfiguration()).equals(getAppLanguage())) {
99+
if (LanguagesUtils.getLocale(resources.getConfiguration()).equals(getAppLanguage(context))) {
100100
return;
101101
}
102-
LanguagesUtils.updateLanguages(resources, getAppLanguage());
102+
LanguagesUtils.updateLanguages(resources, getAppLanguage(context));
103103
}
104104

105105
/**
106106
* 获取 App 的语种
107107
*/
108-
public static Locale getAppLanguage() {
109-
return LanguagesConfig.readAppLanguageSetting(sApplication);
108+
public static Locale getAppLanguage(Context context) {
109+
if (isSystemLanguage(context)) {
110+
return getSystemLanguage(context);
111+
} else {
112+
return LanguagesConfig.readAppLanguageSetting(context);
113+
}
110114
}
111115

112116
/**
@@ -201,14 +205,14 @@ public static boolean equalsCountry(Locale locale1, Locale locale2) {
201205
* 获取某个语种下的 String
202206
*/
203207
public static String getLanguageString(Context context, Locale locale, int id) {
204-
return getLanguageResources(context, locale).getString(id);
208+
return generateLanguageResources(context, locale).getString(id);
205209
}
206210

207211
/**
208-
* 获取某个语种下的 Resources 对象
212+
* 生成某个语种下的 Resources 对象
209213
*/
210-
public static Resources getLanguageResources(Context context, Locale locale) {
211-
return LanguagesUtils.getLanguageResources(context, locale);
214+
public static Resources generateLanguageResources(Context context, Locale locale) {
215+
return LanguagesUtils.generateLanguageResources(context, locale);
212216
}
213217

214218
/**
@@ -231,11 +235,4 @@ public static void setSharedPreferencesName(String name) {
231235
static OnLanguageListener getOnLanguagesListener() {
232236
return sLanguageListener;
233237
}
234-
235-
/**
236-
* 获取应用上下文
237-
*/
238-
static Application getApplication() {
239-
return sApplication;
240-
}
241238
}

0 commit comments

Comments
 (0)