ios - 如何在 SwiftUI 中将 LocalizedStringKey 更改为 String

标签 ios swift xcode swiftui

我正在尝试使用 SwiftUI 本地化 AppleMapView 中显示的标记。

但是,MKAnnotation 的标记标题类型固定为 String .而且我不想继承或创建自定义类,因为它太麻烦了。

我需要的只是将 LocalizedStringKey 转换为字符串 设置标记的标题。对此有什么帮助吗?

最佳答案

编辑:此答案已被编辑一次,以获得更简洁的代码和更好的性能 stringKey . LocalizedStringKey有一个成员叫 key其中包含与本地化文件中的本地化字符串相对应的 key 字符串。不幸的是,我们无法直接访问 key ,因此我们需要解决获取 key 的方法。

// An Example that won't work:
let localizedKey = LocalizedStringKey.init("SOME_LOCALIZED_KEY_HERE")

localizedKey.key // ERRROOOOORR! `key` is an internal member of `LocalizedStringKey` and you can't access it! 
解决方法扩展,以及它如何工作的示例,以从 LocalizedStringKey 中获取 key :
extension LocalizedStringKey {

    // This will mirror the `LocalizedStringKey` so it can access its 
    // internal `key` property. Mirroring is rather expensive, but it 
    // should be more than fine performance-wise, unless you are  
    // using it too much or doing something out of the norm.
    var stringKey: String? {
        Mirror(reflecting: self).children.first(where: { $0.label == "key" })?.value
    }

// An Example:
let localizedKey = LocalizedStringKey("KEY_HERE")
print(localizedKey.stringkey)
// prints `KEY_HERE`
现在我们将键作为字符串,您可以轻松获取 LocalizedStringKey 的键所指向的本地化字符串。
extension String {
    static func localizedString(for key: String,
                                locale: Locale = .current) -> String {
        
        let language = locale.languageCode
        let path = Bundle.main.path(forResource: language, ofType: "lproj")!
        let bundle = Bundle(path: path)!
        let localizedString = NSLocalizedString(key, bundle: bundle, comment: "")
        
        return localizedString
    }
}
要了解这一点,请查看 https://stackoverflow.com/a/27879342/11837341
现在您可以轻松地将 LocalizedStringKey 的值转换为字符串:
extension LocalizedStringKey {
func stringValue(locale: Locale = .current) -> String {
        return .localizedString(for: self.stringKey, locale: locale)
    }
}

TL;博士(摘要)
将这些扩展添加到您的项目中:
extension LocalizedStringKey {
    var stringKey: String? {
        Mirror(reflecting: self).children.first(where: { $0.label == "key" })?.value
    }
}

extension String {
    static func localizedString(for key: String,
                                locale: Locale = .current) -> String {
        
        let language = locale.languageCode
        let path = Bundle.main.path(forResource: language, ofType: "lproj")!
        let bundle = Bundle(path: path)!
        let localizedString = NSLocalizedString(key, bundle: bundle, comment: "")
        
        return localizedString
    }
}

extension LocalizedStringKey {
    func stringValue(locale: Locale = .current) -> String {
        return .localizedString(for: self.stringKey, locale: locale)
    }
}
示例
let localizedKey = LocalizedStringKey("KEY_NAME_HERE")

print(localizedKey.stringKey)
//prints `KEY_NAME_HERE`

print(localizedKey.stringValue())
// prints Localized value of `KEY_NAME_HERE`
// DOESNT print `KEY_NAME_HERE`

关于ios - 如何在 SwiftUI 中将 LocalizedStringKey 更改为 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60841915/

相关文章:

ios - 在实体中找不到键路径 <transientproperty>

ios - 需要帮助使用 AFnetworking 异步下载文件取得进展

json - Swift 函数在循环时不一致地返回 API 调用

ios - 如何禁用键盘而不将其隐藏在 iOS 中?

objective-c - 在 Xcode 中,我如何知道哪个类实现了协议(protocol)?

Swift ImagePicker 在 AutoLayout 约束上抛出 SIGNAL SIGABRT

ios - 强制 iPad 模拟器打开到 100%

ios - 在 Swift 中的整数后显示 .0

objective-c - 使用 Xcode 学习 objective-c?

ios - "Unable to Validate your Application Error"上传新版iOS App时