项目需求
应用支持多语言。
项目实现
准备语言资源文件:
在res目录下创建不同语言的资源文件夹,如values文件夹下创建values-en(英语)、values-fr(法语)等。
在这些文件夹中创建strings.xml文件,分别存放不同语言的字符串资源。
在应用中选择和应用语言:
可以通过设置应用的语言环境来选择当前使用的语言。可以存储用户的语言选择偏好,以便应用重新启动时能够保持语言设置。
动态切换语言:
Android系统支持动态切换应用的语言。可以通过更新应用的Configuration来强制应用使用特定语言的资源。
假设我们有一个简单的应用,包含两种语言的字符串资源:英语和法语。
创建语言资源文件:
在 res/values 文件夹下创建两个文件夹:values-en 和 values-fr,并在每个文件夹下创建 strings.xml 文件。
values-en/strings.xml:
<resources><string name="hello_world">Hello, World!</string>
</resources>
values-fr/strings.xml:
<resources><string name="hello_world">Bonjour le monde!</string>
</resources>
更新Locale和保存用户设置
在Android中,为了实现多语言切换,我们首先需要更新应用的Locale,并保存用户的语言设置。
/*** 更新Locale并保存用户设置* @param mContext 上下文Context* @param mNewUserLocale 新的用户Locale*/
public static void updateLocale(Context mContext, Locale mNewUserLocale) {if (needUpdateLocale(mContext, mNewUserLocale)) {Configuration configuration = mContext.getResources().getConfiguration();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {configuration.setLocale(mNewUserLocale);} else {configuration.locale = mNewUserLocale;}DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();mContext.getResources().updateConfiguration(configuration, displayMetrics);saveUserLocale(mContext, mNewUserLocale);}
}/*** 保存用户设置的Locale* @param mContext 上下文Context* @param mUserLocale 用户选择的Locale*/
public static void saveUserLocale(Context mContext, Locale mUserLocale) {String localeJson = localeToJson(mUserLocale);SharePreferenceUtil.setValue(mContext, "user_selected_language_json", localeJson);
}
updateLocale 方法根据Android版本设置应用的语言环境。
saveUserLocale 方法将用户选择的语言环境保存到SharedPreferences中,以便应用重新启动时能够恢复用户的语言选择。
处理Android 7.0以上版本的语言切换
从Android 7.0(API级别 24)开始,Google推荐使用 createConfigurationContext 方法来处理Context的语言设置,而不再推荐使用 updateConfiguration。以下是在高版本Android上处理语言切换的代码:
@TargetApi(Build.VERSION_CODES.N)
public static Context updateLocaleByHighVersion(Context mContext, Locale mNewUserLocale) {Configuration configuration = mContext.getResources().getConfiguration();configuration.setLocale(mNewUserLocale);configuration.setLocales(new LocaleList(mNewUserLocale));return mContext.createConfigurationContext(configuration);
}/*** 根据Android版本选择更新Context的方式* @param context 当前Context* @param mNewUserLocale 新的用户Locale* @return 更新后的Context*/
public static Context attachBaseContext(Context context, Locale mNewUserLocale) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {return updateLocaleByHighVersion(context, mNewUserLocale);} else {return context;}
}
updateLocaleByHighVersion 方法通过 createConfigurationContext 方法来创建新的Context,以应用新的语言环境。
attachBaseContext 方法用于在Activity或Application的 attachBaseContext 方法中更新Context的语言环境,保证应用在启动时使用正确的语言环境。
在应用中使用正确的语言环境
为了确保应用在启动时使用用户选择的语言环境,需要在每个Activity的 attachBaseContext 方法中更新Context。
@Override
protected void attachBaseContext(Context newBase) {Locale userLocale = ControlDataSourceGlobalUtil.getUserLocale(this);super.attachBaseContext(ControlDataSourceGlobalUtil.attachBaseContext(newBase, userLocale));
}
getUserLocale 方法根据应用逻辑获取用户选择的语言环境,并通过 attachBaseContext 方法更新Context。
但是每一个Activity都要这样写也太麻烦了,基本上都是写一个基类的BaseActivity,然后进行这样写,其他的Activity都继承这个基类。