How to implement localization in Swift UI

后端 未结 6 2077
迷失自我
迷失自我 2020-12-09 03:31

Can anybody help me? I can\'t find any description of the localization in Swift UI. Can anyone please give advice or better an example of how to localize for example T

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 04:34

    There is a thing you can do wrong that's not made very clear in any explanation I've seen. It turns out that Text("hello") is only interpreted as a localization key if you pass it a literal. If you pass a variable of type String, this doesn't happen. The answer is instead to declare the variable as type LocalizedStringKey.

      Text("hello") //-> implicitly treats string literal as a key; looks up and displays "Hello World!"
      let cap1:String = "hello"
      Text(cap1)   //-> no lookup for explicit String variable; just displays "hello"
      let cap2:LocalizedStringKey = "hello"
      Text(cap2)   //-> looks up explicit LocalizedStringKey value; displays "Hello World!"
    

提交回复
热议问题