0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Picker内の選択肢をDictionaryにしたときのLocalization

Posted at

Textについてはこちらの記事のようにLocalizable.stringsに記述すればかんたんに対応できます。

でもPickerの中のDictionaryを使ってるところでは、上記の方法ではうまくいきませんでした。

Localeを使って場合分けしたらうまくいきました。

    func firstLang() -> String {
            let prefLang = Locale.preferredLanguages.first
            return prefLang!
    }

で、こう。

    Picker(selection: $name, label: Text("name"), content: {
        ForEach(1..<3, id: \.self){i in
             if firstLang().hasPrefix("ja"){
                   Text(NumToNameJP[i] ?? "").tag(i)
              } else{
                   Text(NumToName[i] ?? "").tag(i)
              }
         }
    })
let NumToPitchJP: [Int: String] = [
    1: "うさぎ",
    2: "かめ",
    3: "ねずみ"
]

let NumToPitchJP: [Int: String] = [
    1: "rabbit",
    2: "turtle",
    3: "mouse"
]

参考
https://capibara1969.com/2120/
https://medium.com/@dkmczk/swift3-端末の表示言語を読み取り-言語ごとに分岐する方法まとめ-ios-d0d3f92f9bcc

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?