The Util Designer
SwiftUI 帶有參數Localization
xcode 13.4.1, swift 5.5, iOS 15.4
2022-08-19
有時我們需要為我們的App做帶有參數的Localization,關於Localization基本使用可以參考這篇SwiftUI Localization的使用
1. 在這一篇文章這篇SwiftUI Localization的使用之上增加使用Text。
import SwiftUI

struct ContentView : View {
    let numbers = ["one", "two", "three", "four", "five",
    "six", "seven", "eight", "nine", "ten"]
    let name = "Guys"
    var body: some View {
        VStack {
            Text(String(localized: "hello \(name)"))
            List {
                ForEach(numbers, id:\.self) { number in
                    Text(LocalizedStringKey(number))
                }
            }
        }
    }
}
2. 在Localizable (English),增加"hello %@" = "Hello %@!";內容如下:
"one" = "ONE";
"two" = "TWO";
"three" = "THREE";
"four" = "FOUR";
"five" = "FIVE";
"six" = "SIX";
"seven" = "SEVEN";
"eight" = "EIGHT";
"nine" = "NINE";
"ten" = "TEN";
"hello %@" = "Hello %@!";
3. 在Localizable (Chinese, Traditional),增加"hello %@" = "Hello %@!";內容如下:
"one" = "一";
"two" = "二";
"three" = "三";
"four" = "四";
"five" = "五";
"six" = "六";
"seven" = "七";
"eight" = "八";
"nine" = "九";
"ten" = "十";
"hello %@" = "Hello %@!";
4. 重新運行App,就可以得到如下結果: