最近公司在做一款app攒霹,需要做國際化處理。嗯浆洗,那就開始做吧4呤!伏社!
多語言資源文件
多語言資源文件
切換語言代碼
public void switchLanguage(Locale locale) {
Resources resources = getResources();
Configuration config = resources.getConfiguration();
DisplayMetrics dm = resources.getDisplayMetrics();
config.locale = locale; //
resources.updateConfiguration(config, dm);
}
好了抠刺,運行app,沒問題摘昌。
等等K傺!聪黎!在高版本手機上運行怎么不管用罕容?
原來Android7.0以上Configuration將通過LocaleList來管理語言,并且系統(tǒng)切換語言后稿饰,系統(tǒng)默認(rèn)語言可能并不在LocaleList頂部锦秒。
好吧,那我們開始爬坑吧喉镰。
對7.0以上手機做專門的處理
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
config.setLocales(localeList);
Locale.setDefault(locale);
return context.createConfigurationContext(config);
} else {
config.locale = locale;
}
用法
在Application中重寫attachBaseContext()調(diào)用初始化方法
8.0 在 attachBaseContext調(diào)用setLocal()返回個 context 旅择,就可以了。
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocalManageUtil.setLocal(base));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// 當(dāng)切換橫豎屏 重置語言
LocalManageUtil.setLocal(getApplicationContext());
}
然后在BaseActivtiy中重寫attachBaseContext()方法
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocalManageUtil.setLocal(base));
}
具體工具類實現(xiàn)
package base.com.jack.baselibrary.utils;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;
import java.util.Locale;
/**
* 作者: Jack
* 描述: 語言切換工具類
*/
public class LocalManageUtil {
/**
* 獲取選擇的語言設(shè)置
* @param context
* @return
*/
private static Locale getSetLanguageLocale(Context context) {
SharedPreferencesUtil.init(context);
switch (SharedPreferencesUtil.getPrefInt("local",0)) {
case 0://跟隨系統(tǒng)
return getSystemLocale();
case 1://英語
return Locale.ENGLISH;
case 2://漢語
return Locale.CHINESE;
default://默認(rèn) 漢語
return Locale.CHINESE;
}
}
/**
* 設(shè)置 本地語言
*
* @param context
* @param select
*/
public static void saveSelectLanguage(Context context, int select) {
SharedPreferencesUtil.setPrefInt("local",select);
setApplicationLanguage(context);
}
/**
* 初始化語言 方法
*
* @param context
*/
public static Context setLocal(Context context) {
return setApplicationLanguage(context);
}
/**
* 設(shè)置語言類型
*/
public static Context setApplicationLanguage(Context context) {
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
Locale locale = getSetLanguageLocale(context);//獲取sp里面保存的語言
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
config.setLocales(localeList);
Locale.setDefault(locale);
return context.createConfigurationContext(config);
} else {
config.locale = locale;
}
resources.updateConfiguration(config, dm);
return context;
}
/**
* 獲取App的locale
*
* @return Locale對象
*/
public static Locale getAppLocale() {
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = LocaleList.getDefault().get(0);
} else {
locale = Locale.getDefault();
}
return locale;
}
/**
* 獲取系統(tǒng)local
*
* @return
*/
public static Locale getSystemLocale() {
Locale locale = Resources.getSystem().getConfiguration().locale;
return locale;
}
/**
* 獲取本地保存的語言
*
* @param context
* @return
*/
public static String getLocalSaveLanguage(Context context) {
Locale locale = getSetLanguageLocale(context);
String language = locale.getLanguage();
if (language.equals("zh")) {
language = "zh-CN";
} else if (language.equals("en")) {
language = "en";
} else if (language.equals("ja")) {
language = "ja";
}
return language;
}
}
最后在需要調(diào)用語言切換的地方調(diào)用就可以了
switch (view.getId()) {
case R.id.chinese:
LocalManageUtil.saveSelectLanguage(this,2);
break;
case R.id.english:
LocalManageUtil.saveSelectLanguage(this,1);
break;
}
可能在某些手機上還是會有問題侣姆,因為測試機有限只能做到現(xiàn)在的程度了砌左。
參考https://blog.csdn.net/sinat_32961877/article/details/85007148