Darron Schall
06/28/2023, 1:44 PMextension StringResource {
func localized() -> String {
return desc().localized()
}
}
extension Image {
init(resourceKey: KeyPath<MR.images, ImageResource>) {
self.init(uiImage: MR.images()[keyPath: resourceKey].toUIImage()!)
}
}
extension Text {
init(resourceKey: KeyPath<MR.strings, StringResource>) {
self.init(MR.strings()[keyPath: resourceKey].localized())
}
}
extension Button where Label == Text {
init(resourceKey: KeyPath<MR.strings, StringResource>, action: @escaping () -> Void) {
self.init(MR.strings()[keyPath: resourceKey].localized(), action: action)
}
init(resourceKey: KeyPath<MR.strings, StringResource>, role: ButtonRole?, action: @escaping () -> Void) {
self.init(MR.strings()[keyPath: resourceKey].localized(), role: role, action: action)
}
}
This makes the SwiftUI read nicely, allowing you to just reference the keys, like this:
Image(resourceKey: \.logo)
Button(resourceKey: \.menu__logout, role: .destructive) {
viewModel.logout()
}
Text(resourceKey: \.welcome__body)
alex009
06/28/2023, 3:59 PMDarron Schall
06/28/2023, 5:00 PM