在开发海外项目的时候,需要实现app内部的中英文切换功能,所有的英文都是内置的,整体思路为:
创建一个sp对象,存储当前系统的语言类型,然后在BaseActivity中对语言进行判断;
//公共Activity
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setTransparent();
super.onCreate(savedInstanceState);
setContentView(initLayout());
String languageCode = SpUtils.getString(this, Constants.LANGUAGE);
//从SP中取出来的当前语言类型;中文是:"cn",英文是:"en",对应底下的这个languageCode
//系统内置的设置app内语言的功能;
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
}
//设置布局
protected abstract int initLayout();
}
2、可以新开一个页面,用来set当前的sp中数据(只写代码)
Constants.LANGUAGE:
public static final String LANGUAGE = "application_language";//语言设置
//可以自定义一个String字符串,不用全局变量
//设置语言的方法,通过传入的language判断当前用户选择的是中文还是英文
public void setLanguage(String language) {
if (language.equals("中文")) {
switchLanguage("cn");
} else {
switchLanguage("en");
}
}
//选完之后就可以将app中的语言进行切换
public void switchLanguage(String languageCode) {
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
SpUtils.putString(mContext, Constants.LANGUAGE, languageCode);
//(这个只是SP的工具类,可以不用这个,直接用SP写入,都是一样的,我习惯别名用静态的,不容易写错)
restartApplication(mContext);
}
//然后重启app生效
public static void restartApplication(Context context) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
System.exit(0);
}
SpUtils:
public class SpUtils {
private static final String spFileName = "app";
public static String getString(Context context, String strKey) {
SharedPreferences setPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
String result = setPreferences.getString(strKey, "");
return result;
}
public static String getString(Context context, String strKey,
String strDefault) {
SharedPreferences setPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
String result = setPreferences.getString(strKey, strDefault);
return result;
}
public static void putString(Context context, String strKey, String strData) {
SharedPreferences activityPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = activityPreferences.edit();
editor.putString(strKey, strData);
editor.commit();
}
public static Boolean getBoolean(Context context, String strKey) {
SharedPreferences setPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
Boolean result = setPreferences.getBoolean(strKey, false);
return result;
}
public static Boolean getBoolean(Context context, String strKey,
Boolean strDefault) {
SharedPreferences setPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
Boolean result = setPreferences.getBoolean(strKey, strDefault);
return result;
}
public static void putBoolean(Context context, String strKey,
Boolean strData) {
SharedPreferences activityPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = activityPreferences.edit();
editor.putBoolean(strKey, strData);
editor.commit();
}
public static int getInt(Context context, String strKey) {
SharedPreferences setPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
int result = setPreferences.getInt(strKey, -1);
return result;
}
public static int getInt(Context context, String strKey, int strDefault) {
SharedPreferences setPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
int result = setPreferences.getInt(strKey, strDefault);
return result;
}
public static void putInt(Context context, String strKey, int strData) {
SharedPreferences activityPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = activityPreferences.edit();
editor.putInt(strKey, strData);
editor.commit();
}
public static long getLong(Context context, String strKey) {
SharedPreferences setPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
long result = setPreferences.getLong(strKey, -1);
return result;
}
public static long getLong(Context context, String strKey, long strDefault) {
SharedPreferences setPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
long result = setPreferences.getLong(strKey, strDefault);
return result;
}
public static void putLong(Context context, String strKey, long strData) {
SharedPreferences activityPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = activityPreferences.edit();
editor.putLong(strKey, strData);
editor.commit();
}
public static void cleanSp(Context context) {
SharedPreferences activityPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = activityPreferences.edit();
editor.clear();
editor.apply();
}
}