Created
April 21, 2025 19:22
-
-
Save kjlape/f66079e7a797d3ca67eddabc6729050e to your computer and use it in GitHub Desktop.
Gross hack to work around SwiftUI's limited LocalizedStringKey API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@propertyWrapper | |
struct LocalizedString { | |
let localizedKeyValue: LocalizedStringKey | |
let stringValue: String | |
// This initializer handles string literals | |
init(wrappedValue: LocalizedStringKey) { | |
self.localizedKeyValue = wrappedValue | |
let mirror = Mirror(reflecting: wrappedValue) | |
self.stringValue = if let keyValue = mirror.children.first(where: { $0.label == "key" })?.value as? String { | |
keyValue | |
} else { | |
String(describing: wrappedValue) | |
} | |
} | |
// When used directly, it provides the LocalizedKey for SwiftUI | |
var wrappedValue: LocalizedStringKey { | |
return localizedKeyValue | |
} | |
// Use $ prefix to access the string value | |
var projectedValue: String { | |
return stringValue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment