【Android】本地化的实现

项目需求

应用支持多语言。

项目实现
准备语言资源文件:

在res目录下创建不同语言的资源文件夹,如values文件夹下创建values-en(英语)、values-fr(法语)等。

在这些文件夹中创建strings.xml文件,分别存放不同语言的字符串资源。

在应用中选择和应用语言:

可以通过设置应用的语言环境来选择当前使用的语言。可以存储用户的语言选择偏好,以便应用重新启动时能够保持语言设置。

动态切换语言:

Android系统支持动态切换应用的语言。可以通过更新应用的Configuration来强制应用使用特定语言的资源。

假设我们有一个简单的应用,包含两种语言的字符串资源:英语和法语。

创建语言资源文件:

在 res/values 文件夹下创建两个文件夹:values-en 和 values-fr,并在每个文件夹下创建 strings.xml 文件。

values-en/strings.xml:

java 复制代码
<resources>
    <string name="hello_world">Hello, World!</string>
</resources>

values-fr/strings.xml:

java 复制代码
<resources>
    <string name="hello_world">Bonjour le monde!</string>
</resources>
更新Locale和保存用户设置

在Android中,为了实现多语言切换,我们首先需要更新应用的Locale,并保存用户的语言设置。

java 复制代码
/**
 * 更新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上处理语言切换的代码:

java 复制代码
@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。

java 复制代码
@Override
protected void attachBaseContext(Context newBase) {
    Locale userLocale = ControlDataSourceGlobalUtil.getUserLocale(this);
    super.attachBaseContext(ControlDataSourceGlobalUtil.attachBaseContext(newBase, userLocale));
}

getUserLocale 方法根据应用逻辑获取用户选择的语言环境,并通过 attachBaseContext 方法更新Context。

但是每一个Activity都要这样写也太麻烦了,基本上都是写一个基类的BaseActivity,然后进行这样写,其他的Activity都继承这个基类。

相关推荐
李堇2 小时前
android滚动列表VerticalRollingTextView
android·java
lxysbly4 小时前
n64模拟器安卓版带金手指2026
android
游戏开发爱好者86 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
王码码20357 小时前
Flutter for OpenHarmony 实战之基础组件:第三十一篇 Chip 系列组件 — 灵活的标签化交互
android·flutter·交互·harmonyos
黑码哥7 小时前
ViewHolder设计模式深度剖析:iOS开发者掌握Android列表性能优化的实战指南
android·ios·性能优化·跨平台开发·viewholder
亓才孓7 小时前
[JDBC]元数据
android
独行soc7 小时前
2026年渗透测试面试题总结-17(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
金融RPA机器人丨实在智能7 小时前
Android Studio开发App项目进入AI深水区:实在智能Agent引领无代码交互革命
android·人工智能·ai·android studio
科技块儿7 小时前
利用IP查询在智慧城市交通信号系统中的应用探索
android·tcp/ip·智慧城市
独行soc8 小时前
2026年渗透测试面试题总结-18(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮