
iOS 應用程式本地化完整指南
從 Localizable.strings 到 App Store 元數據:使用 Xcode、SwiftUI、Fastlane 和自動化 AI 翻譯來本地化 iOS 應用程式。
在 Xcode 中啟用本地化
打開 Xcode 項目設定,前往 Info > Localizations,然後新增要支援的語言。Xcode 會自動為每種語言建立 .lproj 目錄。
// In Xcode:
// 1. Select your project in the navigator
// 2. Go to Info tab > Localizations
// 3. Click + to add languages (e.g., German, Japanese)
// 4. Select which files to localize
//
// Xcode creates .lproj directories automatically:
// en.lproj/Localizable.strings
// de.lproj/Localizable.strings
// ja.lproj/Localizable.strings建立 Localizable.strings
標準 iOS 本地化檔案使用等號分隔的鍵值對,每行以分號結束。請將來源語言檔案放入 Base.lproj 資料夾。
// Base.lproj/Localizable.strings
"welcome_title" = "Welcome to MyApp";
"login_button" = "Sign In";
"settings_label" = "Settings";
"greeting" = "Hello, %@!"; // %@ = string placeholder
"item_count" = "%d items"; // %d = integer placeholder// ❌ Common mistakes in .strings files:
// Missing semicolon — file loads but translations are empty
"welcome_title" = "Welcome"
// Unescaped quotes — causes parse error
"message" = "Click "here" to continue";
// ✅ Correct versions:
"welcome_title" = "Welcome";
"message" = "Click \"here\" to continue";遷移到 String Catalog(Xcode 15+)
String Catalog(.xcstrings)是 Apple 對 .strings 檔案的現代替代方案,提供 Xcode 視覺化編輯器、從 SwiftUI 視圖自動提取字串以及內置複數支援。
// Xcode 15+ String Catalog (Localizable.xcstrings)
// Xcode automatically extracts strings from your code
// and manages translations in a visual editor.
// In SwiftUI, strings are automatically localizable:
Text("Welcome to MyApp")
Text("Hello, \(userName)!")
// Mark strings explicitly:
let title = String(localized: "welcome_title")在 SwiftUI 和 UIKit 中使用本地化字串
SwiftUI 的 Text 視圖會自動本地化字串字面量,UIKit 則使用 NSLocalizedString。iOS 16+ 可使用現代 String(localized:comment:) API,以更簡潔的語法獲得內置編譯器支援。
import SwiftUI
struct ContentView: View {
let userName: String
var body: some View {
VStack {
// ✅ SwiftUI auto-localizes string literals
Text("welcome_title")
// ⚠️ This does NOT localize (String interpolation)
// Text("Hello, \(userName)")
// ✅ Use String(localized:) for dynamic strings
Text(String(localized: "greeting \(userName)"))
// ✅ UIKit style (works everywhere)
let title = NSLocalizedString(
"settings_label",
comment: "Settings screen title"
)
// ✅ Modern API (iOS 16+)
let modern = String(
localized: "welcome_title",
comment: "Main screen title"
)
}
}
}處理複數
iOS 使用 .stringsdict 檔案處理複數規則,並支援全部 CLDR 複數類別:zero、one、two、few、many、other。String Catalog 可通過 Xcode 的視覺化編輯器處理複數,比手寫 stringsdict XML 簡單得多。
<!-- Localizable.stringsdict -->
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>items_count</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@count@</string>
<key>count</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>zero</key>
<string>No items</string>
<key>one</key>
<string>%d item</string>
<key>other</key>
<string>%d items</string>
</dict>
</dict>
</dict>
</plist>
// Usage in Swift:
String(format: NSLocalizedString("items_count", comment: ""),
itemCount)使用 Fastlane 本地化 App Store 元數據
使用 Fastlane 的 deliver 工具,將 App Store 元數據,包括應用程式名稱、副標題、描述、關鍵詞和發佈說明,以按語言組織的純文字檔案形式納入儲存庫版本控制。
# Install Fastlane
$ gem install fastlane
# Initialize deliver for App Store metadata
$ fastlane deliver init
# Directory structure created:
# fastlane/metadata/
# ├── en-US/
# │ ├── name.txt # App name (30 chars)
# │ ├── subtitle.txt # Subtitle (30 chars)
# │ ├── description.txt # Full description
# │ ├── keywords.txt # Search keywords (100 chars)
# │ ├── release_notes.txt # What's New
# │ └── promotional_text.txt
# ├── de-DE/
# │ └── ...
# └── ja/
# └── ...
# Push metadata to App Store Connect:
$ fastlane deliver測試本地化
無需更改設備語言即可測試本地化內容。使用 Xcode scheme 覆蓋以任意語言執行應用程式,通過語言環境預覽 SwiftUI,並為 XCUITest 提供啟動參數以執行自動化測試。
// 1. Xcode Scheme Override:
// Edit Scheme > Run > Options > App Language > Choose language
// 2. SwiftUI Preview with locale:
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environment(\.locale, Locale(identifier: "de"))
ContentView()
.environment(\.locale, Locale(identifier: "ja"))
ContentView()
.environment(\.locale, Locale(identifier: "ar"))
}
}
// 3. XCUITest with language override:
let app = XCUIApplication()
app.launchArguments += ["-AppleLanguages", "(de)"]
app.launchArguments += ["-AppleLocale", "de_DE"]
app.launch()自動保證翻譯質素
自動翻譯
使用 AI 翻譯 .strings、.xcstrings 和 Fastlane 元數據檔案。自動翻譯應用程式內字串和 App Store 元數據,打造完整的本地化展示。
# Translate .strings files
> Translate Base.lproj/Localizable.strings
to Japanese, German, and Spanish
# Translate App Store metadata too
> Translate fastlane/metadata/en-US/
to de-DE, ja, es-MX
✓ 6 files translated in 3.2s額外功能:使用 LocaleChain 實現智能語言回退
預設情況下,如果用戶的確切語言不可用,iOS 會回退到開發語言。只有 pt-PT 譯文時,pt-BR 用戶會看到英語而非葡萄牙語。LocaleChain 通過可設定回退鏈修復此問題。
LocaleChain 是開源 Swift Package。在 GitHub 上查看
// Swift Package Manager
// File > Add Package Dependencies >
// https://github.com/i18n-agent/ios-localechain.gitimport LocaleChain
// In your App init or AppDelegate:
LocaleChain.configure() // Activates all default chains
// 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: ["es-MX": ["es-419", "es"]]
)常見問題
.strings 檔案語法錯誤
SwiftUI Text 插值未本地化
小元件或擴展顯示原始鍵
缺失譯文在生產環境中顯示鍵
推薦的檔案結構
MyApp/
├── MyApp.xcodeproj
├── MyApp/
│ ├── Base.lproj/
│ │ ├── Localizable.strings # Source strings
│ │ └── Localizable.stringsdict # Plural rules
│ ├── en.lproj/
│ │ └── Localizable.strings
│ ├── de.lproj/
│ │ └── Localizable.strings
│ ├── ja.lproj/
│ │ └── Localizable.strings
│ ├── Localizable.xcstrings # OR String Catalog
│ └── Info.plist
├── MyAppTests/
├── fastlane/
│ ├── Fastfile
│ └── metadata/
│ ├── en-US/
│ │ ├── name.txt
│ │ ├── description.txt
│ │ └── keywords.txt
│ ├── de-DE/
│ └── ja/
└── Package.swift立即試用 i18n Agent
將翻譯檔案拖放到此處
JSON, YAML, PO, XML, CSV, Markdown, Properties
或點擊選擇檔案
目標語言
使用 ios-localechain 實現語言回退
如果 de-AT 等區域語言缺少翻譯鍵,iOS 會直接跳到開發語言,而不會先檢查父語言 de。
// Swift Package Manager
// https://github.com/i18n-agent/ios-localechainimport LocaleChain
LocaleChain.configure(overrides: [
"de": ["en-GB", "en"],
"pt-BR": ["pt", "en"],
"zh-Hant-HK": ["zh-Hant", "zh", "en"],
])查看語言回退指南,瞭解受支援框架的完整列表和 75 條內置回退鏈。 Learn more →