Skip to content

Allow choosing the locale for a PaywallView #4833

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ajpallares
Copy link
Member

Checklist

  • If applicable, unit tests
  • If applicable, create follow-up issues for purchases-android and hybrids

Motivation

Currently, the PaywallPreview is displayed using a locale decided by the SDK (based on the device's locales preferences). There have been some requests to allow specifying the locale to use when presenting the paywall.

Description

This PR introduces the preferredLocale in PaywallView. Initially, this parameter is only included under @_spi(Internal), but the idea is to make this public in the short term.

Notes

Why preferredLocale?

The SDK can't guarantee that any locale specified by the caller will be available for a specific paywall. That's why the name preferredLocale was chosen. To highlight the fact that it will be used for the paywall only when possible. If that locale isn't available for the paywall, then the SDK will fall back to the current behavior of choosing the locale based on the device's locales preferences.

@ajpallares ajpallares requested review from joshdholtz and a team February 27, 2025 17:43
@ajpallares ajpallares changed the title Allow choosing the locale to display a PaywallView Allow choosing the locale for a PaywallView Feb 27, 2025
@@ -22,7 +22,7 @@ struct PaywallViewConfiguration {
let useDraftPaywall: Bool
var introEligibility: TrialOrIntroEligibilityChecker?
var purchaseHandler: PurchaseHandler
var locale: Locale
Copy link
Member Author

@ajpallares ajpallares Feb 27, 2025

Choose a reason for hiding this comment

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

I realized that the locale of PaywallViewConfiguration was actually redundant. It was always holding Locale.current, as the init had .current as the default value for the locale parameter and this locale parameter was never used.
That's why I've replaced it with preferredLocale, which will now potentially come from the integrator's side

Copy link
Member Author

Choose a reason for hiding this comment

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

In those places where a locale is non-optional (basically in v1 paywalls, if I'm not mistaken), I've replaced the use of self.locale to self.preferredLocale ?? .current to keep the existing behavior if preferredLocale is not specified

@ajpallares ajpallares requested a review from a team February 27, 2025 17:53
@@ -22,7 +22,7 @@ struct PaywallViewConfiguration {
let useDraftPaywall: Bool
var introEligibility: TrialOrIntroEligibilityChecker?
var purchaseHandler: PurchaseHandler
var locale: Locale
var preferredLocale: Locale?
Copy link
Member

Choose a reason for hiding this comment

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

Nice! We should probably add it to the presentPaywallIfNeeded() api too, as well as Android (and hybrids 🫠).

@ferrannp
Copy link

Hello @ajpallares!

I came across this PR while looking into some related issues, and I found it very helpful—great work!

I’m reaching out from the React Native side, where we typically don’t rely on the device’s system language. Instead, most apps use an i18n package to manage localization dynamically. Because of this, when a user changes the app’s language, we update the locale at the JavaScript level, but the native iOS paywall still follows the device’s language.

It would be incredibly useful to have a way to pass the preferred locale from React Native to ensure that the paywall aligns with the app’s selected language.

Thank you!

@ajpallares
Copy link
Member Author

I’m reaching out from the React Native side, where we typically don’t rely on the device’s system language. Instead, most apps use an i18n package to manage localization dynamically. Because of this, when a user changes the app’s language, we update the locale at the JavaScript level, but the native iOS paywall still follows the device’s language.

Thank you for the feedback @ferrannp! I honestly didn't know that fact about React Native and it makes this even more necessary. We don't have an exact timeline for it, but we definitely do intend to finish the work on this soon!

@ferrannp
Copy link

Thanks @ajpallares ! FYI, I patched this library to make it work for now in my project. I was struggling with some stuff and I realized that here:

let preferredLanguages = self.systemInfo.preferredLanguages.prefix(3).map {
you download languages only based on the iOS system settings, so I had to patch that too. Just in case you want to handle this similar situation (e.g. I make the Paywall in Spanish but the user does not have Spanish in their iPhone settings).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants