我正在尝试使用AppleMapView来本地化SwiftUI中显示的标记。
然而,MKAn表示法的标记标题的类型是固定在String
。我不想继承或创建自定义类,因为它太麻烦了。
我需要的只是强制转换LocalizedStringKey来字符串来设置标记的标题。在这方面有什么帮助吗?
发布于 2020-09-18 08:01:29
编辑:这个答案已经编辑过一次,以获得更清晰的代码,并在stringKey
**.**中获得更好的性能。
LocalizedStringKey
有一个名为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中获取密钥:
extension LocalizedStringKey {
// This will mirror the `LocalizedStringKey` so it can access its
// internal `key` property. Mirroring is rather expensive, but it
// should be 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 as? String
}
}
// 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的值转换为string:
extension LocalizedStringKey {
func stringValue(locale: Locale = .current) -> String {
return .localizedString(for: self.stringKey, locale: locale)
}
}
TL;DR (概述)
将这些扩展添加到项目中:
extension LocalizedStringKey {
var stringKey: String? {
Mirror(reflecting: self).children.first(where: { $0.label == "key" })?.value as? String
}
}
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`
发布于 2022-04-16 19:15:55
只需使用:String(localized: "YOUR_LOCALIZED_KEY")
即可完成此操作。
如果您的本地化键是以编程方式生成的,则必须将其存储在符合"LocalizationValue“的变量中,如下所示:
let localizedKey = String.LocalizationValue(stringLiteral: yourLocalizedVar)
然后可以使用String(localized: localizedKey)
获取本地化文本内容。
Mahdi BM的解决方案有一个问题,因为Swift只返回语言代码,即使您使用西班牙语、葡萄牙语等多种语言的变体。这两个示例的语言代码总是返回ES和PT,但是带有本地化键的文件夹的名称将有所不同: PT可以是'pt-PT‘或'pt-BR',西班牙语可以是'es’或'es-419‘(拉丁美洲),这些情况会导致应用程序崩溃。
发布于 2020-07-20 22:59:30
您可以使用NSLocalizedString。
let localizedString = NSLocalizedString("LOCALIZED-STRING-KEY", comment: "Describe what is being localized here")
https://stackoverflow.com/questions/60841915
复制相似问题