BaiJiFeiLong
BaiJiFeiLong

Reputation: 4605

How to make one language work on all regions in android I18N?

I want to let values-zh work on all regions of zh, such as zh-rCN, zh-rTW, zh-rSG, because any kind of Chinese language is more readable than English for most people that speak any Chinese. But values-zh.xml worked same as values-zh-rCN on my machine of MI-5X. So what is the best solution to achieve this goal?

Upvotes: 2

Views: 60

Answers (1)

ByteHamster
ByteHamster

Reputation: 4951

Approach 1:

If you are using Linux on your development machine, you can create a symlink from values-rCN to values-zh by using:

ln -s values-zh values-rCN


Approach 2:

You can change the language at runtime if you detect that rCN is used. Please keep in mind that this solution is a bit hacky and might not work in all cases.

Resources res = getApplicationContext().getResources();
Configuration config = res.getConfiguration();

Locale sysLocale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    sysLocale = config.getLocales().get(0);
} else {
    sysLocale = config.locale;
}

if (sysLocale.getLanguage().equals("zh-rCN")) {
    android.content.res.Configuration conf = res.getConfiguration();
    conf.setLocale(new Locale("zh"));
    res.updateConfiguration(conf, res.getDisplayMetrics());
}

Upvotes: 1

Related Questions