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

Paywalls: fix finding locales with different regions #3633

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions Sources/FoundationExtensions/Locale+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,23 @@ extension Locale {
#endif
}

// swiftlint:disable:next identifier_name
var rc_languageCode: String? {
#if swift(>=5.9)
// `Locale.languageCode` is deprecated
if #available(macOS 13, iOS 16, tvOS 16, watchOS 9, visionOS 1.0, *) {
return self.language.languageCode?.identifier
} else {
return self.languageCode
}
#else
return self.languageCode
#endif
}

/// - Returns: the same locale as `self` but removing its region.
var removingRegion: Self? {
return self.rc_languageCode.map(Locale.init(identifier:))
}

}
22 changes: 19 additions & 3 deletions Sources/Paywalls/PaywallData+Localization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,31 @@ public extension PaywallData {
/// - Returns: the ``PaywallData/LocalizedConfiguration-swift.struct`` to be used
/// based on `Locale.current` or `Locale.preferredLocales`.
var localizedConfiguration: LocalizedConfiguration {
let locales: [Locale] = [.current] + Locale.preferredLocales
return self.localizedConfiguration(for: Self.localesOrderedByPriority)
}

// Visible for testing
internal func localizedConfiguration(for locales: [Locale]) -> LocalizedConfiguration {
return locales
.lazy
.compactMap(self.config(for:))
.first { _ in true } // See https://github.com/apple/swift/issues/55374
?? self.fallbackLocalizedConfiguration
}

// Visible for testing
/// - Returns: The list of locales that paywalls should try to search for.
/// Includes `Locale.current` and `Locale.preferredLanguages`.
internal static var localesOrderedByPriority: [Locale] {
var result = [.current] + Locale.preferredLocales

if let withoutRegion = Locale.current.removingRegion {
result.append(withoutRegion)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix

}

return result
}

private var fallbackLocalizedConfiguration: LocalizedConfiguration {
// This can't happen because `localization` has `@EnsureNonEmptyCollectionDecodable`.
guard let result = self.localization.first?.value else {
Expand All @@ -40,9 +56,9 @@ public extension PaywallData {

// MARK: -

private extension Locale {
extension Locale {

static var preferredLocales: [Self] {
fileprivate static var preferredLocales: [Self] {
return Self.preferredLanguages.map(Locale.init(identifier:))
}

Expand Down
20 changes: 19 additions & 1 deletion Tests/UnitTests/FoundationExtensions/LocaleExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,26 @@ class LocaleExtensionsTests: TestCase {
expect(Locale(identifier: "en_US").rc_currencyCode) == "USD"
}

func testMissingCurrenctCode() {
func testMissingCurrencyCode() {
expect(Locale(identifier: "").rc_currencyCode).to(beNil())
}

func testLanguageCodeCode() {
expect(Locale(identifier: "en_US").rc_languageCode) == "en"
expect(Locale(identifier: "en-IN").rc_languageCode) == "en"
expect(Locale(identifier: "en").rc_languageCode) == "en"
}

func testMissingLanguageCode() {
expect(Locale(identifier: "").rc_languageCode).to(beNil())
}

func testRemovingRegion() {
expect(Locale(identifier: "en_US").removingRegion?.identifier) == "en"
expect(Locale(identifier: "en-IN").removingRegion?.identifier) == "en"
expect(Locale(identifier: "en_ES").removingRegion?.identifier) == "en"
expect(Locale(identifier: "en").removingRegion?.identifier) == "en"
expect(Locale(identifier: "").removingRegion).to(beNil())
}

}
32 changes: 32 additions & 0 deletions Tests/UnitTests/Paywalls/PaywallDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,38 @@ class PaywallDataTests: BaseHTTPResponseTest {
expect(esConfig.title) == "Tienda"
}

func testLocalizedConfigurationFallsBackToLanguageWithDifferentRegion() throws {
let paywall: PaywallData = try self.decodeFixture("PaywallData-Sample1")

let enConfig = try XCTUnwrap(paywall.localizedConfiguration(for: [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also have a test for this method, which we didn't have before.

.init(identifier: "en_IN"),
.init(identifier: "en-IN"),
.init(identifier: "en-IN").removingRegion
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that this is included in Paywall.localizedConfiguration is tested below.

].compactMap { $0 }))
expect(enConfig.title) == "Paywall"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a failing test before the fix.

}

func testLocalesOrderedByPriority() throws {
let expected: [String]

if #available(iOS 17.0, tvOS 17, watchOS 10, *) {
expected = [
"en_US",
"en-US",
"en"
]
} else {
expected = [
"en_US",
// `Locale.preferredLanguages` returns `en` before iOS 17.
"en",
"en"
]
}

expect(PaywallData.localesOrderedByPriority.map(\.identifier)) == expected
}

func testDoesNotFindLocaleWithMissingLanguage() throws {
let paywall: PaywallData = try self.decodeFixture("PaywallData-Sample1")

Expand Down