
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 →