Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions app/src/main/java/com/hjq/language/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
Expand Down Expand Up @@ -54,7 +57,7 @@ protected void onCreate(Bundle savedInstanceState) {
getApplication().getResources().getString(R.string.current_language));
mSystemLanguageView = findViewById(R.id.tv_main_language_system);
mSystemLanguageView.setText(MultiLanguages.getLanguageString(this,
MultiLanguages.getSystemLanguage(this), R.string.current_language));
MultiLanguages.getSystemLanguage(this), R.string.current_language));

if (MultiLanguages.isSystemLanguage(this)) {
radioGroup.check(R.id.rb_main_language_auto);
Expand All @@ -66,6 +69,8 @@ protected void onCreate(Bundle savedInstanceState) {
radioGroup.check(R.id.rb_main_language_tw);
} else if (LocaleContract.getEnglishLocale().equals(locale)) {
radioGroup.check(R.id.rb_main_language_en);
} else if (LocaleContract.getArabicLocale().equals(locale)) {
radioGroup.check(R.id.rb_main_language_ar);
} else {
radioGroup.check(R.id.rb_main_language_auto);
}
Expand Down Expand Up @@ -94,6 +99,9 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
} else if (checkedId == R.id.rb_main_language_en) {
// 英语
restart = MultiLanguages.setAppLanguage(this, LocaleContract.getEnglishLocale());
} else if (checkedId == R.id.rb_main_language_ar) {
// 阿拉伯语
restart = MultiLanguages.setAppLanguage(this, LocaleContract.getArabicLocale());
}

if (restart) {
Expand Down Expand Up @@ -130,7 +138,7 @@ public void onResume() {
return;
}
mSystemLanguageView.setText(MultiLanguages.getLanguageString(this,
MultiLanguages.getSystemLanguage(this), R.string.current_language));
MultiLanguages.getSystemLanguage(this), R.string.current_language));
}

@Override
Expand Down Expand Up @@ -189,6 +197,18 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
}
return true;
}
@Override
public void onPageCommitVisible(WebView view, String url) {
super.onPageCommitVisible(view, url);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Locale locale = MultiLanguages.getAppLanguage(view.getContext());
if (View.LAYOUT_DIRECTION_RTL == TextUtils.getLayoutDirectionFromLocale(locale)) {
// 水平滚动条滚动到最右边
view.scrollTo(view.getWidth(), 0);
}
}
}
}

/**
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,33 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:text="简体" />

<RadioButton
android:id="@+id/rb_main_language_tw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:text="繁體" />

<RadioButton
android:id="@+id/rb_main_language_en"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:text="English" />

<RadioButton
android:id="@+id/rb_main_language_ar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:text="عربي" />

</RadioGroup>

<LinearLayout
Expand All @@ -82,6 +93,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:text="@string/current_language" />
</LinearLayout>

Expand All @@ -100,7 +112,8 @@
android:id="@+id/tv_main_language_application"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp" />
</LinearLayout>

<LinearLayout
Expand All @@ -119,7 +132,8 @@
android:id="@+id/tv_main_language_system"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp" />
</LinearLayout>

</LinearLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">تدويل</string>
<string name="current_language">اتبع النظام</string>

<string name="current_activity_language">النشاط الحالي اللغة:</string>
<string name="current_application_language">لغة التطبيق الحالية:</string>
<string name="current_system_language">لغة النظام الحالية:</string>
</resources>
5 changes: 4 additions & 1 deletion library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.hjq.language" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hjq.language">
<application android:supportsRtl="true" />
</manifest>
6 changes: 6 additions & 0 deletions library/src/main/java/com/hjq/language/LanguagesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ static void setLocale(Configuration config, Locale locale) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
config.setLocales(localeList);
// 在一台Android13的联想平板上,setLocales和setLocale均无效,修改config.locale生效,所以这里判断一下是否设置成功
if (config.getLocales().get(0) != locale) {
config.locale = locale;
}
config.setLayoutDirection(locale);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(locale);
config.setLayoutDirection(locale);
} else {
config.locale = locale;
}
Expand Down
7 changes: 6 additions & 1 deletion library/src/main/java/com/hjq/language/MultiLanguages.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Looper;
import android.os.MessageQueue;
import android.text.TextUtils;
Expand Down Expand Up @@ -96,7 +98,10 @@ public static void updateAppLanguage(Context context, Resources resources) {
return;
}
Locale locale = getAppLanguage(context);
if (!locale.equals(LanguagesUtils.getLocale(resources.getConfiguration()))) {
Configuration config = resources.getConfiguration();
if (!locale.equals(LanguagesUtils.getLocale(config))
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
&& TextUtils.getLayoutDirectionFromLocale(locale) != config.getLayoutDirection())) {
LanguagesUtils.updateLanguages(resources, locale);
}
if (!locale.equals(LanguagesUtils.getDefaultLocale())) {
Expand Down