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 {
// imagine `self` is equal to LocalizedStringKey("KEY_HERE")
var stringKey: String {
let description = "\(self)"
// in this example description will be `LocalizedStringKey(key: "KEY_HERE", hasFormatting: false, arguments: [])`
// for more clarity, `let description = "\(self)"` will have no differences
// compared to `let description = "\(LocalizedStringKey(key: "KEY_HERE", hasFormatting: false, arguments: []))"` in this example.
let components = description.components(separatedBy: "key: \"")
.map { $0.components(separatedBy: "\",") }
// here we separate the string by its components.
// in `LocalizedStringKey(key: "KEY_HERE", hasFormatting: false, arguments: [])`
// our key lays between two strings which are `key: "` and `",`.
// if we manage to get what is between `key: "` and `",`, that would be our Localization Key
// which in this example is `KEY_HERE`
return components[1][0]
// by trial, we know that `components[1][0]` will always be our localization Key
// which is `KEY_HERE` in this example.
}
// 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; DR(摘要)
将这些扩展添加到您的项目中:
extension LocalizedStringKey {
var stringKey: String {
let description = "\(self)"
let components = description.components(separatedBy: "key: \"")
.map { $0.components(separatedBy: "\",") }
return components[1][0]
}
}
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`