You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android(Java)应用本地化失效:无法切换语言求助

Android应用本地化语言切换失效排查与解决

问题现象

  • 2台实体设备+1个模拟器测试,Android Java应用无法切换语言,手动切换、回退至历史正常Git版本、debug/release版本均无效,始终默认英文
  • Android Studio内可切换语言预览界面,但应用运行后无效果

已尝试操作

  • 在多台设备运行应用
  • 回退至测试正常的Git提交版本
  • 检查实现代码并尝试硬编码参数
  • 排查Gradle配置(对Gradle不熟悉,怀疑此处有问题)

附相关代码

Gradle配置

plugins {
    id 'com.android.application'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.company.app"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildToolsVersion '33.0.0'
    buildFeatures {
        viewBinding true
    }
    flavorDimensions 'default'
    productFlavors {
        free {
            dimension 'default'
            applicationIdSuffix 'free'
        }
        paid {
            dimension 'default'
            applicationIdSuffix 'paid'
        }
    }

    sourceSets{
        free{
            res{
                srcDir 'src/free/res'
            }
            java{
                srcDir 'src/free/java'
            }
        }
        paid{
            res{
                srcDir 'src/paid/res'
            }
            java{
                srcDir 'src/paid/java'
            }
        }
    }
    useLibrary 'org.apache.http.legacy'
    packagingOptions {
        resources {
            excludes += ['META-INF/DEPENDENCIES', 'META-INF/NOTICE', 'META-INF/LICENSE', 'META-INF/LICENSE.txt', 'META-INF/NOTICE.txt']
        }
    }
    namespace 'com.company.app'

}


dependencies {
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

    def room_version = "2.4.2"
    def lifecycle_version = "2.6.0-alpha01"

    //room
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

    // viewModelProvider
    implementation "androidx.fragment:fragment-ktx:1.5.2"

    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"


    // Annotation processor
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    // google sign in
    implementation 'com.google.android.gms:play-services-auth:20.3.0'

    // google drive
    implementation 'com.google.api-client:google-api-client:2.0.0'
    implementation 'com.google.auth:google-auth-library-oauth2-http:1.3.0'
    implementation ('com.google.apis:google-api-services-drive:v3-rev20220815-2.0.0')
}

Java语言切换代码

Locale languageToSwitch = new Locale("nl");
Locale.setDefault(languageToSwitch);
Resources res = getBaseContext().getResources();
DisplayMetrics dm = Resources.getSystem().getDisplayMetrics();
Configuration config = res.getConfiguration();
config.locale = languageToSwitch;
res.updateConfiguration(config, dm);
recreate();

可能的原因及解决方法

1. 语言切换代码未适配高版本Android

你的切换代码仅适配API 24以下版本,API 24+系统对Configuration的Locale处理逻辑变更,直接设置config.locale不再生效。修改后的兼容代码:

Locale languageToSwitch = new Locale("nl");
Locale.setDefault(languageToSwitch);

Resources resources = getApplicationContext().getResources();
Configuration config = resources.getConfiguration();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    config.setLocales(new LocaleList(languageToSwitch));
} else {
    config.locale = languageToSwitch;
}

resources.updateConfiguration(config, resources.getDisplayMetrics());

// 重启当前Activity使设置生效
Intent intent = getIntent();
finish();
startActivity(intent);

同时需确保在自定义Application类的attachBaseContext方法中也做相同的Locale配置,避免页面重启后Locale被重置。

2. 多语言资源文件结构错误

  • 确认目标语言资源文件路径正确:荷兰语需放在res/values-nl/strings.xml(主模块),若为flavor专属资源,需放在src/free/res/values-nl/或src/paid/res/values-nl/对应目录
  • 检查资源文件是否存在拼写错误,比如文件名是否为strings.xml,翻译项是否完整

3. Gradle配置潜在问题

  • 检查defaultConfig中是否存在resConfigs配置,若指定了仅保留部分语言,会过滤掉其他语言资源(当前Gradle配置中无此设置,但需确认历史改动)
  • 建议将buildToolsVersion与compileSdk版本保持一致(比如都改为32.0.0),避免版本不兼容导致资源打包异常
  • 确认productFlavors的sourceSets配置正确,对应flavor的资源目录确实包含目标语言的strings.xml,未被主模块资源覆盖

4. 系统应用语言强制设置

Android 12+系统支持应用独立语言设置,进入设备「设置→应用→你的应用→语言」,检查是否被强制设置为英文,若有则改为「跟随系统」或手动选择目标语言

5. 缓存或安装残留问题

  • 彻底卸载应用后重新安装,清除Android Studio缓存:执行Build→Clean Project后再Build→Rebuild Project
  • 检查自定义Application类或第三方库是否存在全局Locale覆盖逻辑,部分国际化框架会自动重置Locale设置

内容的提问来源于stack exchange,提问作者Nader

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 10:21:03