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();
}
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
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With