Implement modern locale tag processing #21956
Open
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.
Implemented modern IETF BCP 47 locale tag processing
Problem & Initial Fix
The issue we aimed to fix was incorrect parsing of the script in a locale tag. This occurred because the system-provided locale tag used a different format than what our app expected—specifically, it used
#
instead of+
to separate the script from other locale parameters.A quick fix was introduced in PR #21908, which addressed this formatting issue. However, it assumed that if a script was present, it would always be the third element in the locale tag. This assumption was incorrect, as scripts are optional, and other components like extensions could take the third position instead. Attempting to set an extension as a script in
Locale.Builder
caused crashes, since scripts must be alpha-4 codes as per ISO 15924 (all possible scripts can be found here (search for "Type: script")).This issue arose due to a misunderstanding of the locale tag specification and how its components are structured.
Solution
After further investigation, we discovered that
Locale.toString()
returns locale tags in an outdated format, whereasLocale.toLanguageTag()
produces tags compliant with the modern IETF BCP 47 standard.To resolve this, we made the following changes:
Locale.forLanguageTag()
to correctly interpret locale tags from system settings.Locale.toLanguageTag()
.This ensures:
Since newer Android versions no longer display an in-app language selection option in OsmAnd (instead relying on system settings), this approach aligns with the platform's expected behavior.