Jatinder
Jatinder

Reputation: 41

Android Simplified Chinese and traditional chinese not working

I have put simplified Chinese in values-zh-rCN , zh and traditional Chinese in values-zh-rTW. But on changing locale it always load strings from zH. Here is how i am changing Locale.

public  void setLanguage(String languageCode, String countryCode){
    Locale locale = new Locale(languageCode, countryCode);
    Locale.setDefault(locale);
    Configuration config = getResources().getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        setSystemLocale(config, locale);
    } else {
        setSystemLocaleLegacy(config, locale);
    }
    BaseSharedPreference.getInstance().setLanguage(locale.getLanguage());
    recreate();
}

Upvotes: 2

Views: 4660

Answers (1)

lmm333
lmm333

Reputation: 323

In Android 7 many new locales are supported by default. It can be tricky to support both old locales and new locales correctly in some cases; here I will discuss one I happen to be aware of: Chinese.

Basic background:

Chinese is written in two different scripts: Simplified and Traditional Each Chinese-speaking region generally uses just one script While ideally one would localize for each region, we will assume here that we have just one resource set for each script.

Prior to Android 7, the following Chinese locales were available:

zh-CN (Simplified)
zh-TW (Traditional)

In some cases:

zh-SG (Simplified)
zh-HK (Traditional)
zh-MO (Traditional)

A common resource layout scheme to support the above locales while minimizing resource duplication would be:

values-zh: Traditional
values-zh-rCN: Simplified
values-zh-rSG: Simplified

In other words Traditional resources are put at the root, and zh-TW, zh-HK, and zh-MO are covered by fallback.

In Android 7, the older language-region locales are gone, replaced by the following:

zh-Hans-CN
zh-Hans-MO
zh-Hans-HK
zh-Hans-SG
zh-Hant-TW
zh-Hant-HK
zh-Hant-MO

Note:

The script and region are specified separately There are now default locales specifying Simplified script in traditionally Traditional regions: zh-Hans-MO and zh-Hans-HK. Problems using the old scheme in Android 7:

  • zh-Hans-* falls back to zh before any children of zh, and thus would appear as Traditional instead of Simplified
  • zh-Hans does not appear to be recognized at all This indicates a preference for zh to be Simplified, not Traditional. However this is not clear from the SDK itself, which has only zh-CN, zh-HK, and zh-TW resources.
  • zh-Hant-* falls back to zh-Hant and then the default, and thus would appear as en. Just zh and zh-Hant are sufficient for covering the Android 7 locales, but we need to maintain support for Android 6 and earlier.

Thus the minimal correct resource layout is now:

values-zh: Simplified
values-zh-rTW: Traditional
values-zh-rHK: Traditional
values-zh-rMO: Traditional
values-b+zh+Hans+HK: Simplified
values-b+zh+Hans+MO: Simplified

With this we get the desired behavior:

On Android 6 and earlier:

  • zh-CN and zh-SG fall back to zh (Simplified)
  • zh-TW, zh-HK, and zh-MO have specific resources (Traditional)

On Android 7:

  • zh-Hans-CN and zh-Hans-SG fall back to zh (Simplified)
  • zh-Hant-TW, zh-Hant-HK, and zh-Hant-MO fall back to their language-region locales (Traditional)
  • zh-Hans-HK and zh-Hans-MO have specific resources (Simplified)

You can see detail from this post Link

Upvotes: 7

Related Questions