
Hướng dẫn toàn diện về bản địa hóa ứng dụng Android
Từ strings.xml đến siêu dữ liệu Play Store: bản địa hóa ứng dụng Android bằng Kotlin, Jetpack Compose, Fastlane và công nghệ dịch AI tự động.
Thiết lập dự án Android để bản địa hóa
Android dùng quy ước dựa trên thư mục để bản địa hóa. Các chuỗi mặc định nằm trong res/values/strings.xml còn chuỗi đã dịch nằm trong các thư mục riêng cho từng ngôn ngữ như res/values-de/, res/values-ja/ và các thư mục tương tự.
// 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-rTWTạo strings.xml
Tài nguyên chuỗi Android dùng XML với các phần tử '<string>' bên trong phần tử gốc '<resources>'. Dùng %s cho phần giữ chỗ chuỗi, %d cho số nguyên và %1$s/%2$s cho các đối số theo vị trí để người dịch có thể sắp xếp lại.
<!-- 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>Xử lý số nhiều
Android dùng các phần tử '<plurals>' với thuộc tính số lượng: zero, one, two, few, many, other. Mỗi ngôn ngữ đích có thể cần các danh mục khác nhau: tiếng Ả Rập dùng cả 6, tiếng Nga cần few/many còn tiếng Nhật chỉ dùng 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
) -->Dùng trong mã: Kotlin và Jetpack Compose
Android truyền thống dùng getString(R.string.key) và resources.getQuantityString(). Jetpack Compose dùng stringResource(R.string.key) và pluralStringResource(). Cả hai đều phân giải bản dịch phù hợp theo ngôn ngữ thiết bị trong thời gian chạy.
// 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
))
}Mảng chuỗi và chuỗi đã định dạng
Dùng '<string-array>' cho danh sách có thứ tự (ví dụ: tùy chọn thả xuống, bước làm quen). Dùng đối số định dạng theo vị trí (%1$s, %2$d) trong chuỗi đã định dạng để người dịch có thể sắp xếp lại từ mà không phá vỡ cấu trúc câu.
<!-- 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>Bản địa hóa siêu dữ liệu Google Play bằng Fastlane
Dùng lệnh supply của Fastlane để quản lý siêu dữ liệu Play Store gồm tiêu đề, mô tả ngắn, mô tả đầy đủ và nhật ký thay đổi dưới dạng tệp văn bản thuần được sắp xếp theo ngôn ngữ trong kho lưu trữ.
# 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 supplyTự động bản địa hóa trang thông tin Play Store
Bỏ qua thao tác sao chép và dán thủ công. Dịch tiêu đề, mô tả và ghi chú phát hành trên Play Store sang hơn 175 ngôn ngữ trong khi vẫn tuân thủ giới hạn ký tự.
Khám phá tính năng tích hợp Google PlayKiểm thử bản địa hóa
Kiểm thử bằng cách chuyển đổi ngôn ngữ trên trình giả lập, xem trước Compose với LocaleList tùy chỉnh và dùng ngôn ngữ giả trong Developer Options. Dùng resConfigs trong Gradle để loại bỏ tài nguyên ngôn ngữ không mong muốn khỏi thư viện bên thứ ba.
// 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 mirroringTự động đảm bảo chất lượng bản dịch
Tự động dịch
Dùng AI để dịch strings.xml, số nhiều, mảng chuỗi và siêu dữ liệu Fastlane Supply. Tự động dịch cả chuỗi trong ứng dụng lẫn siêu dữ liệu Play Store để bản địa hóa toàn diện.
# 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Đã có trình bổ trợ Android Studio
Dịch tài nguyên XML của Android ngay trong IDE bằng trình bổ trợ i18n Agent cho IntelliJ / Android Studio.
Nội dung bổ sung: ngôn ngữ dự phòng thông minh với LocaleChain
Hệ điều hành kiểm soát cơ chế tài nguyên dự phòng của Android. Khi thiếu bản dịch pt-BR, Android bỏ qua hoàn toàn pt-PT và hiển thị tiếng Anh. LocaleChain chặn hoạt động tra cứu chuỗi và duyệt qua chuỗi dự phòng có thể cấu hình để người dùng trong khu vực thấy bản dịch gần nhất hiện có.
LocaleChain cho Android là một thư viện Kotlin mã nguồn mở. Xem trên 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"))
)Lỗi thường gặp
Thiếu chuỗi mặc định gây sự cố
Tách ngôn ngữ trong App Bundle làm hỏng tính năng chuyển đổi trong ứng dụng
Bố cục RTL bị hỏng
Tài nguyên thư viện gây nhiễu
Cấu trúc tệp đề xuất
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.ktsDùng thử i18n Agent ngay
Thả tệp bản dịch của bạn vào đây
JSON, YAML, PO, XML, CSV, Markdown, Properties
hoặc nhấp để duyệt
Ngôn ngữ đích
Ngôn ngữ dự phòng với locale-chain-android
Khi thiếu khóa dịch trong một ngôn ngữ khu vực như pt-BR, Android chuyển thẳng đến thư mục tài nguyên mặc định thay vì kiểm tra ngôn ngữ mẹ pt trước.
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"),
)
)Xem Hướng dẫn về ngôn ngữ dự phòng để biết danh sách đầy đủ các framework được hỗ trợ và 75 chuỗi tích hợp sẵn. Learn more →