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: prioritize Locale.current over Locale.preferredLocales #3657

Merged
merged 4 commits into from
Feb 8, 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
7 changes: 4 additions & 3 deletions Sources/Logging/Strings/PaywallsStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum PaywallsStrings {
case caching_presented_paywall
case clearing_presented_paywall

case looking_up_localization([Locale])
case looking_up_localization(preferred: [Locale], search: [Locale])
case found_localization(Locale)
case fallback_localization(localeIdentifier: String)

Expand Down Expand Up @@ -60,8 +60,9 @@ extension PaywallsStrings: LogMessage {
case .clearing_presented_paywall:
return "PurchasesOrchestrator: clearing presented paywall"

case let .looking_up_localization(locales):
return "Looking up localized configuration for \(locales.map(\.identifier))"
case let .looking_up_localization(preferred, search):
return "Looking up localized configuration for \(preferred.map(\.identifier)), " +
"searching for \(search.map(\.identifier))"

case let .found_localization(locale):
return "Found localized configuration for '\(locale.identifier)'"
Expand Down
17 changes: 8 additions & 9 deletions Sources/Paywalls/PaywallData+Localization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ public extension PaywallData {
}

// Visible for testing
internal func localizedConfiguration(for locales: [Locale]) -> LocalizedConfiguration {
Logger.verbose(Strings.paywalls.looking_up_localization(locales))
internal func localizedConfiguration(for preferredLocales: [Locale]) -> LocalizedConfiguration {
// Allows us to search each locale in order of priority, both with the region and without.
// Example: [en_UK, es_ES] => [en_UK, en, es_ES, es]
Copy link
Contributor

Choose a reason for hiding this comment

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

🙌 Makes sense!

let locales: [Locale] = preferredLocales.flatMap { [$0, $0.removingRegion].compactMap { $0 } }

Logger.verbose(Strings.paywalls.looking_up_localization(preferred: preferredLocales,
search: locales))

let result: (locale: Locale, config: LocalizedConfiguration)? = locales
.lazy
Expand All @@ -50,13 +55,7 @@ public extension PaywallData {
/// - 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)
}

return result
return [.current] + Locale.preferredLocales
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 goes back to the original implementation. It allows us to test the actual behavior in localizedConfiguration since it no longer relies on this returning the .removingRegion addition.

}

private var fallbackLocalizedConfiguration: (String, LocalizedConfiguration) {
Expand Down
19 changes: 13 additions & 6 deletions Tests/UnitTests/Paywalls/PaywallDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,18 @@ class PaywallDataTests: BaseHTTPResponseTest {

let enConfig = try XCTUnwrap(paywall.localizedConfiguration(for: [
.init(identifier: "en_IN"),
.init(identifier: "en-IN"),
.init(identifier: "en-IN").removingRegion
].compactMap { $0 }))
.init(identifier: "en-IN")
]))
expect(enConfig.title) == "Paywall"
}

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

let enConfig = try XCTUnwrap(paywall.localizedConfiguration(for: [
.init(identifier: "en_IN"),
.init(identifier: "es_ES")
]))
expect(enConfig.title) == "Paywall"
}

Expand All @@ -139,14 +148,12 @@ class PaywallDataTests: BaseHTTPResponseTest {
if #available(iOS 17.0, tvOS 17, watchOS 10, *) {
expected = [
"en_US",
"en-US",
"en"
"en-US"
]
} else {
expected = [
"en_US",
// `Locale.preferredLanguages` returns `en` before iOS 17.
"en",
"en"
]
}
Expand Down