-
Notifications
You must be signed in to change notification settings - Fork 160
Add support for LocalizedStringResource
#315
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
Open
connor-ricks
wants to merge
2
commits into
pointfreeco:main
Choose a base branch
from
connor-ricks:task/add-localizedstringresource-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+118
−12
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,9 +119,10 @@ public struct TextState: Equatable, Hashable, Sendable { | |
fileprivate enum Storage: Equatable, Hashable, @unchecked Sendable { | ||
indirect case concatenated(TextState, TextState) | ||
#if canImport(SwiftUI) | ||
case localized( | ||
case localizedStringKey( | ||
LocalizedStringKey, tableName: String?, bundle: Bundle?, comment: StaticString?) | ||
#endif | ||
case localizedStringResource(LocalizedStringResourceBox) | ||
case verbatim(String) | ||
|
||
static func == (lhs: Self, rhs: Self) -> Bool { | ||
|
@@ -130,7 +131,7 @@ public struct TextState: Equatable, Hashable, Sendable { | |
return l1 == r1 && l2 == r2 | ||
|
||
#if canImport(SwiftUI) | ||
case let (.localized(lk, lt, lb, lc), .localized(rk, rt, rb, rc)): | ||
case let (.localizedStringKey(lk, lt, lb, lc), .localizedStringKey(rk, rt, rb, rc)): | ||
return lk.formatted(tableName: lt, bundle: lb, comment: lc) | ||
== rk.formatted(tableName: rt, bundle: rb, comment: rc) | ||
#endif | ||
|
@@ -139,8 +140,8 @@ public struct TextState: Equatable, Hashable, Sendable { | |
return lhs == rhs | ||
|
||
#if canImport(SwiftUI) | ||
case let (.localized(key, tableName, bundle, comment), .verbatim(string)), | ||
let (.verbatim(string), .localized(key, tableName, bundle, comment)): | ||
case let (.localizedStringKey(key, tableName, bundle, comment), .verbatim(string)), | ||
let (.verbatim(string), .localizedStringKey(key, tableName, bundle, comment)): | ||
return key.formatted(tableName: tableName, bundle: bundle, comment: comment) == string | ||
#endif | ||
|
||
|
@@ -153,7 +154,8 @@ public struct TextState: Equatable, Hashable, Sendable { | |
func hash(into hasher: inout Hasher) { | ||
enum Key { | ||
case concatenated | ||
case localized | ||
case localizedStringKey | ||
case localizedStringResource | ||
case verbatim | ||
} | ||
|
||
|
@@ -164,11 +166,15 @@ public struct TextState: Equatable, Hashable, Sendable { | |
hasher.combine(second) | ||
|
||
#if canImport(SwiftUI) | ||
case let .localized(key, tableName, bundle, comment): | ||
hasher.combine(Key.localized) | ||
case let .localizedStringKey(key, tableName, bundle, comment): | ||
hasher.combine(Key.localizedStringKey) | ||
hasher.combine(key.formatted(tableName: tableName, bundle: bundle, comment: comment)) | ||
#endif | ||
|
||
case let .localizedStringResource(resource): | ||
hasher.combine(Key.localizedStringResource) | ||
hasher.combine(resource) | ||
|
||
case let .verbatim(string): | ||
hasher.combine(Key.verbatim) | ||
hasher.combine(string) | ||
|
@@ -177,6 +183,62 @@ public struct TextState: Equatable, Hashable, Sendable { | |
} | ||
} | ||
|
||
// MARK: - LocalizedStringResourceBox | ||
|
||
fileprivate struct LocalizedStringResourceBox: Equatable, Hashable, @unchecked Sendable { | ||
let value: Any | ||
|
||
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) | ||
init(_ resource: LocalizedStringResource) { | ||
self.value = resource as Any | ||
} | ||
|
||
static func == (lhs: LocalizedStringResourceBox, rhs: LocalizedStringResourceBox) -> Bool { | ||
guard | ||
#available(macOS 13, iOS 16, tvOS 16, watchOS 9, *), | ||
let lhs = lhs.value as? LocalizedStringResource, | ||
let rhs = rhs.value as? LocalizedStringResource | ||
else { | ||
return false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably |
||
} | ||
|
||
return lhs == rhs | ||
} | ||
|
||
func hash(into hasher: inout Hasher) { | ||
guard | ||
#available(macOS 13, iOS 16, tvOS 16, watchOS 9, *), | ||
let resource = value as? LocalizedStringResource | ||
else { | ||
preconditionFailure("LocalizedStringResourceBox should only be exposed where LocalizedStringResource is available.") | ||
} | ||
|
||
hasher.combine(String(localized: resource)) | ||
} | ||
|
||
func asText() -> Text { | ||
guard | ||
#available(macOS 13, iOS 16, tvOS 16, watchOS 9, *), | ||
let resource = value as? LocalizedStringResource | ||
else { | ||
preconditionFailure("LocalizedStringResourceBox should only be exposed where LocalizedStringResource is available.") | ||
} | ||
|
||
return Text(resource) | ||
} | ||
|
||
func asString() -> String { | ||
guard | ||
#available(macOS 13, iOS 16, tvOS 16, watchOS 9, *), | ||
let resource = value as? LocalizedStringResource | ||
else { | ||
preconditionFailure("LocalizedStringResourceBox should only be exposed where LocalizedStringResource is available.") | ||
} | ||
|
||
return String(localized: resource) | ||
} | ||
} | ||
|
||
// MARK: - API | ||
|
||
extension TextState { | ||
|
@@ -196,10 +258,19 @@ extension TextState { | |
bundle: Bundle? = nil, | ||
comment: StaticString? = nil | ||
) { | ||
self.storage = .localized(key, tableName: tableName, bundle: bundle, comment: comment) | ||
self.storage = .localizedStringKey(key, tableName: tableName, bundle: bundle, comment: comment) | ||
} | ||
#endif | ||
|
||
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) | ||
public init( | ||
_ resource: LocalizedStringResource | ||
) { | ||
self.storage = .localizedStringResource( | ||
LocalizedStringResourceBox(resource) | ||
) | ||
} | ||
|
||
public static func + (lhs: Self, rhs: Self) -> Self { | ||
.init(storage: .concatenated(lhs, rhs)) | ||
} | ||
|
@@ -389,6 +460,16 @@ extension TextState { | |
return `self` | ||
} | ||
|
||
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) | ||
public func accessibilityLabel( | ||
_ resource: LocalizedStringResource | ||
) -> Self { | ||
var `self` = self | ||
`self`.modifiers.append( | ||
.accessibilityLabel(.init(verbatim: String(localized: resource)))) | ||
return `self` | ||
} | ||
|
||
public func accessibilityLabel( | ||
_ key: LocalizedStringKey, tableName: String? = nil, bundle: Bundle? = nil, | ||
comment: StaticString? = nil | ||
|
@@ -445,8 +526,10 @@ extension TextState { | |
switch state.storage { | ||
case let .concatenated(first, second): | ||
text = Text(first) + Text(second) | ||
case let .localized(content, tableName, bundle, comment): | ||
case let .localizedStringKey(content, tableName, bundle, comment): | ||
text = .init(content, tableName: tableName, bundle: bundle, comment: comment) | ||
case let .localizedStringResource(resourceBox): | ||
text = resourceBox.asText() | ||
case let .verbatim(content): | ||
text = .init(verbatim: content) | ||
} | ||
|
@@ -463,9 +546,13 @@ extension TextState { | |
switch value.storage { | ||
case let .verbatim(string): | ||
return text.accessibilityLabel(string) | ||
case let .localized(key, tableName, bundle, comment): | ||
case let .localizedStringKey(key, tableName, bundle, comment): | ||
return text.accessibilityLabel( | ||
Text(key, tableName: tableName, bundle: bundle, comment: comment)) | ||
case let .localizedStringResource(resourceBox): | ||
return text.accessibilityLabel( | ||
resourceBox.asText() | ||
) | ||
case .concatenated(_, _): | ||
assertionFailure("`.accessibilityLabel` does not support concatenated `TextState`") | ||
return text | ||
|
@@ -570,7 +657,7 @@ extension String { | |
self = String(state: lhs, locale: locale) + String(state: rhs, locale: locale) | ||
|
||
#if canImport(SwiftUI) | ||
case let .localized(key, tableName, bundle, comment): | ||
case let .localizedStringKey(key, tableName, bundle, comment): | ||
self = key.formatted( | ||
locale: locale, | ||
tableName: tableName, | ||
|
@@ -579,6 +666,9 @@ extension String { | |
) | ||
#endif | ||
|
||
case let .localizedStringResource(resourceBox): | ||
self = resourceBox.asString() | ||
|
||
case let .verbatim(string): | ||
self = string | ||
} | ||
|
@@ -635,9 +725,12 @@ extension TextState: CustomDumpRepresentable { | |
case let .concatenated(lhs, rhs): | ||
output = dumpHelp(lhs) + dumpHelp(rhs) | ||
#if canImport(SwiftUI) | ||
case let .localized(key, tableName, bundle, comment): | ||
case let .localizedStringKey(key, tableName, bundle, comment): | ||
output = key.formatted(tableName: tableName, bundle: bundle, comment: comment) | ||
#endif | ||
case let .localizedStringResource(resourceBox): | ||
output = resourceBox.asString() | ||
|
||
case let .verbatim(string): | ||
output = string | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you use
any Sendable
here to get rid of the@unchecked Sendable
?