Refactor TXPreferredLocaleProvider logic #48
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
TXPreferredLocaleProvider
class does not always report the currentlocale to be used for localization correctly.
This is because the
TXPreferredLocaleProvider
implementation alwaysreturns 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 needsto 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 usedin the
getCurrentLocale()
method ofTXPreferredLocaleProvider
toreturn the proper locale.
The
getPreferredLocale()
is now being used only as a fallback and hasbeen 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 ofsupported 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 ofthe
TXLocaleState
has been altered so that the source locale isalways added first, in order to align with one of the internal quirks of
the
Bundle.preferredLocalizations()
method that fallsback to the firstlanguage of its provided
localizationsArray
argument.The
testCurrentLocale
unit test has been refactored to test againstthe scenario where the correct locale is not the first language set by
the user and more unit tests have been added.
Ref: