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 all 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
29 changes: 26 additions & 3 deletions Sources/KeyboardShortcuts/Recorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ extension KeyboardShortcuts.Recorder<Text> {
- 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.
*/
public init(
_ 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
) {
self.init(
for: name,
onChange: onChange,
hasLabel: true
) {
Text(title)
}
}
}

@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
public init(
_ title: String,
name: KeyboardShortcuts.Name,
Expand Down Expand Up @@ -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.

.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