Skip to content

Commit

Permalink
修复在设置中切换语种后重启应用语种不准确的问题
Browse files Browse the repository at this point in the history
优化框架内部中的一些方法命名、代码逻辑、代码注释
  • Loading branch information
getActivity committed Dec 16, 2023
1 parent 701110a commit b47f7be
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 52 deletions.
8 changes: 4 additions & 4 deletions HelpDoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public final class MainActivity extends Activity {

mWebView.setWebViewClient(new LanguagesViewClient());
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.loadUrl("https://developer.android.google.cn/kotlin", generateLanguageRequestHeader());
mWebView.loadUrl("https://developer.android.google.cn/kotlin", generateLanguageRequestHeader(this));
}

public static class LanguagesViewClient extends WebViewClient {
Expand All @@ -194,7 +194,7 @@ public final class MainActivity extends Activity {
// 如果这是跳链接操作
case "http":
case "https":
view.loadUrl(url, generateLanguageRequestHeader());
view.loadUrl(url, generateLanguageRequestHeader(view.getContext()));
break;
default:
break;
Expand All @@ -207,11 +207,11 @@ public final class MainActivity extends Activity {
* 给 WebView 请求头添加语种环境
*/
@NonNull
public static Map<String, String> generateLanguageRequestHeader() {
public static Map<String, String> generateLanguageRequestHeader(Context context) {
Map<String, String> map = new HashMap<>(1);
// Android 13 上面语种失效的问题解决方案
// https://developer.android.google.cn/about/versions/13/features/app-languages?hl=zh-cn#consider-header
map.put("Accept-Language", String.valueOf(MultiLanguages.getAppLanguage()));
map.put("Accept-Language", String.valueOf(MultiLanguages.getAppLanguage(context)));
return map;
}
}
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

![](picture/demo_code.png)

Expand Down Expand Up @@ -37,7 +37,7 @@ dependencyResolutionManagement {
```groovy
dependencies {
// 语种切换框架:https://github.com/getActivity/MultiLanguages
implementation 'com.github.getActivity:MultiLanguages:9.2'
implementation 'com.github.getActivity:MultiLanguages:9.3'
}
```

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

// 获取当前的语种
MultiLanguages.getAppLanguage();
MultiLanguages.getAppLanguage(Context context);

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

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

// 更新 Context 的语种
MultiLanguages.updateAppLanguage(Context context);
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.hjq.language.demo"
minSdkVersion 16
targetSdkVersion 34
versionCode 902
versionName "9.2"
versionCode 930
versionName "9.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
25 changes: 17 additions & 8 deletions app/src/main/java/com/hjq/language/demo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hjq.language.demo;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
Expand Down Expand Up @@ -30,6 +31,7 @@ public final class MainActivity extends BaseActivity
implements RadioGroup.OnCheckedChangeListener, OnTitleBarListener {

private WebView mWebView;
private TextView mSystemLanguageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -45,18 +47,19 @@ protected void onCreate(Bundle savedInstanceState) {

mWebView.setWebViewClient(new LanguagesViewClient());
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.loadUrl("https://developer.android.google.cn/kotlin", generateLanguageRequestHeader());
mWebView.loadUrl("https://developer.android.google.cn/kotlin", generateLanguageRequestHeader(this));

//((TextView) findViewById(R.id.tv_language_activity)).setText(this.getResources().getString(R.string.current_language));
((TextView) findViewById(R.id.tv_main_language_application)).setText(
getApplication().getResources().getString(R.string.current_language));
((TextView) findViewById(R.id.tv_main_language_system)).setText(
MultiLanguages.getLanguageString(this, MultiLanguages.getSystemLanguage(this), R.string.current_language));
mSystemLanguageView = findViewById(R.id.tv_main_language_system);
mSystemLanguageView.setText(MultiLanguages.getLanguageString(this,
MultiLanguages.getSystemLanguage(this), R.string.current_language));

if (MultiLanguages.isSystemLanguage(this)) {
radioGroup.check(R.id.rb_main_language_auto);
} else {
Locale locale = MultiLanguages.getAppLanguage();
Locale locale = MultiLanguages.getAppLanguage(this);
if (LocaleContract.getSimplifiedChineseLocale().equals(locale)) {
radioGroup.check(R.id.rb_main_language_cn);
} else if (LocaleContract.getTraditionalChineseLocale().equals(locale)) {
Expand Down Expand Up @@ -114,6 +117,12 @@ public void onResume() {
super.onResume();
mWebView.onResume();
mWebView.resumeTimers();

if (mSystemLanguageView == null) {
return;
}
mSystemLanguageView.setText(MultiLanguages.getLanguageString(this,
MultiLanguages.getSystemLanguage(this), R.string.current_language));
}

@Override
Expand Down Expand Up @@ -143,7 +152,7 @@ protected void onDestroy() {
@Override
public void onTitleClick(TitleBar titleBar) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://github.com/getActivity/MultiLanguages"));
intent.setData(Uri.parse(titleBar.getTitle().toString()));
startActivity(intent);
}

Expand All @@ -165,7 +174,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 如果这是跳链接操作
case "http":
case "https":
view.loadUrl(url, generateLanguageRequestHeader());
view.loadUrl(url, generateLanguageRequestHeader(view.getContext()));
break;
default:
break;
Expand All @@ -178,11 +187,11 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
* 给 WebView 请求头添加语种环境
*/
@NonNull
public static Map<String, String> generateLanguageRequestHeader() {
public static Map<String, String> generateLanguageRequestHeader(Context context) {
Map<String, String> map = new HashMap<>(1);
// Android 13 上面语种失效的问题解决方案
// https://developer.android.google.cn/about/versions/13/features/app-languages?hl=zh-cn#consider-header
map.put("Accept-Language", String.valueOf(MultiLanguages.getAppLanguage()));
map.put("Accept-Language", String.valueOf(MultiLanguages.getAppLanguage(context)));
return map;
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:leftIcon="@null"
app:title="@string/app_name" />
app:title="https://github.com/getActivity/MultiLanguages" />

<com.hjq.language.demo.LanguagesWebView
android:id="@+id/wv_main_web"
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ android {

defaultConfig {
minSdkVersion 14
versionCode 902
versionName "9.2"
versionCode 930
versionName "9.3"
}

android.libraryVariants.configureEach { variant ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ final class ConfigurationObserver implements ComponentCallbacks {
* 注册系统语种变化监听
*/
static void register(Application application) {
ConfigurationObserver configurationObserver = new ConfigurationObserver();
ConfigurationObserver configurationObserver = new ConfigurationObserver(application);
application.registerComponentCallbacks(configurationObserver);
}

private final Application mApplication;

private ConfigurationObserver(Application application) {
mApplication = application;
}

/**
* 手机的配置发生了变化
*/
Expand All @@ -29,11 +35,11 @@ public void onConfigurationChanged(Configuration newConfig) {
return;
}
// 如果当前是跟随系统语种,就则不往下执行
if (MultiLanguages.isSystemLanguage(MultiLanguages.getApplication())) {
if (MultiLanguages.isSystemLanguage(mApplication)) {
return;
}
// 更新 Application 的配置,否则会出现横竖屏切换之后 Application 的 orientation 没有随之变化的问题
LanguagesUtils.updateConfigurationChanged(MultiLanguages.getApplication(), newConfig);
LanguagesUtils.updateConfigurationChanged(mApplication, newConfig, MultiLanguages.getAppLanguage(mApplication));
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions library/src/main/java/com/hjq/language/LanguagesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,19 @@ static void updateLanguages(Resources resources, Locale locale) {
/**
* 更新手机配置信息变化
*/
static void updateConfigurationChanged(Context context, Configuration newConfig) {
static void updateConfigurationChanged(Context context, Configuration newConfig, Locale appLanguage) {
Configuration config = new Configuration(newConfig);
// 绑定当前语种到这个新的配置对象中
setLocale(config, LanguagesConfig.readAppLanguageSetting(context));
setLocale(config, appLanguage);
Resources resources = context.getResources();
// 更新上下文的配置信息
resources.updateConfiguration(config, resources.getDisplayMetrics());
}

/**
* 获取某个语种下的 Resources 对象
* 生成某个语种下的 Resources 对象
*/
static Resources getLanguageResources(Context context, Locale locale) {
static Resources generateLanguageResources(Context context, Locale locale) {
Configuration config = new Configuration();
setLocale(config, locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Expand Down
14 changes: 10 additions & 4 deletions library/src/main/java/com/hjq/language/LocaleChangeReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ final class LocaleChangeReceiver extends BroadcastReceiver {
static void register(Application application) {
sSystemLanguage = LanguagesUtils.getSystemLocale(application);
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
application.registerReceiver(new LocaleChangeReceiver(), filter);
application.registerReceiver(new LocaleChangeReceiver(application), filter);
}

private final Application mApplication;

public LocaleChangeReceiver(Application application) {
mApplication = application;
}

@Override
Expand All @@ -44,7 +50,7 @@ public void onReceive(Context context, Intent intent) {
return;
}

Locale latestSystemLocale = MultiLanguages.getSystemLanguage(MultiLanguages.getApplication());
Locale latestSystemLocale = MultiLanguages.getSystemLanguage(mApplication);
if (MultiLanguages.equalsCountry(latestSystemLocale, sSystemLanguage)) {
return;
}
Expand All @@ -59,8 +65,8 @@ public void notifySystemLocaleChange(Locale oldLocale, Locale newLocale) {
sSystemLanguage = newLocale;

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

OnLanguageListener listener = MultiLanguages.getOnLanguagesListener();
Expand Down
35 changes: 16 additions & 19 deletions library/src/main/java/com/hjq/language/MultiLanguages.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void init(final Application application, boolean inject) {
// Github issue:https://github.com/getActivity/MultiLanguages/issues/37
localeManager.setApplicationLocales(LocaleList.getEmptyLocaleList());
} else {
localeManager.setApplicationLocales(new LocaleList(getAppLanguage()));
localeManager.setApplicationLocales(new LocaleList(getAppLanguage(application)));
}
}
}
Expand All @@ -75,7 +75,7 @@ public boolean queueIdle() {
* 在上下文的子类中重写 attachBaseContext 方法(用于更新 Context 的语种)
*/
public static Context attach(Context context) {
Locale locale = LanguagesConfig.readAppLanguageSetting(context);
Locale locale = getAppLanguage(context);
if (LanguagesUtils.getLocale(context).equals(locale)) {
return context;
}
Expand All @@ -86,27 +86,31 @@ public static Context attach(Context context) {
* 更新 Context 的语种
*/
public static void updateAppLanguage(Context context) {
updateAppLanguage(context.getResources());
updateAppLanguage(context, context.getResources());
}

/**
* 更新 Resources 的语种
*/
public static void updateAppLanguage(Resources resources) {
public static void updateAppLanguage(Context context, Resources resources) {
if (resources == null) {
return;
}
if (LanguagesUtils.getLocale(resources.getConfiguration()).equals(getAppLanguage())) {
if (LanguagesUtils.getLocale(resources.getConfiguration()).equals(getAppLanguage(context))) {
return;
}
LanguagesUtils.updateLanguages(resources, getAppLanguage());
LanguagesUtils.updateLanguages(resources, getAppLanguage(context));
}

/**
* 获取 App 的语种
*/
public static Locale getAppLanguage() {
return LanguagesConfig.readAppLanguageSetting(sApplication);
public static Locale getAppLanguage(Context context) {
if (isSystemLanguage(context)) {
return getSystemLanguage(context);
} else {
return LanguagesConfig.readAppLanguageSetting(context);
}
}

/**
Expand Down Expand Up @@ -201,14 +205,14 @@ public static boolean equalsCountry(Locale locale1, Locale locale2) {
* 获取某个语种下的 String
*/
public static String getLanguageString(Context context, Locale locale, int id) {
return getLanguageResources(context, locale).getString(id);
return generateLanguageResources(context, locale).getString(id);
}

/**
* 获取某个语种下的 Resources 对象
* 生成某个语种下的 Resources 对象
*/
public static Resources getLanguageResources(Context context, Locale locale) {
return LanguagesUtils.getLanguageResources(context, locale);
public static Resources generateLanguageResources(Context context, Locale locale) {
return LanguagesUtils.generateLanguageResources(context, locale);
}

/**
Expand All @@ -231,11 +235,4 @@ public static void setSharedPreferencesName(String name) {
static OnLanguageListener getOnLanguagesListener() {
return sLanguageListener;
}

/**
* 获取应用上下文
*/
static Application getApplication() {
return sApplication;
}
}

0 comments on commit b47f7be

Please sign in to comment.