在 SwiftUI-国际化一文中,我们详细介绍了国际化的内容。在 Xcode 15 之后,Apple 提供了一种新的国际化方式,通过引入String Catalog
,使得处理国际化更加高效与便捷。
String Catelog
,必须命名为InfoPlist.xcstrings
。String Catelog
,必须命名为Localizable.xcstrings
。xcstrings
文件提供了可视化的编辑界面,并且会显示每一种语言的国际化完成比例。xcstrings
文件。Localizable.xcstrings
。import SwiftUI
struct ContentView: View {
let temperature = 10
var body: some View {
VStack {
// 纯文本
Text(String(localized: "title", defaultValue: "Kindly Reminder"))
// 自定义View
MessageView(String(localized: "message", defaultValue: "Weather Information"))
// 插值
Text(String(localized: "weather",
defaultValue: "Weather is \(String(localized: "localizedWeather", defaultValue: "Sunny"))"))
Text(String(localized: "temperature",
defaultValue: "Temperature is \(temperature) ℃"))
}
.padding()
}
}
struct MessageView: View {
let message: String
init(_ message: String) {
self.message = message
}
var body: some View {
Text(message)
}
}
xcstrings
文件。