Skip to content
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

Merged
merged 4 commits into from Jan 14, 2024

Conversation

rtharston
Copy link
Contributor

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

@@ -151,18 +174,18 @@ extension KeyboardShortcuts.Recorder {

@available(macOS 10.15, *)
#Preview {
KeyboardShortcuts.Recorder(for: .init("xcodePreview"))
KeyboardShortcuts.Recorder("record_shortcut", name: .init("xcodePreview"))
Copy link
Contributor Author

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
Copy link
Contributor Author

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,
Copy link
Contributor Author

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.

@sindresorhus sindresorhus merged commit 8964a7d into sindresorhus:main Jan 14, 2024
1 check passed
@rtharston
Copy link
Contributor Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants