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

Refactor TXPreferredLocaleProvider logic #48

Merged
merged 3 commits into from
Dec 27, 2022

Conversation

stelabouras
Copy link
Collaborator

@stelabouras stelabouras commented Dec 23, 2022

The TXPreferredLocaleProvider class does not always report the current
locale to be used for localization correctly.

This is because the TXPreferredLocaleProvider implementation always
returns the first supported language from the Settings app, as set by
the user. That language, though, is not always the correct candidate, as
it might not be the most appropriate localization candidate, or not even
a valid candidate at all in some cases.

For example:

If user has set the following languages as their preference in the
Settings app: "Dutch", "French" and the supported languages for
localization in the app are "French", "English", "German", "Spanish"
and "Italian" then the correct language should be "French" and not
"Dutch" that is the first preferred language of the user.

Similarly, if "French" was not a supported language for localization on
the above example, then the logic must return "English" and not "Dutch".

In order to fix this issue, the TXPreferredLocaleProvider class needs
to be aware of the supported app locales (that are provided by the
developer) and also respect the order of preferred languages (that are
set by the user in the Settings app).

The method responsible for that is
Bundle.preferredLocalizations(from:, forPreferences:) and is now used
in the getCurrentLocale() method of TXPreferredLocaleProvider to
return the proper locale.

The getPreferredLocale() is now being used only as a fallback and has
been refactored for better readability and the final fallback locale
code ("en") has been made a static property (FALLBACK_LOCALE).

The TXPreferredLocaleProvider initializer now accepts the list of
supported application locales (set by the developer) as they are needed
for the aforementioned calculation.

Also, the list of the appLocales as calculated in the initializer of
the TXLocaleState has been altered so that the source locale is
always added first, in order to align with one of the internal quirks of
the Bundle.preferredLocalizations() method that fallsback to the first
language of its provided localizationsArray argument.

The testCurrentLocale unit test has been refactored to test against
the scenario where the correct locale is not the first language set by
the user and more unit tests have been added.

Ref:

@stelabouras stelabouras force-pushed the fix/current-locale branch 2 times, most recently from 4ade2e8 to ea0b3f8 Compare December 27, 2022 09:48
The `TXPreferredLocaleProvider` class does not always report the current
locale to be used for localization correctly.

This is because the `TXPreferredLocaleProvider` implementation always
returns the first supported language from the Settings app, as set by
the user. That language, though, is not always the correct candidate, as
it might not be the most appropriate localization candidate, or not even
a valid candidate at all in some cases.

For example:

If user has set the following languages as their preference in the
Settings app: "Dutch", "French" and the supported languages for
localization in the app are "French", "English", "German", "Spanish"
and "Italian" then the correct language should be "French" and not
"Dutch" that is the first preferred language of the user.

Similarly, if "French" was not a supported language for localization on
the above example, then the logic must return "English" and not "Dutch".

In order to fix this issue, the `TXPreferredLocaleProvider` class needs
to be aware of the supported app locales (that are provided by the
developer) and also respect the order of preferred languages (that are
set by the user in the Settings app).

The method responsible for that is
`Bundle.preferredLocalizations(from:, forPreferences:)` and is now used
in the `getCurrentLocale()` method of `TXPreferredLocaleProvider` to
return the proper locale.

The `getPreferredLocale()` is now being used only as a fallback and has
been refactored for better readability and the final fallback locale
code ("en") has been made a static property (`FALLBACK_LOCALE`).

The `TXPreferredLocaleProvider` initializer now accepts the list of
supported application locales (set by the developer) as they are needed
for the aforementioned calculation.

Also, the list of the `appLocales` as calculated in the initializer of
the `TXLocaleState` has been altered so that the source locale is
always added first, in order to align with one of the internal quirks of
the `Bundle.preferredLocalizations()` method that fallsback to the first
language of its provided `localizationsArray` argument.

The `testCurrentLocale` unit test has been refactored to test against
the scenario where the correct locale is not the first language set by
the user and more unit tests have been added.

Ref:
* https://developer.apple.com/library/archive/technotes/tn2418/_index.html
* https://medium.com/@hectorricardomendez/ios-bad-news-bundle-preferredlocalizations-is-not-100-reliable-13fa33454d14
Updates Github action script so that it will stop producing deprecation
warnings.
* Bumps `TXNative.version` to 1.0.3
* Updates CHANGELOG with the changes implemented for 1.0.3.
@stelabouras stelabouras changed the title WIP: Refactor TXPreferredLocaleProvider logic Refactor TXPreferredLocaleProvider logic Dec 27, 2022
@n1k0sv n1k0sv merged commit 249d851 into transifex:devel Dec 27, 2022
@txsentinel txsentinel mentioned this pull request Dec 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants