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: add logs for localization lookup #3649

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

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

// MARK: - Events

case event_manager_initialized
Expand Down Expand Up @@ -56,6 +60,15 @@ 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 .found_localization(locale):
return "Found localized configuration for '\(locale.identifier)'"

case let .fallback_localization(localeIdentifier):
return "Failed looking up localization, using fallback: \(localeIdentifier)"

// MARK: - Events

case .event_manager_initialized:
Expand Down
26 changes: 21 additions & 5 deletions Sources/Paywalls/PaywallData+Localization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,27 @@ public extension PaywallData {

// Visible for testing
internal func localizedConfiguration(for locales: [Locale]) -> LocalizedConfiguration {
return locales
Logger.verbose(Strings.paywalls.looking_up_localization(locales))

let result: (locale: Locale, config: LocalizedConfiguration)? = locales
.lazy
.compactMap(self.config(for:))
.compactMap { locale in
self.config(for: locale)
.map { (locale, $0) }
}
.first { _ in true } // See https://github.com/apple/swift/issues/55374
?? self.fallbackLocalizedConfiguration

if let result {
Logger.verbose(Strings.paywalls.found_localization(result.locale))

return result.config
} else {
let (locale, fallback) = self.fallbackLocalizedConfiguration

Logger.warn(Strings.paywalls.fallback_localization(localeIdentifier: locale))

return fallback
}
}

// Visible for testing
Expand All @@ -43,9 +59,9 @@ public extension PaywallData {
return result
}

private var fallbackLocalizedConfiguration: LocalizedConfiguration {
private var fallbackLocalizedConfiguration: (String, LocalizedConfiguration) {
// This can't happen because `localization` has `@EnsureNonEmptyCollectionDecodable`.
guard let result = self.localization.first?.value else {
guard let result = self.localization.first else {
fatalError("Corrupted data: localization is empty.")
}

Expand Down