Skip to content

Conversation

ajpallares
Copy link
Member

@ajpallares ajpallares commented Jun 23, 2025

This is closely related to #5309, as the solution in that PR was not correct.

Checklist

  • If applicable, unit tests

Motivation

Before this PR, the SDK would have a custom implementation to resolve the best locale from ui_config.localizations based on a paywall's locale. This would cause some bugs:

Description

This PR now uses the system's Bundle.preferredLocalizations(from:forPreferences:) API, which takes care of these special cases for us.

…s components

The new implementation uses the API `Bundle.preferredLocalizations(from:forPreferences:)`
@ajpallares
Copy link
Member Author

@RCGitBot please test

@ajpallares ajpallares added the pr:fix A bug fix label Jun 23, 2025
@ajpallares ajpallares requested review from joshdholtz and a team June 23, 2025 12:54
@ajpallares ajpallares marked this pull request as ready for review June 23, 2025 12:54
Copy link
Member

@JayShortway JayShortway left a comment

Choose a reason for hiding this comment

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

So much simpler! Just a request for 1 more test.

Comment on lines 33 to 44
/// Returns the elements in `identifiers` that best match the any language in `preferredLanguages`
/// (i.e. same language code), sorted by best match.
///
/// This method takes into account the order of `preferredLanguages` to determine the best match.
/// E.g.
/// if `preferredLanguages` is `["en-US", "es-ES"]`, then the identifier `en-GB`
/// will be preferred over `es-ES`.
///
/// - important: As specified in the documentation of `Bundle.preferredLocalizations(from:forPreferences:)`
/// "_This method doesn’t return all localizations in order of user preference. To get this information,
/// you can call this method repeatedly, each time removing the identifiers returned by the previous call._"
/// This means that not all matches will be returned, but only the best ones based on `preferredLanguages`.
Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit confused by these 2 statements:

[...] sorted by best match.

and

[...] doesn’t return all localizations in order of user preference.

They seem to contradict each other, but I'm probably misunderstanding.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right that it's confusing. I'll rephrase that.
Anyway, I'm struggling a lot when trying to explain it because what the method Bundle.preferredLocalizations(from:forPreferences:) returns is also very confusing! 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

Simplified implementation and documentation in d761e7f

Comment on lines +148 to +153
func test_es_MX_Matches_es_before_es_ES() {
let localizations = [
"en": ["wrong": "this is en"],
"es_ES": ["wrong": "this is es_ES"],
"es": Self.expectedTranslations
]
Copy link
Member

Choose a reason for hiding this comment

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

Could we add another test to check that es_MX matches es_ES if that's the only available Spanish language?

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense! Done in be52a72

@ajpallares ajpallares merged commit e1a3ff9 into main Jun 25, 2025
12 checks passed
@ajpallares ajpallares deleted the fix-paywall-locale-resolution branch June 25, 2025 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants