SwiftUI本地化不起作用只显示键Xcode

ruyhziif  于 2023-08-07  发布在  Swift
关注(0)|答案(2)|浏览(137)

我正在使用Xcode中的SwiftUI开发应用程序,我想将其本地化,但我遇到了一个问题。我已经做了所有步骤从youtube视频
1.在项目->信息->本地化中添加本地化
1.创建了2个本地化文件
1.填充它像“hll”=“你好”;等等。
当我构建一个应用程序时,我只有键而不是值(hll而不是Hello),所以我尝试了Text(“hll”)和Text(LocalizedStringKey(“hll”))。我错过了什么?

6yoyoihd

6yoyoihd1#

确保项目中有Localizable.strings文件。LocalizedStringKey在这里查找键/值作为默认值。
https://developer.apple.com/documentation/swiftui/localizedstringkey
如果要定义其他文件,请使用

Text(LocalizedStringKey, tableName: String?, bundle: Bundle?, comment: StaticString?)

字符串
初始化器和tableName应该匹配您的.strings文件名。

slhcrj9b

slhcrj9b2#

extension Text {
public init<S>(_ text: S) where S : StringProtocol {
    let text = String(text)
    
    let path = Bundle.main.path(forResource: "en", ofType: "lproj")
    let bundle = Bundle(path: path!)
    
    let translated = NSLocalizedString(text, tableName: "Localizable", bundle: bundle!, value: "", comment: "")

    self.init(verbatim: translated)
}

字符串

相关问题