最近再做一款其他国家也能通用的app,所以涉及到语言国际化,本来感觉挺简单的,直接获取手机系统语言,然后app根据用户系统语言变化而变化,但是这边还要求要加用户可以手动切换语言的功能,及涉及到退出应用肯定要保存用户之前设置好的语言偏好,在费劲了一下午终于搞定了,直接来看完整代码吧,实现过程也相对其他的比较简单:
声明 我这里设置用户可切换的语言为简体中文,英语和繁体中文
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
//初始化语言
LanguageUtil.checkLanguage(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initData()
}
private fun initData() {
nihao.setOnClickListener {
startActivity(Intent(this,LanguageChangeActivity::class.java))
}
}
}
MainActivity 里面就是 一个文字,点击文字跳转到语言选择界面
public class LanguageUtil {
//默认跟随系统语言,用户上一次切换过,显示用户设置的语言
public static void checkLanguage(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(Contast.TABLE_TABLE, MODE_PRIVATE);
String language = sharedPreferences.getString(Contast.TABLE_LANGUAGE, "");
//获取系统语言
String lablelang =context.getResources().getString(R.string.lable_lang).trim();
Log.i("BaseApplicationaa","current language is"+lablelang);
Log.i("BaseApplicationaa","save language is"+language);
String localObject1="";
DisplayMetrics localDisplayMetrics;
//本地存储的和获取系统的不一样时
if(!language.equals(lablelang)){
if(!"".equals(language)){
localObject1=language;
}else {
localObject1=lablelang;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Contast.TABLE_LANGUAGE, lablelang);
editor.commit();
}
}
Configuration localConfiguration;
Resources resources = context.getResources();
localConfiguration = resources.getConfiguration();
localDisplayMetrics = resources.getDisplayMetrics();
if ("EN".equals(localObject1)) {
localConfiguration.locale = Locale.ENGLISH;
}
if ("CN".equals(localObject1)) {
localConfiguration.locale = Locale.CHINESE;
}
if ("TW".equals(localObject1)) {
localConfiguration.locale = Locale.TAIWAN;
}
resources.updateConfiguration(localConfiguration, localDisplayMetrics);
}
}
这里我用sp来保存用户手动切换的语言,在登入app时我进行判断从而决定要显示的语言
这里的获取语言我是在我的string资源里面设置的,不同系统语言下获取的值是不一样的
这里分别对应的语言是 英语,中文,繁体,繁体
LanguageChangeActivity 切换语言
public class LanguageChangeActivity extends Activity{
String flag = null;
private RadioGroup radioGroup_Language;
private RadioButton radio_cn;
private RadioButton radio_en;
private RadioButton radio_fa;
private SharedPreferences rememberUser;
private SharedPreferences.Editor edit;
@Override
public int onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_language_change);
initData();
initListener();
}
@Override
protected void initData() {
flag = getResources().getString(lable_lang).trim();
radioGroup_Language = findViewById(R.id.radioGroup_Language);
radio_cn = findViewById(R.id.radio_cn);
radio_en = findViewById(R.id.radio_en);
radio_fa = findViewById(R.id.radio_fa);
if ("CN".equals(flag)) {
radio_cn.setChecked(true);
}
if ("EN".equals(flag)) {
radio_en.setChecked(true);
}
if("TW".equals(flag)){
radio_fa.setChecked(true);
}
}
@Override
protected void initListener() {
radioGroup_Language.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radio_cn:
flag = "CN";
break;
case R.id.radio_en:
flag = "EN";
break;
case R.id.radio_fa:
flag = "TW";
break;
}
Configuration localConfiguration;
DisplayMetrics localDisplayMetrics;
Resources resources = getResources();
localConfiguration = resources.getConfiguration();
localDisplayMetrics = resources.getDisplayMetrics();
if ("EN".equals(flag)) {
localConfiguration.locale = Locale.ENGLISH;
}
if ("CN".equals(flag)) {
localConfiguration.locale = Locale.CHINESE;
}
if("TW".equals(flag)){
localConfiguration.locale = Locale.TAIWAN;
}
//获取模式
rememberUser = getSharedPreferences(Contast.TABLE_TABLE, MODE_PRIVATE);
edit = rememberUser.edit();
edit.putString(Contast.TABLE_LANGUAGE, flag);
edit.commit();
resources.updateConfiguration(localConfiguration, localDisplayMetrics);
finish();
//清空任务栈
Intent intent = new Intent(LanguageChangeActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});
}
}
这里用sp来保存用户手动设置的语言
activity_language_change.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/grayWhite"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.LanguageChangeActivity">
<RadioGroup android:id="@+id/radioGroup_Language"
android:layout_width="match_parent"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/radio_cn"
android:textSize="16sp"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="50dp"
android:checked="true"
android:text="@string/lable_china" />
<View android:background="@color/item_boderLine"
android:layout_width="match_parent"
android:layout_height="1dp" />
<RadioButton android:id="@+id/radio_en"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:textSize="16sp"
android:text="@string/lable_english" />
<View android:background="@color/item_boderLine"
android:layout_width="match_parent"
android:layout_height="1dp" />
<RadioButton android:id="@+id/radio_fa"
android:layout_width="match_parent"
android:textSize="16sp"
android:layout_height="50dp"
android:background="@color/white"
android:text="@string/lable_fanti" />
</RadioGroup>
</LinearLayout>
欧克,这就结束了 是不是很简单 ,