Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can not display chinese traditional language in android app

Tags:

android

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();

    }
like image 916
AndroidBeginnerJhon Avatar asked Nov 11 '16 07:11

AndroidBeginnerJhon


2 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

like image 185
AndroidBeginnerJhon Avatar answered Sep 20 '22 09:09

AndroidBeginnerJhon


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:

  • https://gist.github.com/amake/0ac7724681ac1c178c6f95a5b09f03ce#new-locales-vs-old-locales-chinese
  • https://fabiohub.wordpress.com/2016/12/31/chinese-locale-in-android/
  • https://blog.egorand.me/a-curious-case-of-multiple-locales/
like image 38
Mr-IDE Avatar answered Sep 22 '22 09:09

Mr-IDE