-
-
Notifications
You must be signed in to change notification settings - Fork 186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add LocalizedStringKey initializer to Recorder to support localizing the title #161
Conversation
@@ -151,18 +174,18 @@ extension KeyboardShortcuts.Recorder { | |||
|
|||
@available(macOS 10.15, *) | |||
#Preview { | |||
KeyboardShortcuts.Recorder(for: .init("xcodePreview")) | |||
KeyboardShortcuts.Recorder("record_shortcut", name: .init("xcodePreview")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used the key of an existing string so the preview wouldn't require a new string to be localized.
- Parameter name: Strongly-typed keyboard shortcut name. | ||
- Parameter onChange: Callback which will be called when the keyboard shortcut is changed/removed by the user. This can be useful when you need more control. For example, when migrating from a different keyboard shortcut solution and you need to store the keyboard shortcut somewhere yourself instead of relying on the built-in storage. However, it's strongly recommended to just rely on the built-in storage when possible. | ||
*/ | ||
@_disfavoredOverload |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the description for an explanation of @_disfavoredOverload
.
@@ -114,7 +114,7 @@ extension KeyboardShortcuts.Recorder<Text> { | |||
- Parameter onChange: Callback which will be called when the keyboard shortcut is changed/removed by the user. This can be useful when you need more control. For example, when migrating from a different keyboard shortcut solution and you need to store the keyboard shortcut somewhere yourself instead of relying on the built-in storage. However, it's strongly recommended to just rely on the built-in storage when possible. | |||
*/ | |||
public init( | |||
_ title: String, | |||
_ title: LocalizedStringKey, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If @_disfavoredOverload
can't be used in this package, this line can be changed to something like: key title: LocalizedStringKey
to remove the conflict between this init and the init that accepts StringProtocol.
Oops. Sorry about the wrong spacing. I was careful about the lines I made changes on, but forgot to check that whole block. Thanks for fixing it! |
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.)