Fix display language not switching correctly to Chinese on native. #6621
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.
Why
In web development, language codes generally follow the RFC 1766 standard. In this standard, the language tag for Chinese is typically written as:
zh-CN
,zh-TW
,zh-HK
.However, in Android and iOS, language codes follow the RFC 4646 standard, which further distinguishes Simplified Chinese (Hans) and Traditional Chinese (Hant). The language tags in this standard look like:
zh-Hans-CN
,zh-Hant-TW
,zh-Hant-HK
When using
getLocales().languageTag
from expo-localization on an Android device (with system language set to Simplified Chinese), it will return this output:This cannot be matched to existing
AppLanguage
, so it will rollback to English. BecauseAppLanguage
naming follows RFC 1766, which is still widely used in browsers. Therefore, we cannot rename ChineseAppLanguage
to following RFC 4646.This patch is meant to solve this issue by converting the RFC 4646 output from the device into RFC 1766, just like how we handle incorrect languageCode on legacy Java devices (#4461).
With this patch, now using
getLocales().languageTag
will return the following output:Based on #5384, the app can now correctly handle language tags with region codes. This allows it to match the existing
zh-CN
inAppLanguage
and display Chinese when the app is opened for the first time, instead of rolling back to English.It's hard to say RFC 1766 or RFC 4646 which standard is better, but both will likely coexist for a long time. Web development is unlikely to transition to RFC 4646, as it would break browser behavior, and W3C seem to prefer RFC 1766.
For now, Chinese is one of the few languages that requires mapping conversion. Most languages work as expected under either standard. So this patch only includes conversion mapping for Chinese. I prefer not to rewrite the logic in the later parts of the code.
Before:
before.mp4
After:
after.mp4