Skip to content

Instantly share code, notes, and snippets.

@kjlape
Created April 21, 2025 19:22
Show Gist options
  • Save kjlape/f66079e7a797d3ca67eddabc6729050e to your computer and use it in GitHub Desktop.
Save kjlape/f66079e7a797d3ca67eddabc6729050e to your computer and use it in GitHub Desktop.
Gross hack to work around SwiftUI's limited LocalizedStringKey API
@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