Home » Android » can not display chinese traditional language in android app

can not display chinese traditional language in android app

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have array folder:

<string-array name="language">
    <item>English</item>
    <item>Chinese Simplified</item>
    <item>Chinese Traditional</item>
</string-array>

<string-array name="language_values">
    <item>en</item>
    <item>zh</item>
    <item>zh-rTW</item>
</string-array>

I have put folder name “values-zh-rTW” in res folder and android studio only shows zh in translation editor

now as per my code, I can select English and Chinese simplified but when I select Chinese traditional from settings English is displayed do not why?

here is my code to get language and set language:

private String getLanguage(Context c, String defaultLanguage) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
        return preferences.getString("language", defaultLanguage);
    }

    @SuppressWarnings("deprecation")
    public void setLanguage(Context context, String languageCode) {
        Locale locale = new Locale(languageCode);
        Locale.setDefault(locale);
        Configuration config = new Configuration();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            config.setLocale(locale);
        } else {
            config.locale = locale;
        }

        context.getApplicationContext().getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("language", languageCode);
        editor.apply();

    }
How to&Answers:

got it ,if you guys are using language which uses something like this zh-rTW or anylanguage-blabla

then you have to split the language code “-” and then pass second parameter in

Locale locale = new Locale("zh","TW");

done everything work as expected

Answer:

Loading Traditional Chinese string resources has changed slightly in Android 7+ (API 24). To support Traditional and Simplified Chinese at the same time in your app, you must now explicitly create a minimum of 2 language files:

  • values-zh/strings.xml — For Simplified Chinese (zh, zh-CN, zh-SG)
  • values-zh-rTW/strings.xml — For Traditional Chinese (zh-TW, zh-HK)

If you don’t include the values-zh-rTW/strings.xml, then Android 7 devices will fall back to English, not Simplified Chinese. It’s unusual and confusing. (Android 6 and below will fallback to values-zh – Simplified Chinese, and only requires one language file)

More info: