In my app I am having an option of switching from Chinese to traditional chinese.
I am using spinner, where position 1 is chinese and 2 is traditional chinese. When position 1 is selected, here is my code which switches the language
if (pos == 0)
{
langSelected ="en";
}
else if (pos == 1)
{
langSelected ="zh";
}
else if (pos == 2)
{
langSelected ="zh-rTW";
}
Locale locale = new Locale(lang);
Locale.setDefault(locale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (locale != null){
newConfig.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
While switching from english to chinese using spinner the correct language gets loaded, but when loading traditional chinese(zh-rTW), only the chinese text gets loaded
I have my simplified chinese text in values-zh, where as I have loaded the Traditional Chinese text in values-zh-rTW
The app name differs for each language, so I also tried changing the Language from device Settings, now also in Simplified Chinese it is loading correctly but traditional Chinese not loading. But here the app name gets changed for Traditional Chinese, i.e. app name loads from values-zh-rTW
Where I am going wrong, should I have to change the folder for Traditional Chinese ?
I know it's a late post, but hope it helps someone..
The solution is to simply create a Locale using the country name. What this means is that the Locale
class already has some static locale declared. For example:-
China locale - https://developer.android.com/reference/java/util/Locale.html#CHINA
Taiwan locale - https://developer.android.com/reference/java/util/Locale.html#TAIWAN
So put simply, the solution is:-
Locale locale;
if(lang.equals("zh-rTW"))
locale = Locale.TAIWAN;
else(lang.equals("zh-rCN")
locale = Locale.CHINA;
else
//handle other languages
This code changes locales of Chinese simplified and traditional:
public void setAppLocale(Context context, String languageCode, String countryCode){
Resources resources = context.getResources();
Locale locale = new Locale(languageCode, countryCode);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.setLocale(locale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
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