sindresorhus / KeyboardShortcuts

⌨️ Add user-customizable global keyboard shortcuts (hotkeys) to your macOS app in minutes
https://swiftpackageindex.com/sindresorhus/KeyboardShortcuts/documentation/keyboardshortcuts/keyboardshortcuts
MIT License
1.94k stars 181 forks source link

Add LocalizedStringKey initializer to Recorder to support localizing the title #161

Closed rtharston closed 8 months ago

rtharston commented 8 months ago

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.

Screenshot 2024-01-12 at 4 42 33 PM

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.)

Screenshot 2024-01-12 at 4 45 25 PM
rtharston commented 8 months ago

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!