Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android changing locales of Chinese and traditional chinese not working

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 ?

like image 744
Siva K Avatar asked Feb 11 '15 08:02

Siva K


2 Answers

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
like image 63
daisura99 Avatar answered Sep 21 '22 06:09

daisura99


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());
}
like image 4
Kamran Ahmad Avatar answered Sep 23 '22 06:09

Kamran Ahmad