
Android 应用本地化完整指南
从 strings.xml 到 Play Store 元数据:使用 Kotlin、Jetpack Compose、Fastlane 和自动化 AI 翻译来本地化 Android 应用。
设置 Android 项目以支持本地化
Android 使用基于文件夹的本地化约定。默认字符串位于 res/values/strings.xml,译文则放在 res/values-de/、res/values-ja/ 等特定语言文件夹中。
// 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创建 strings.xml
Android 字符串资源使用 XML,在 '<resources>' 根元素内包含 '<string>' 元素。字符串占位符使用 %s,整数使用 %d,允许译员调整顺序的位置参数则使用 %1$s/%2$s。
<!-- 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><!-- ❌ 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>处理复数
Android 使用带 quantity 属性的 '<plurals>' 元素:zero、one、two、few、many、other。每种目标语言需要的类别可能不同,阿拉伯语使用全部 6 类,俄语需要 few/many,日语只使用 other。
<!-- 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
) -->在代码中使用:Kotlin 与 Jetpack Compose
传统 Android 使用 getString(R.string.key) 和 resources.getQuantityString(),Jetpack Compose 使用 stringResource(R.string.key) 和 pluralStringResource()。两者都会在运行时根据设备语言解析正确译文。
// 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
))
}字符串数组与格式化字符串
有序列表(例如下拉选项、入门步骤)使用 '<string-array>'。格式化字符串中使用位置格式参数(%1$s、%2$d),让译员可以调整词序而不破坏句子结构。
<!-- 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>使用 Fastlane 本地化 Google Play 元数据
使用 Fastlane 的 supply 命令管理 Play Store 元数据,包括标题、简短描述、完整描述和更新日志,并以仓库中按语言组织的纯文本文件形式保存。
# 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测试本地化
通过模拟器切换语言、使用自定义 LocaleList 的 Compose 预览,以及开发者选项中的伪语言来测试。使用 Gradle 中的 resConfigs 删除第三方库不需要的语言资源。
// 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自动保证翻译质量
自动翻译
使用 AI 翻译 strings.xml、复数、字符串数组和 Fastlane Supply 元数据。自动翻译应用内字符串和 Play Store 元数据,打造完整的本地化展示。
# 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现已提供 Android Studio 插件
使用适用于 IntelliJ / Android Studio 的 i18n Agent 插件,直接从 IDE 翻译 Android XML 资源。
额外功能:使用 LocaleChain 实现智能语言回退
Android 的资源回退由操作系统控制。pt-BR 译文缺失时,Android 会完全跳过 pt-PT 并显示英语。LocaleChain 会拦截字符串查找并遍历可配置回退链,让区域用户看到最接近的可用译文。
LocaleChain for Android 是开源 Kotlin 库。在 GitHub 上查看
// build.gradle.kts (app module)
dependencies {
implementation("com.i18nagent:locale-chain-android:0.1.0")
}// 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"))
)常见问题
缺少默认字符串会导致崩溃
App Bundle 语言拆分会破坏应用内切换
RTL 布局失效
库资源污染
推荐的文件结构
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。
implementation("com.i18nagent:locale-chain-android:0.1.0")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 →