iOS 国际化与本地化完整指南

一、基础概念

1. 术语区分

  • 国际化 (Internationalization, i18n): 让应用支持多语言的设计过程

  • 本地化 (Localization, l10n): 将应用适配到特定语言/地区的过程

  • Locale : 语言+地区的组合,如 zh_CN, en_US

二、项目配置

添加支持的语言

复制代码
1. 项目设置 → Project → Info → Localizations
  点击 + 号添加语言(如 Chinese (Simplified))
复制代码
 2. 或者通过 Info.plist 文件
XML 复制代码
<key>CFBundleLocalizations</key>
<array>
    <string>en</string>
    <string>zh-Hans</string>
    <string>ja</string>
</array>

三、文本国际化

创建 Localizable.strings 文件

复制代码
1. File → New → File → Strings File
2. 命名为 "Localizable"
3. 选中文件,在右侧 File Inspector 中点击 Localize...
4. 选择要支持的语言

各语言版本内容

Localizable.strings (English - Base)
*

XML 复制代码
// 基础用法
"welcome_message" = "Welcome to our App!";
"login_button" = "Log In";
"logout_button" = "Log Out";

// 带参数的字符串
"welcome_user" = "Welcome, %@!";
"items_count" = "You have %d items";
"price_format" = "Price: $%.2f";

// 复数形式
"apple_count" = "%d apple";
"apple_count_plural" = "%d apples";

Localizable.strings (Chinese)
*

XML 复制代码
"welcome_message" = "欢迎使用我们的应用!";
"login_button" = "登录";
"logout_button" = "退出登录";

"welcome_user" = "欢迎, %@!";
"items_count" = "您有 %d 个项目";
"price_format" = "价格: ¥%.2f";

"apple_count" = "%d 个苹果";
"apple_count_plural" = "%d 个苹果";

在代码中使用

Swift 复制代码
// 基本用法
let welcome = NSLocalizedString("welcome_message", comment: "欢迎消息")
let loginText = NSLocalizedString("login_button", comment: "登录按钮")

// 带参数的用法
let userName = "张三"
let welcomeUser = String(format: NSLocalizedString("welcome_user", comment: ""), userName)

let itemCount = 5
let itemsText = String(format: NSLocalizedString("items_count", comment: ""), itemCount)

三、 相关应用

监听语言变化

Swift 复制代码
// 监听语言变化通知
NotificationCenter.default.addObserver(
    self,
    selector: #selector(languageChanged),
    name: .languageChanged,
    object: nil
)
  • 查看当前本地化状态
Swift 复制代码
 func printLocalizationInfo() {
        print("=== 本地化调试信息 ===")
        print("首选语言: \(Locale.preferredLanguages)")
        print("当前区域: \(Locale.current.identifier)")
        print("当前语言: \(Bundle.main.preferredLocalizations.first ?? "未知")")
        
        // 测试本地化字符串
        let testKey = "welcome_message"
        let localized = NSLocalizedString(testKey, comment: "")
        print("测试键 '\(testKey)': \(localized)")
        
        // 检查所有支持的本地化
        if let bundlePath = Bundle.main.path(forResource: "Localizable", ofType: "strings") {
            print("本地化文件路径: \(bundlePath)")
        }
        
        // 列出所有可用的本地化
        if let localizations = Bundle.main.localizations {
            print("支持的本地化: \(localizations)")
        }
    }

注意事项

  • 资源文件的命名 :确保你的图片、XIB 文件等资源的命名也支持国际化。例如,可以使用 Image.png, Image-zh-Hans.png, Image-zh-Hant.png, 和 Image-en.png

  • 重新启动应用:动态更改语言后,可能需要重新启动应用程序才能完全生效。这是因为某些 UI 元素(如 UITabBarItem 的标题)在初始化后不会自动更新。

  • 测试:确保在真实设备和模拟器上测试所有支持的语言,以验证国际化是否正确。

相关推荐
末代iOS程序员华仔19 小时前
Cursor + GitOps:自动化运维新姿势
flutter·ios·swift
HarderCoder1 天前
SwiftUI 可复用视图设计解剖:从语义到 API 的完整实践
swiftui·swift
东坡肘子1 天前
当灵感跑在了结果前面 -- 肘子的 Swift 周报 #145
人工智能·swiftui·swift
世界尽头与你1 天前
iOS 越狱检测原理
安全·网络安全·ios·信息安全·渗透测试
HarderCoder1 天前
Swift 静态分派与动态分派:方法到底是如何被调用的
swift
_瑞2 天前
AI Coding 那么快,为什么还需要 SDD?
人工智能·ios·ai编程
轩辕十四leo2 天前
Astrolabe(星盘):让 AI 看见自己写出的 UI
ios·ai编程
白玉cfc2 天前
熟悉Objective-C
开发语言·ios·objective-c
星辰即远方2 天前
字符串合法性检验
macos·ios·cocoa
末代iOS程序员华仔3 天前
KMP全栈开发:从Android到AI Agent的技术演进与实践
flutter·ios·figma