Skip to content

Commit bbed563

Browse files
authored
Merge pull request #62 from Mr-JingShi/master
1. 修复了一个在一台 Android13 的联想平板上,setLocales 和 setLocale 均无效的 BUG 2. 国际化过程中除了修改语种外,适配 RTL 也是一个挑战,因此引入了阿拉伯语
2 parents ca023ea + 50741e4 commit bbed563

File tree

6 files changed

+63
-6
lines changed

6 files changed

+63
-6
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import android.annotation.TargetApi;
44
import android.content.Context;
55
import android.content.Intent;
6+
import android.graphics.Bitmap;
67
import android.net.Uri;
78
import android.os.Build;
89
import android.os.Bundle;
10+
import android.text.TextUtils;
11+
import android.view.View;
912
import android.webkit.WebChromeClient;
1013
import android.webkit.WebResourceRequest;
1114
import android.webkit.WebView;
@@ -54,7 +57,7 @@ protected void onCreate(Bundle savedInstanceState) {
5457
getApplication().getResources().getString(R.string.current_language));
5558
mSystemLanguageView = findViewById(R.id.tv_main_language_system);
5659
mSystemLanguageView.setText(MultiLanguages.getLanguageString(this,
57-
MultiLanguages.getSystemLanguage(this), R.string.current_language));
60+
MultiLanguages.getSystemLanguage(this), R.string.current_language));
5861

5962
if (MultiLanguages.isSystemLanguage(this)) {
6063
radioGroup.check(R.id.rb_main_language_auto);
@@ -66,6 +69,8 @@ protected void onCreate(Bundle savedInstanceState) {
6669
radioGroup.check(R.id.rb_main_language_tw);
6770
} else if (LocaleContract.getEnglishLocale().equals(locale)) {
6871
radioGroup.check(R.id.rb_main_language_en);
72+
} else if (LocaleContract.getArabicLocale().equals(locale)) {
73+
radioGroup.check(R.id.rb_main_language_ar);
6974
} else {
7075
radioGroup.check(R.id.rb_main_language_auto);
7176
}
@@ -94,6 +99,9 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
9499
} else if (checkedId == R.id.rb_main_language_en) {
95100
// 英语
96101
restart = MultiLanguages.setAppLanguage(this, LocaleContract.getEnglishLocale());
102+
} else if (checkedId == R.id.rb_main_language_ar) {
103+
// 阿拉伯语
104+
restart = MultiLanguages.setAppLanguage(this, LocaleContract.getArabicLocale());
97105
}
98106

99107
if (restart) {
@@ -130,7 +138,7 @@ public void onResume() {
130138
return;
131139
}
132140
mSystemLanguageView.setText(MultiLanguages.getLanguageString(this,
133-
MultiLanguages.getSystemLanguage(this), R.string.current_language));
141+
MultiLanguages.getSystemLanguage(this), R.string.current_language));
134142
}
135143

136144
@Override
@@ -189,6 +197,18 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
189197
}
190198
return true;
191199
}
200+
@Override
201+
public void onPageCommitVisible(WebView view, String url) {
202+
super.onPageCommitVisible(view, url);
203+
204+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
205+
Locale locale = MultiLanguages.getAppLanguage(view.getContext());
206+
if (View.LAYOUT_DIRECTION_RTL == TextUtils.getLayoutDirectionFromLocale(locale)) {
207+
// 水平滚动条滚动到最右边
208+
view.scrollTo(view.getWidth(), 0);
209+
}
210+
}
211+
}
192212
}
193213

194214
/**

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,33 @@
4848
android:layout_width="wrap_content"
4949
android:layout_height="wrap_content"
5050
android:layout_marginLeft="15dp"
51+
android:layout_marginStart="15dp"
5152
android:text="简体" />
5253

5354
<RadioButton
5455
android:id="@+id/rb_main_language_tw"
5556
android:layout_width="wrap_content"
5657
android:layout_height="wrap_content"
5758
android:layout_marginLeft="15dp"
59+
android:layout_marginStart="15dp"
5860
android:text="繁體" />
5961

6062
<RadioButton
6163
android:id="@+id/rb_main_language_en"
6264
android:layout_width="wrap_content"
6365
android:layout_height="wrap_content"
6466
android:layout_marginLeft="15dp"
67+
android:layout_marginStart="15dp"
6568
android:text="English" />
6669

70+
<RadioButton
71+
android:id="@+id/rb_main_language_ar"
72+
android:layout_width="wrap_content"
73+
android:layout_height="wrap_content"
74+
android:layout_marginLeft="15dp"
75+
android:layout_marginStart="15dp"
76+
android:text="عربي" />
77+
6778
</RadioGroup>
6879

6980
<LinearLayout
@@ -82,6 +93,7 @@
8293
android:layout_width="wrap_content"
8394
android:layout_height="wrap_content"
8495
android:layout_marginLeft="20dp"
96+
android:layout_marginStart="20dp"
8597
android:text="@string/current_language" />
8698
</LinearLayout>
8799

@@ -100,7 +112,8 @@
100112
android:id="@+id/tv_main_language_application"
101113
android:layout_width="wrap_content"
102114
android:layout_height="wrap_content"
103-
android:layout_marginLeft="20dp" />
115+
android:layout_marginLeft="20dp"
116+
android:layout_marginStart="20dp" />
104117
</LinearLayout>
105118

106119
<LinearLayout
@@ -119,7 +132,8 @@
119132
android:id="@+id/tv_main_language_system"
120133
android:layout_width="wrap_content"
121134
android:layout_height="wrap_content"
122-
android:layout_marginLeft="20dp" />
135+
android:layout_marginLeft="20dp"
136+
android:layout_marginStart="20dp" />
123137
</LinearLayout>
124138

125139
</LinearLayout>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="app_name">تدويل</string>
4+
<string name="current_language">اتبع النظام</string>
5+
6+
<string name="current_activity_language">النشاط الحالي اللغة:</string>
7+
<string name="current_application_language">لغة التطبيق الحالية:</string>
8+
<string name="current_system_language">لغة النظام الحالية:</string>
9+
</resources>
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest package="com.hjq.language" />
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.hjq.language">
4+
<application android:supportsRtl="true" />
5+
</manifest>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,14 @@ static void setLocale(Configuration config, Locale locale) {
3939
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
4040
LocaleList localeList = new LocaleList(locale);
4141
config.setLocales(localeList);
42+
// 在一台Android13的联想平板上,setLocales和setLocale均无效,修改config.locale生效,所以这里判断一下是否设置成功
43+
if (config.getLocales().get(0) != locale) {
44+
config.locale = locale;
45+
}
46+
config.setLayoutDirection(locale);
4247
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
4348
config.setLocale(locale);
49+
config.setLayoutDirection(locale);
4450
} else {
4551
config.locale = locale;
4652
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import android.app.Application;
44
import android.content.Context;
5+
import android.content.res.Configuration;
56
import android.content.res.Resources;
7+
import android.os.Build;
68
import android.os.Looper;
79
import android.os.MessageQueue;
810
import android.text.TextUtils;
@@ -96,7 +98,10 @@ public static void updateAppLanguage(Context context, Resources resources) {
9698
return;
9799
}
98100
Locale locale = getAppLanguage(context);
99-
if (!locale.equals(LanguagesUtils.getLocale(resources.getConfiguration()))) {
101+
Configuration config = resources.getConfiguration();
102+
if (!locale.equals(LanguagesUtils.getLocale(config))
103+
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
104+
&& TextUtils.getLayoutDirectionFromLocale(locale) != config.getLayoutDirection())) {
100105
LanguagesUtils.updateLanguages(resources, locale);
101106
}
102107
if (!locale.equals(LanguagesUtils.getDefaultLocale())) {

0 commit comments

Comments
 (0)