Skip to content

Commit 2b7e80f

Browse files
committed
优化代码逻辑嵌套
优化 Locale 对象处理逻辑 开放是否跟随系统的语种的 API 修复 Application 语种不同步的问题
1 parent 667b0e2 commit 2b7e80f

File tree

16 files changed

+279
-265
lines changed

16 files changed

+279
-265
lines changed

MultiLanguages.apk

-37.3 KB
Binary file not shown.

README.md

Lines changed: 69 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 多语种适配框架
1+
# 语种切换框架
22

33
* 码云地址:[Gitee](https://gitee.com/getActivity/MultiLanguages)
44

@@ -10,34 +10,33 @@
1010

1111
```groovy
1212
dependencies {
13-
// 国际化框架:https://github.com/getActivity/MultiLanguages
14-
implementation 'com.hjq:language:6.0'
13+
// 语种切换框架:https://github.com/getActivity/MultiLanguages
14+
implementation 'com.hjq:language:6.5'
1515
}
1616
```
1717

18-
#### 初始化框架
19-
18+
#### 初始化框架
19+
2020
* 在 Application 中初始化框架
2121

22-
```java
23-
public final class XxxApplication extends Application {
24-
25-
@Override
26-
public void onCreate() {
27-
super.onCreate();
28-
// 初始化多语种框架(自动适配第三方库中 Activity 语种)
29-
MultiLanguages.init(this);
30-
}
31-
}
22+
```java
23+
public final class XxxApplication extends Application {
3224

25+
@Override
26+
public void onCreate() {
27+
super.onCreate();
28+
// 初始化语种切换框架(自动适配第三方库中 Activity 语种)
29+
MultiLanguages.init(this);
30+
}
31+
}
3332
```
3433

3534
* 重写 Application 的 attachBaseContext 方法
3635

3736
```java
3837
@Override
3938
protected void attachBaseContext(Context base) {
40-
// 国际化适配(绑定语种
39+
// 绑定语种
4140
super.attachBaseContext(MultiLanguages.attach(base));
4241
}
4342
```
@@ -47,7 +46,7 @@ protected void attachBaseContext(Context base) {
4746
```java
4847
@Override
4948
protected void attachBaseContext(Context newBase) {
50-
// 国际化适配(绑定语种
49+
// 绑定语种
5150
super.attachBaseContext(MultiLanguages.attach(newBase));
5251
}
5352
```
@@ -62,15 +61,17 @@ MultiLanguages.setAppLanguage(Context context, Locale locale);
6261

6362
// 获取当前的语种
6463
MultiLanguages.getAppLanguage();
65-
```
66-
64+
```
65+
6766
#### 其他 API
6867

6968
```java
7069
// 将 App 语种设置为系统语种(返回 true 需要重启 App)
7170
MultiLanguages.setSystemLanguage(Context context);
7271
// 获取系统的语种
7372
MultiLanguages.getSystemLanguage();
73+
// 是否跟随系统的语种
74+
MultiLanguages.isSystemLanguage();
7475

7576
// 对比两个语言是否是同一个语种(比如:中文的简体和繁体,英语的美式和英式)
7677
MultiLanguages.equalsLanguage(Locale locale1, Locale locale2);
@@ -80,30 +81,30 @@ MultiLanguages.equalsCountry(Locale locale1, Locale locale2);
8081
// 获取某个语种下的 String
8182
MultiLanguages.getLanguageString(Context context, Locale locale, int stringId);
8283
// 获取某个语种下的 Resources 对象
83-
MultiLanguages.getLanguageResources(Context context, Locale locale);
84-
85-
// 更新 Context 的语种
86-
MultiLanguages.updateAppLanguage(Context context);
87-
// 更新 Resources 的语种
84+
MultiLanguages.getLanguageResources(Context context, Locale locale);
85+
86+
// 更新 Context 的语种
87+
MultiLanguages.updateAppLanguage(Context context);
88+
// 更新 Resources 的语种
8889
MultiLanguages.updateAppLanguage(Resources resources);
89-
```
90-
91-
#### 语种变化监听器
92-
93-
```java
94-
// 设置语种变化监听器
95-
MultiLanguages.setOnLanguageListener(new OnLanguageListener() {
96-
97-
@Override
98-
public void onAppLocaleChange(Locale oldLocale, Locale newLocale) {
99-
Log.d("MultiLanguages", "监听到应用切换了语种,旧语种:" + oldLocale + ",新语种:" + newLocale);
100-
}
101-
102-
@Override
103-
public void onSystemLocaleChange(Locale oldLocale, Locale newLocale) {
104-
Log.d("MultiLanguages", "监听到系统切换了语种,旧语种:" + oldLocale + ",新语种:" + newLocale);
105-
}
106-
});
90+
```
91+
92+
#### 语种变化监听器
93+
94+
```java
95+
// 设置语种变化监听器
96+
MultiLanguages.setOnLanguageListener(new OnLanguageListener() {
97+
98+
@Override
99+
public void onAppLocaleChange(Locale oldLocale, Locale newLocale) {
100+
Log.d("MultiLanguages", "监听到应用切换了语种,旧语种:" + oldLocale + ",新语种:" + newLocale);
101+
}
102+
103+
@Override
104+
public void onSystemLocaleChange(Locale oldLocale, Locale newLocale) {
105+
Log.d("MultiLanguages", "监听到系统切换了语种,旧语种:" + oldLocale + ",新语种:" + newLocale + ",是否跟随系统:" + MultiLanguages.isSystemLanguage());
106+
}
107+
});
107108
```
108109

109110
#### 使用案例
@@ -142,29 +143,29 @@ public void onClick(View v) {
142143
finish();
143144
}
144145
}
145-
```
146-
147-
#### WebView 导致语种失效的解决方案
148-
149-
* 由于 WebView 初始化会修改 Activity 语种配置,间接导致 Activity 语种会被还原回去,所以需要你手动重写 WebView 对这个问题进行修复
150-
151-
```java
152-
public final class LanguagesWebView extends WebView {
153-
154-
public LanguagesWebView(@NonNull Context context) {
155-
this(context, null);
156-
}
157-
158-
public LanguagesWebView(@NonNull Context context, @Nullable AttributeSet attrs) {
159-
this(context, attrs, 0);
160-
}
161-
162-
public LanguagesWebView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
163-
super(context, attrs, defStyleAttr);
164-
// 修复 WebView 初始化时会修改 Activity 语种配置的问题
165-
MultiLanguages.updateAppLanguage(context);
166-
}
167-
}
146+
```
147+
148+
#### WebView 导致语种失效的解决方案
149+
150+
* 由于 WebView 初始化会修改 Activity 语种配置,间接导致 Activity 语种会被还原回去,所以需要你手动重写 WebView 对这个问题进行修复
151+
152+
```java
153+
public final class LanguagesWebView extends WebView {
154+
155+
public LanguagesWebView(@NonNull Context context) {
156+
this(context, null);
157+
}
158+
159+
public LanguagesWebView(@NonNull Context context, @Nullable AttributeSet attrs) {
160+
this(context, attrs, 0);
161+
}
162+
163+
public LanguagesWebView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
164+
super(context, attrs, defStyleAttr);
165+
// 修复 WebView 初始化时会修改 Activity 语种配置的问题
166+
MultiLanguages.updateAppLanguage(context);
167+
}
168+
}
168169
```
169170

170171
#### 其他资源:[语言代码列表大全](https://github.com/championswimmer/android-locales)
@@ -187,12 +188,12 @@ public final class LanguagesWebView extends WebView {
187188

188189
* 日志查看框架:[Logcat](https://github.com/getActivity/Logcat)
189190

190-
#### Android技术讨论Q群:78797078
191-
192191
#### 微信公众号:Android轮子哥
193192

194193
![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/official_ccount.png)
195194

195+
#### Android 技术分享 QQ 群:78797078
196+
196197
#### 如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 :monkey_face:就太:thumbsup:了。您的支持将鼓励我继续创作:octocat:
197198

198199
![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/pay_ali.png) ![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/pay_wechat.png)
@@ -215,4 +216,4 @@ distributed under the License is distributed on an "AS IS" BASIS,
215216
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
216217
See the License for the specific language governing permissions and
217218
limitations under the License.
218-
```
219+
```

app/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "com.hjq.language.demo"
88
minSdkVersion 16
99
targetSdkVersion 30
10-
versionCode 60
11-
versionName "6.0"
10+
versionCode 65
11+
versionName "6.5"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {
@@ -26,9 +26,9 @@ dependencies {
2626
implementation 'androidx.appcompat:appcompat:1.2.0'
2727
implementation 'com.google.android.material:material:1.2.1'
2828

29-
// 标题栏:https://github.com/getActivity/TitleBar
29+
// 标题栏框架:https://github.com/getActivity/TitleBar
3030
implementation 'com.hjq:titlebar:8.2'
31-
// 吐司工具类:https://github.com/getActivity/ToastUtils
31+
// 吐司框架:https://github.com/getActivity/ToastUtils
3232
implementation 'com.hjq:toast:8.8'
3333
// 内存泄漏捕捉:https://github.com/square/leakcanary
3434
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.5'

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
tools:targetApi="q">
1818

1919
<activity
20-
android:name=".LanguageActivity">
20+
android:name=".MainActivity">
2121
<intent-filter>
2222
<action android:name="android.intent.action.MAIN" />
2323
<action android:name="android.intent.action.VIEW" />

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ public void onAppLocaleChange(Locale oldLocale, Locale newLocale) {
3535

3636
@Override
3737
public void onSystemLocaleChange(Locale oldLocale, Locale newLocale) {
38-
Log.d("MultiLanguages", "监听到系统切换了语种,旧语种:" + oldLocale + ",新语种:" + newLocale);
38+
Log.d("MultiLanguages", "监听到系统切换了语种,旧语种:" + oldLocale + ",新语种:" + newLocale + ",是否跟随系统:" + MultiLanguages.isSystemLanguage());
3939
}
4040
});
4141
}
4242

4343
@Override
4444
protected void attachBaseContext(Context newBase) {
45-
// 国际化适配(绑定语种
45+
// 绑定语种
4646
super.attachBaseContext(MultiLanguages.attach(newBase));
4747
}
4848
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public abstract class BaseActivity extends AppCompatActivity {
1616

1717
@Override
1818
protected void attachBaseContext(Context newBase) {
19-
// 国际化适配(绑定语种
19+
// 绑定语种
2020
super.attachBaseContext(MultiLanguages.attach(newBase));
2121
}
2222
}

0 commit comments

Comments
 (0)