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
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions Sources/KeyboardShortcuts/Recorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
- 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.

name: KeyboardShortcuts.Name,
onChange: ((KeyboardShortcuts.Shortcut?) -> Void)? = nil
) {
Expand All @@ -128,6 +128,29 @@
}
}

@available(macOS 10.15, *)
extension KeyboardShortcuts.Recorder<Text> {
/**
- Parameter title: The title of the keyboard shortcut recorder, describing its purpose.
- 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.

public init<S>(
_ title: S,
rtharston marked this conversation as resolved.
Show resolved Hide resolved
name: KeyboardShortcuts.Name,
onChange: ((KeyboardShortcuts.Shortcut?) -> Void)? = nil
) where S : StringProtocol {

Check warning on line 143 in Sources/KeyboardShortcuts/Recorder.swift

View workflow job for this annotation

GitHub Actions / lint

Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)
self.init(
for: name,
onChange: onChange,
hasLabel: true
) {
Text(title)
}
}
}

@available(macOS 10.15, *)
extension KeyboardShortcuts.Recorder {
/**
Expand All @@ -151,18 +174,18 @@

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

.environment(\.locale, .init(identifier: "en"))
}

@available(macOS 10.15, *)
#Preview {
KeyboardShortcuts.Recorder(for: .init("xcodePreview"))
KeyboardShortcuts.Recorder("record_shortcut", name: .init("xcodePreview"))
.environment(\.locale, .init(identifier: "zh-Hans"))
}
@available(macOS 10.15, *)
#Preview {
KeyboardShortcuts.Recorder(for: .init("xcodePreview"))
KeyboardShortcuts.Recorder("record_shortcut", name: .init("xcodePreview"))
.environment(\.locale, .init(identifier: "ru"))
}
#endif