摘要
实现功能:
1.默认采用系统语言
2.语言切换后实时生效
3.支持中英文
4.我的->设置->切换语言
思路:app首次初始设置为系统语言,用户在app内切换语言时发送语言切换事件,刷新所有页面
在AppBaseActivity、AppBaseFragment通过EventBus来增加语言变更监听,收到变更语言的时间时更新当前context的语言环境,再刷新当前界面即可
AppBaseActivity
package com.wrs.project.module.app.common.base;
import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import androidx.annotation.Nullable;
import com.wrs.project.module.app.common.R;
import com.wrs.project.module.app.common.eventbus.MessageEvent;
import com.wrs.project.module.app.common.language.LanguageService;
import com.wrs.project.module.common.base.BaseActivity;
import com.wrs.project.module.common.network.HttpMethod;
import com.wrs.project.module.common.utils.activitymgr.ActivityMgr;
import com.wrs.project.module.common.utils.utils.L;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.Map;
public class AppBaseActivity extends BaseActivity {
private String tag = "AppBaseActivity";
@SuppressLint("SourceLockedOrientationActivity")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 禁用横屏
ActivityMgr.getInstance().addActivity(this);//把当前activity添加到堆栈里面
EventBus.getDefault().register(this);
LanguageService.updateLanguageConfig(this);
initData();
initUI();
}
@Override
protected void onDestroy() {
super.onDestroy();
L.e(tag, "activity destroy:" + this.toString());
ActivityMgr.getInstance().removeActivity(this);
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
MessageEvent.EventType type = event.getEventType();
switch (type) {
case SwitchLanguage:
{
LanguageService.updateLanguageConfig(this);
refreshLanguageUI();
}
break;
}
}
protected void refreshLanguageUI() {
}
@Override
public void onStop() {
super.onStop();
}
protected void initData() {
}
protected void initUI() {
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
boolean flag = super.onKeyUp(keyCode, event);
// if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
// ActivityMgr.getInstance().removeActivity(this);
// //不执行父类点击事件
// return false;
// }
return flag;
}
}
AppBaseFragment
package com.wrs.project.module.app.common.base;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.wrs.project.module.app.common.eventbus.MessageEvent;
import com.wrs.project.module.app.common.language.LanguageService;
import com.wrs.project.module.common.base.BaseFragment;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class AppBaseFragment extends BaseFragment {
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
initData();
View view = initView(inflater, container, savedInstanceState);
if (null == view) {
view = super.onCreateView(inflater, container, savedInstanceState);
}
EventBus.getDefault().register(this);
return view;
}
protected void initData() {
}
protected View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return null;
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
MessageEvent.EventType type = event.getEventType();
switch (type) {
case SwitchLanguage:
{
LanguageService.updateLanguageConfig(this.getContext());
refreshLanguageUI();
}
break;
}
}
protected void refreshLanguageUI() {
}
}
在我的->设置->切换语言可以切换app应用内语言,切换的语言类型保存到SharedPreferences
public void switchLanguageBtnClick(View view) {
AlertUtils.getAlertDialogBuilder(this, getString(R.string.mine_setting_switch_language), "", "中文", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LanguageService.switchLanguage(Locale.CHINESE);
}
}, "English", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LanguageService.switchLanguage(Locale.ENGLISH);
}
}).create().show();
}
本demo只做了中英文的切换,如果还需要支持其他语言,只需要在语言的枚举类型里添加语言类型,并增加对应的页面翻译即可
public enum LanguageType {
EN(Locale.ENGLISH), // 英文
CHINESE(Locale.CHINESE); // 中文
private Locale locale;
LanguageType(Locale locale) {
this.locale = locale;
}
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
public static LanguageType getLanguageType(Locale locale) {
if (null != locale) {
for (LanguageType languageType : LanguageType.values()) {
if (languageType.getLocale().equals(locale)) {
return languageType;
}
}
}
return null;
}
}
源码下载
网友评论