Skip to main content

Android 应用本地化完整指南

从 strings.xml 到 Play Store 元数据:使用 Kotlin、Jetpack Compose、Fastlane 和自动化 AI 翻译来本地化 Android 应用。

1

设置 Android 项目以支持本地化

Android 使用基于文件夹的本地化约定。默认字符串位于 res/values/strings.xml,译文则放在 res/values-de/、res/values-ja/ 等特定语言文件夹中。

Android Project Structure
// Android project structure for localization:
// res/
// ├── values/              ← Default (fallback) locale
// │   └── strings.xml
// ├── values-de/           ← German
// │   └── strings.xml
// ├── values-ja/           ← Japanese
// │   └── strings.xml
// └── values-es/           ← Spanish
//     └── strings.xml
//
// Folder naming: values-{language} or values-{language}-r{Region}
// Examples: values-pt-rBR, values-zh-rCN, values-zh-rTW
res/values/ 文件夹是回退语言。如果特定语言文件夹中缺少字符串,Android 会从默认文件夹加载。但如果默认文件中缺少字符串,应用在不受支持的语言环境中就会崩溃。
2

创建 strings.xml

Android 字符串资源使用 XML,在 '<resources>' 根元素内包含 '<string>' 元素。字符串占位符使用 %s,整数使用 %d,允许译员调整顺序的位置参数则使用 %1$s/%2$s。

res/values/strings.xml
<!-- res/values/strings.xml -->
<resources>
    <string name="welcome_title">Welcome to MyApp</string>
    <string name="login_button">Sign In</string>
    <string name="settings_label">Settings</string>
    <string name="greeting">Hello, %s!</string>        <!-- %s = string -->
    <string name="item_count">%d items</string>          <!-- %d = integer -->
    <string name="app_name" translatable="false">MyApp</string>
</resources>
未转义的撇号会导致 XML 解析器无提示崩溃。请使用 \',或用双引号包裹值。另外,默认 values/strings.xml 中缺少字符串会直接导致崩溃,不会像 iOS 那样平稳回退。
Common strings.xml Mistakes
<!-- ❌ Common mistakes in strings.xml: -->

<!-- Unescaped apostrophe — crashes XML parser silently -->
<string name="message">It's a great day</string>

<!-- Missing from default values/strings.xml — app crashes -->
<!-- (only exists in values-de/strings.xml) -->

<!-- ✅ Correct versions: -->
<string name="message">It\'s a great day</string>
<!-- Or wrap in double quotes: -->
<string name="message">"It's a great day"</string>
3

处理复数

Android 使用带 quantity 属性的 '&lt;plurals&gt;' 元素:zero、one、two、few、many、other。每种目标语言需要的类别可能不同,阿拉伯语使用全部 6 类,俄语需要 few/many,日语只使用 other。

res/values/strings.xml
<!-- res/values/strings.xml -->
<resources>
    <plurals name="items_count">
        <item quantity="zero">No items</item>
        <item quantity="one">%d item</item>
        <item quantity="other">%d items</item>
    </plurals>
</resources>

<!-- Usage in Kotlin: -->
<!-- val text = resources.getQuantityString(
    R.plurals.items_count,
    count,    // selects plural form
    count     // format argument
) -->
quantity 属性会根据设备语言的 CLDR 规则选择复数形式。务必包含 'other' 作为回退,它是每种语言都保证存在的唯一类别。
4

在代码中使用:Kotlin 与 Jetpack Compose

传统 Android 使用 getString(R.string.key) 和 resources.getQuantityString(),Jetpack Compose 使用 stringResource(R.string.key) 和 pluralStringResource()。两者都会在运行时根据设备语言解析正确译文。

WelcomeScreen.kt
// Traditional Android (Activity/Fragment)
val title = getString(R.string.welcome_title)
val greeting = getString(R.string.greeting, userName)
val items = resources.getQuantityString(
    R.plurals.items_count, count, count
)

// Jetpack Compose
@Composable
fun WelcomeScreen(userName: String, itemCount: Int) {
    // ✅ stringResource — Compose-aware, triggers recomposition
    Text(text = stringResource(R.string.welcome_title))

    // ✅ With format arguments
    Text(text = stringResource(R.string.greeting, userName))

    // ✅ Plurals — count passed TWICE
    Text(text = pluralStringResource(
        R.plurals.items_count,
        itemCount,    // selects plural form
        itemCount     // format argument
    ))
}
pluralStringResource(R.plurals.items, count, count) 中的 count 参数会传入两次:第一次选择复数形式,第二次作为格式参数。缺少第二个 count 是最常见的 Compose 复数错误。
5

字符串数组与格式化字符串

有序列表(例如下拉选项、入门步骤)使用 '&lt;string-array&gt;'。格式化字符串中使用位置格式参数(%1$s、%2$d),让译员可以调整词序而不破坏句子结构。

res/values/strings.xml
<!-- res/values/strings.xml -->
<resources>
    <!-- String array for dropdown/list -->
    <string-array name="sort_options">
        <item>Most Recent</item>
        <item>Most Popular</item>
        <item>Price: Low to High</item>
        <item>Price: High to Low</item>
    </string-array>

    <!-- Positional format args for reordering -->
    <string name="welcome_message">
        Hello %1$s, you have %2$d new messages
    </string>
    <!-- Translators can reorder: -->
    <!-- %2$d neue Nachrichten für %1$s -->
</resources>
%1$s 等位置参数允许译员自由调整参数顺序。对于词序不同的语言,'Hello %1$s, you have %2$d items' 可以改为 '%2$d items for %1$s',无需修改任何代码。
6

使用 Fastlane 本地化 Google Play 元数据

使用 Fastlane 的 supply 命令管理 Play Store 元数据,包括标题、简短描述、完整描述和更新日志,并以仓库中按语言组织的纯文本文件形式保存。

Terminal
# Install Fastlane
$ gem install fastlane

# Initialize supply for Play Store metadata
$ fastlane supply init

# Directory structure created:
# fastlane/metadata/android/
# ├── en-US/
# │   ├── title.txt              # App name (50 chars)
# │   ├── short_description.txt  # Short desc (80 chars)
# │   ├── full_description.txt   # Full desc (4000 chars)
# │   └── changelogs/
# │       └── default.txt        # What's New
# ├── de-DE/
# │   └── ...
# └── ja-JP/
#     └── ...

# Push metadata to Play Store:
$ fastlane supply
本地化 Play Store 商品详情可让非英语市场的下载量提升 30% 以上。标题、简短描述和完整描述都会纳入搜索索引,翻译这些内容是投资回报率最高的本地化工作。
Google Play

自动完成 Play Store 商品详情本地化

跳过手动复制粘贴。在遵守字符限制的同时,将 Play Store 标题、描述和发布说明翻译到 175 种以上语言环境。

了解 Google Play 集成
7

测试本地化

通过模拟器切换语言、使用自定义 LocaleList 的 Compose 预览,以及开发者选项中的伪语言来测试。使用 Gradle 中的 resConfigs 删除第三方库不需要的语言资源。

Testing Localization
// 1. Emulator: Settings > System > Language > Add language

// 2. Compose Preview with locale:
@Preview
@Composable
fun WelcomePreview() {
    val config = Configuration(resources.configuration).apply {
        setLocale(Locale("de"))
    }
    val localContext = LocalContext.current
    val localizedContext = localContext.createConfigurationContext(config)
    CompositionLocalProvider(
        LocalContext provides localizedContext
    ) {
        WelcomeScreen()
    }
}

// 3. Restrict library locales in build.gradle.kts:
android {
    defaultConfig {
        // Only include locales you actually translate
        resourceConfigurations += listOf("en", "de", "ja", "es", "fr")
    }
}

// 4. Enable pseudolocales in Developer Options:
// en-XA (accented) — detects hardcoded strings
// ar-XB (RTL) — tests layout mirroring
使用德语(文本约扩展 30%)和日语(约缩短 50%)进行测试,以发现布局问题。在开发者选项中启用伪语言(en-XA 用于重音字符,ar-XB 用于 RTL),无需真实译文即可对布局进行压力测试。

自动保证翻译质量

使用 i18n-validate 在发布前发现缺失键、损坏的占位符和复数问题。真实译文完成前,可使用 i18n-pseudo 生成伪译文来测试 UI。
8

自动翻译

使用 AI 翻译 strings.xml、复数、字符串数组和 Fastlane Supply 元数据。自动翻译应用内字符串和 Play Store 元数据,打造完整的本地化展示。

Terminal
# Translate strings.xml files
> Translate res/values/strings.xml
  to Japanese, German, and Spanish

# Translate Play Store metadata too
> Translate fastlane/metadata/android/en-US/
  to de-DE, ja-JP, es-ES

✓ 6 files translated in 3.2s
i18n Agent 会处理 Android XML 转义,保留 translatable="false" 标记,遵守每种目标语言的 CLDR 复数类别,并保持位置格式参数完整。
JetBrains

现已提供 Android Studio 插件

使用适用于 IntelliJ / Android Studio 的 i18n Agent 插件,直接从 IDE 翻译 Android XML 资源。

Install
+

额外功能:使用 LocaleChain 实现智能语言回退

Android 的资源回退由操作系统控制。pt-BR 译文缺失时,Android 会完全跳过 pt-PT 并显示英语。LocaleChain 会拦截字符串查找并遍历可配置回退链,让区域用户看到最接近的可用译文。

LocaleChain for Android 是开源 Kotlin 库。在 GitHub 上查看

build.gradle.kts
// build.gradle.kts (app module)
dependencies {
    implementation("com.i18nagent:locale-chain-android:0.1.0")
}
MyApp.kt / BaseActivity.kt
// 1. Application.onCreate() — configure chains once
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        LocaleChain.configure()
    }
}

// 2. BaseActivity — wrap context per Activity
open class BaseActivity : AppCompatActivity() {
    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(LocaleChain.wrap(newBase))
    }
}

// pt-BR user with only pt-PT translations?
// → Shows Portuguese instead of falling back to English

// Custom overrides for your specific locales:
LocaleChain.configure(
    overrides = mapOf("es-MX" to listOf("es-419", "es"))
)

常见问题

缺少默认字符串会导致崩溃

iOS 会显示原始键,而 Android 在默认 res/values/strings.xml 中缺少字符串时会抛出 ResourceNotFoundException 并崩溃。务必确保默认文件中包含所有键。

App Bundle 语言拆分会破坏应用内切换

Google Play App Bundle 会按语言拆分 APK,用户只会收到设备语言对应的字符串。如果提供应用内语言切换,请在 build.gradle.kts 中添加 bundle '{ language { enableSplit = false } }'。

RTL 布局失效

布局中使用 left/right 而不是 start/end,或 AndroidManifest.xml 中缺少 android:supportsRtl="true"。请使用 Android Studio 的 Refactor > Add RTL Support 自动转换现有布局。

库资源污染

第三方库自带 values-XX/strings.xml 文件,会让 Android 误以为应用支持实际上未翻译的语言。请在 build.gradle.kts 中使用 resConfigs,将包含的语言限制为实际已翻译的语言。

推荐的文件结构

Project Structure
MyApp/
├── app/
│   └── src/main/
│       ├── res/
│       │   ├── values/
│       │   │   ├── strings.xml          # Default (source) strings
│       │   │   └── plurals.xml          # Plural rules
│       │   ├── values-de/
│       │   │   └── strings.xml
│       │   ├── values-ja/
│       │   │   └── strings.xml
│       │   └── values-es/
│       │       └── strings.xml
│       ├── java/com/example/myapp/
│       └── AndroidManifest.xml
├── fastlane/
│   └── metadata/android/
│       ├── en-US/
│       │   ├── title.txt
│       │   ├── short_description.txt
│       │   ├── full_description.txt
│       │   └── changelogs/default.txt
│       ├── de-DE/
│       └── ja-JP/
├── build.gradle.kts
└── settings.gradle.kts

立即试用 i18n Agent

将翻译文件拖放到此处

JSON, YAML, PO, XML, CSV, Markdown, Properties

或点击选择文件

目标语言

无需注册即时估价

使用 locale-chain-android 实现语言回退

如果 pt-BR 等区域语言缺少翻译键,Android 会直接跳到默认资源文件夹,而不会先检查父语言 pt。

Terminal
implementation("com.i18nagent:locale-chain-android:0.1.0")
Configuration
import com.i18nagent.localechain.LocaleChain

LocaleChain.configure(
    overrides = mapOf(
        "pt-BR" to listOf("pt", "en"),
        "zh-Hant-HK" to listOf("zh-Hant", "zh", "en"),
    )
)

查看语言回退指南,了解受支持框架的完整列表和 75 条内置回退链。 Learn more →

Android 本地化常见问题