Reputation: 871
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?
Upvotes: 51
Views: 35055
Reputation: 606
You can do it simply by using: String(localized: "YOUR_LOCALIZED_KEY")
If your localized key is programmatically generated, you must store it in a variable that conforms to "LocalizationValue" like this:
let localizedKey = String.LocalizationValue(stringLiteral: yourLocalizedVar)
Then you can use String(localized: localizedKey)
to get your localized text content.
Mahdi BM's solution has a problem because Swift returns only the language code even if you use variants of a language like Spanish, Portuguese, and so many others. The language code for both examples will always return ES and PT, but the names of folders with the localized keys will be different: PT can be 'pt-PT' or 'pt-BR', Spanish can be 'es' or 'es-419' (Latin America) and these cases will cause your app to crash. Locale is also a terrible idea to get this reference because it's composed by both current language and region, both can be from different places, example, if the user is from Japan but is in the US, the user's locale will very likely be jp_US@japaneseCalendar.
Upvotes: 43
Reputation: 11416
import Foundation
import SwiftUI
@available(macOS 10.15, *)
public extension LocalizedStringKey {
private var keyStr: String {
return "\(self)".keyFromLocalizedStringKey
}
func localizedStr(locale: Locale = .current) -> String {
if #available(macOS 12, *) {
return String(localized: "\(self.keyStr)")
} else {
return String.localizedString(for: self.keyStr, locale: locale)
}
}
}
fileprivate extension String {
static func localizedString(for key: String, locale: Locale = .current) -> String {
let language = locale.languageCode
guard let path = Bundle.main.path(forResource: language, ofType: "lproj"),
let bundle = Bundle(path: path)
else { return "Failed To Get Localized String" }
let localizedString = NSLocalizedString(key, bundle: bundle, comment: "")
return localizedString
}
var keyFromLocalizedStringKey: String {
let comp2 = self.substring(from: 25).components(separatedBy:"\", hasFormatting")
if comp2.count == 2 {
return comp2.first!
}
return "failed to get stringKey"
}
}
public extension String {
func substring(from: Int) -> String {
return self[min(from, count) ..< count]
}
}
usage:
let lsk = LocalizedStringKey("KEY_NAME_HERE")
let key = lsk.keyStr
let translatedToCurrLocale = lsk.localizedStr()
Upvotes: -1
Reputation: 71
I am currently using this, but I would like something official and documented. The deployment target is iOS 14
extension String {
func slice(start: String, end: String) -> String? {
(range(of: start)?.upperBound).flatMap { substringFrom in
(range(of: end, range: substringFrom..<endIndex)?.lowerBound).map { substringTo in
String(self[substringFrom..<substringTo])
}
}
}
}
extension LocalizedStringKey {
/// Extracts the string value from LocalizedStringKey.
var string: String {
"\(self)".slice(start: "LocalizedStringKey(key: \"", end: "\", hasFormatting") ?? "Bad string"
}
}
Upvotes: 0
Reputation: 4940
I modified the top answer to compile on Xcode14, remove forced unwraps and to return the key when a translation wasn't found:
extension LocalizedStringKey {
var stringKey: String? {
Mirror(reflecting: self).children.first(where: { $0.label == "key" })?.value as? String
}
func stringValue(locale: Locale = .current) -> String? {
guard let stringKey = self.stringKey else { return nil }
let language = locale.languageCode
guard let path = Bundle.main.path(forResource: language, ofType: "lproj") else { return stringKey }
guard let bundle = Bundle(path: path) else { return stringKey }
let localizedString = NSLocalizedString(stringKey, bundle: bundle, comment: "")
return localizedString
}
}
Upvotes: 4
Reputation: 2104
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`
Upvotes: 33
Reputation: 359
Add an extension to the string to read the localized language
extension String {
func localized() -> String {
let path = Bundle.main.path(forResource: "your language", ofType: "lproj")!
if let bundle = Bundle(path: path) {
let str = bundle.localizedString(forKey: self, value: nil, table: nil)
return str
}
return ""
}
}
Use LocalizedStringKey to load sample code
let title: String = "LocalizedStringKey".localized()
Upvotes: -4
Reputation: 432
You can use NSLocalizedString.
let localizedString = NSLocalizedString("LOCALIZED-STRING-KEY", comment: "Describe what is being localized here")
Upvotes: 6