Englis...">

Cannot display Chinese traditional language in Android application

I have a folder with an array:

<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 set the folder name "values-zh-rTW" in the res folder, and android studio only shows zh in the translation editor

Now according to my code, I can choose English and Chinese Simplified, but when I select Chinese Traditional from the settings displayed in English, why not?

here is my code to get the language and set the 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();

    }
+4
source share
2 answers

got it if you guys use a language that uses something like this zh-rTW or anylanguage-blabla

then you need to break the language code "-", and then pass the second parameter to

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

everything works as expected

+3

Android 7+ (API 24). , 2 :

  • values-zh/strings.xml - (zh, zh-CN, zh-SG)
  • values-zh-rTW/strings.xml - (zh-TW, zh-HK)

values-zh-rTW/strings.xml, Android 7 , . . (Android 6 -zh - )

:

+3

Source: https://habr.com/ru/post/1660469/


All Articles