I am trying to localize markers shown in my AppleMapView using SwiftUI.
However, MKAnnotation's marker title's type is fixed to String
. And I don't want to inherit or create custom class because it is too bothering.
What I need is just cast LocalizedStringKey to String to set marker's title. Any help on this?
EDIT: This answer has been edited once for cleaner code and a bit better performance in stringKey
.
LocalizedStringKey
has a member called key
which contains the key string which corresponds to the localized string that is in the localization files. We can't access the key directly unfortunately, so we need to workaround getting the 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!
workaround extension, plus an example of how it works, to get the key out of the 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`
now that we have the key as an string, you can easily get the localized string which is pointed to by the key of 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
}
}
to understand this, take a look at https://stackoverflow.com/a/27879342/11837341
now you can easily convert a LocalizedStringKey's value to string:
extension LocalizedStringKey {
func stringValue(locale: Locale = .current) -> String {
return .localizedString(for: self.stringKey, locale: locale)
}
}
TL; DR (Summary)
add these extensions to your project:
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)
}
}
Examples
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`