Skip to content

Commit

Permalink
Paywalls: added logs for localization lookup
Browse files Browse the repository at this point in the history
Will make debugging issues like #3633 and #3633 (comment) easier.

```
VERBOSE: Looking up localized configuration for ["de_SW", "de"]
VERBOSE: Found localized configuration for 'de'
```
  • Loading branch information
NachoSoto committed Feb 7, 2024
1 parent 7ee0dde commit 3a17578
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
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

0 comments on commit 3a17578

Please sign in to comment.