Files
purchases-ios/RevenueCatUI/Data/ProcessedLocalizedConfiguration.swift
T
Josh Holtz 1fa69d42f4 [Paywalls] Improve locale consistency (#4158)
⚠️ Waiting for #4188 to be merged first (because needs all the tests to
pass)

## Motivation

Fix cases where paywall was showing multiple languages at once and
random languages.

## Description

👉 For a demo of the problem and solution, look in Slack in the paywalls
or SDK channel for the latest Loom that I posted

### Flow of paywall localization logic (since it can be confusing)

1.
[PaywallView](https://github.com/RevenueCat/purchases-ios/pull/4158/files#diff-ea75fe7280803e15b975d702a16c6851ffc2bbed01b7b196bf0fb80d75b4b332)
starts with `Locale.current` but then calls
`offering.validatedPaywall()` which will return the new
`displayedLocale: Locale` variable
2.
[PaywallData+Validation](https://github.com/RevenueCat/purchases-ios/pull/4158/files#diff-452790cf5287a44f75a4b2260254db2281a066faeea2b3e441a007ec60522b6b)
returns `displayedLocale` if its a valid paywall, otherwise it return
`Locale.current` for fallback paywall
3.
[PaywallData+Localization](https://github.com/RevenueCat/purchases-ios/pull/4158/files#diff-e620baead94f7acad56d931c485d7691c4f909be3fe625e104528a2702cca2a0)
has a lot of the new logic
- New `locale: Locale` computer property that it determines the locale
from the `localizedConfiguration` or `localizedConfigurationsByTier`
- New ` defaultLocalizedConfiguration` that uses the developer set
`defaultLocale` (from `PaywallData`) to find the default localization
- Updated `localizedConfiguration<Value>()` to take a new
`defaultLocalization` to put in its priority (and log it if its used).
If for some reason that's not found, it will use the legacy
`fallbackLocalization` (which is the first localization it finds or
english)
4. After all of that, the `displayedLocale` is passed into the actually
SwiftUI `LoadedOfferingPaywallView` to show this locale everywhere

### Problem 1 - Inconsistent languages displayed at once

The user defined localizations and the pre-localized content in the SDK
(like links in the footer and package duration names) could should
different localizations. The choice of which locale was happening in two
different spots.

Since the SDK has all possible localizations that the dashboard offers,
the locale SDK displays from the dashboard will be chosen for the
pre-localized content.

### Problem 2 - Showing random languages if not supported

We decided early on to not use a `default_locale` but that has hurt us.
The solution to use was use `localizations.first` to show the first
locale in the list and the intention that this was the first locale the
user did in the paywall. That was not the case, however. The order of
the localizations list was indeterministic so it should should a random
language (even to the point where different random languages would show
the the same user).

We are now introducing a `default_locale` that the developer can set in
the paywall. If the locale the user is requesting is not translated by
the developer, it will use the `default_locale`

### Problem 3 - Footer not using correct localization from the bundle

There were times where some parts of the footer itself (mainly the
restore button) would be a different language than the terms of service
and privacy policy buttons. This was due to a code issue where we were
using `Bundle.module`. Instead, we needed to specify a localization
bundle specifically that we wanted to use.
2024-08-20 09:44:27 -05:00

111 lines
4.4 KiB
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// ProcessedLocalizedConfiguration.swift
import Foundation
import RevenueCat
/// A `PaywallData.LocalizedConfiguration` with processed variables
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
struct ProcessedLocalizedConfiguration: PaywallLocalizedConfiguration {
typealias Feature = PaywallData.LocalizedConfiguration.Feature
var title: String
var subtitle: String?
var callToAction: String
var callToActionWithIntroOffer: String?
var offerDetails: String?
var offerDetailsWithIntroOffer: String?
var offerName: String?
var offerBadge: String?
var features: [Feature]
var tierName: String?
var locale: Locale
init(
_ configuration: PaywallData.LocalizedConfiguration,
_ dataProvider: VariableDataProvider,
_ context: VariableHandler.Context,
_ locale: Locale
) {
let packageIdentifier = dataProvider.packageIdentifier
let offerOverrides = configuration.offerOverrides[packageIdentifier]
let offerDetails = offerOverrides?.offerDetails ?? configuration.offerDetails
let offerDetailsWithIntroOffer = offerOverrides?.offerDetailsWithIntroOffer
?? configuration.offerDetailsWithIntroOffer
let offerName = offerOverrides?.offerName ?? configuration.offerName
let offerBadge: String?
if let offerOverrides {
offerBadge = offerOverrides.offerBadge
} else if let discount = context.discountRelativeToMostExpensivePerMonth {
offerBadge = dataProvider.localizedRelativeDiscount(discount, locale)
} else {
offerBadge = nil
}
self.init(
title: configuration.title.processed(with: dataProvider, context: context, locale: locale),
subtitle: configuration.subtitle?.processed(with: dataProvider, context: context, locale: locale),
callToAction: configuration.callToAction.processed(with: dataProvider, context: context, locale: locale),
callToActionWithIntroOffer: configuration.callToActionWithIntroOffer?.processed(with: dataProvider,
context: context,
locale: locale),
offerDetails: offerDetails?.processed(with: dataProvider, context: context, locale: locale),
offerDetailsWithIntroOffer: offerDetailsWithIntroOffer?.processed(with: dataProvider,
context: context,
locale: locale),
offerName: offerName?.processed(with: dataProvider, context: context, locale: locale),
offerBadge: offerBadge?.processed(with: dataProvider, context: context, locale: locale),
features: configuration.features.map {
.init(title: $0.title.processed(with: dataProvider, context: context, locale: locale),
content: $0.content?.processed(with: dataProvider, context: context, locale: locale),
iconID: $0.iconID)
},
tierName: configuration.tierName,
locale: locale
)
}
private init(
title: String,
subtitle: String?,
callToAction: String,
callToActionWithIntroOffer: String?,
offerDetails: String?,
offerDetailsWithIntroOffer: String?,
offerName: String?,
offerBadge: String?,
features: [Feature],
tierName: String?,
locale: Locale
) {
self.title = title
self.subtitle = subtitle
self.callToAction = callToAction
self.callToActionWithIntroOffer = callToActionWithIntroOffer
self.offerDetails = offerDetails
self.offerDetailsWithIntroOffer = offerDetailsWithIntroOffer
self.offerName = offerName
self.offerBadge = offerBadge
self.features = features
self.tierName = tierName
self.locale = locale
}
}
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension ProcessedLocalizedConfiguration: Equatable {}