Currently I am using a Text view manually to localize the title to my shortcut recorder, because the existing initializer only takes a String and isn't localized automatically.
This PR adds a second initializer to Recorder so the caller can continue to use String (as currently supported) or a LocalizedStringKey (as Text does) for localization.
NOTE: This doesn't add any localization burden to this package. This only enabled localization to work properly in the application using this package.
To get the compiler to prefer the LocalizedStringKey initializer instead of the String version, SwiftUI labels the String version of the Text view initializer with @_disfavoredOverload. I copied that attribute directly from SwiftUI.Text's initializer, and it is necessary for this change to work.
(As explained here.)
If using a private attribute isn't acceptable for this package then another option would be to rename the initializer parameter of the LocalizedStringKey version to include 'localizationKey' or something like that. That would require the user to know to use the right version of init, but at least it makes it available, which is better than the status quo.
I also added a title to the previews so the change could be visible in Xcode with just the package open, but the preview doesn't load for me. I tested in my own application and confirmed it works there.
(The SwiftUI.Text LocalizedStringKey initializer also has a compiler attribute, @_semantics, but it is just used for optimizations of some kind so I left it off in the PR. What do you think of including it too? This blog post discusses how it is used.)
Currently I am using a Text view manually to localize the title to my shortcut recorder, because the existing initializer only takes a String and isn't localized automatically.
This PR adds a second initializer to Recorder so the caller can continue to use String (as currently supported) or a LocalizedStringKey (as Text does) for localization.
NOTE: This doesn't add any localization burden to this package. This only enabled localization to work properly in the application using this package.
To get the compiler to prefer the LocalizedStringKey initializer instead of the String version, SwiftUI labels the String version of the Text view initializer with
@_disfavoredOverload
. I copied that attribute directly from SwiftUI.Text's initializer, and it is necessary for this change to work. (As explained here.)If using a private attribute isn't acceptable for this package then another option would be to rename the initializer parameter of the LocalizedStringKey version to include 'localizationKey' or something like that. That would require the user to know to use the right version of init, but at least it makes it available, which is better than the status quo.
I also added a title to the previews so the change could be visible in Xcode with just the package open, but the preview doesn't load for me. I tested in my own application and confirmed it works there.
(The SwiftUI.Text LocalizedStringKey initializer also has a compiler attribute,
@_semantics
, but it is just used for optimizations of some kind so I left it off in the PR. What do you think of including it too? This blog post discusses how it is used.)