ChatGPT解决这个技术问题 Extra ChatGPT

Simplified and Traditional Chinese vs Regions

In the process of implementing traditional and simplified chinese support in my Android application and I confused on how this is supposed to work.

So from reading the documentation as well as some discussions like this and this I have put simplified chinese into

values-zh

values-zh-rCN

values-zh-rSG

and traditional chinese into

values-zh-rTW

values-zh-rHK

That all works fine but somehow does not make sense to me (sorry if I dont understand enough about chinese simplified vs traditional usage).

From what I understand from checking the locale setting dialog in the emulator as well as on a rooted phone a user can change the locale to Simplified Chinese or Traditional Chinese.

Now here is the question. How does the system know where to get the simplified or traditional chinese strings.xml. Is there some sort of assumption baked in that says if I am supposed to display simplified chinese get it from values-zh and get traditional from values-zh-rTW?

But what if a user is located in HK and set his device up to display simplified chinese? Or what if an emigrant somewhere else in the world sets his device to traditional chinese but his region is e.g. US or CA?

What do I have to do to allow my users to set their locale and have my app appear with the right locale?

Update:

From further investigation I believe that a Android phone user can not set the country separately from the language. They can only set what is called the locale in the UI. It consists of language and country, but the list is limited so many combinations are not possible.

E.g. a Chinese person living in the US that wants to use Traditional Chinese could set the language to traditional chinese (at least on a rooted phone) and would then have a locale of zh-rTW. He could in no way set his country separately to be US. In a similar manner any sort of emigrant can not set their native language with the country they currently live in..

Basically that means that only a bunch of combinations of language and country are supported. Now the questions is how to find out which ones they are?

I know I could try to figure this out by reading the source .. and I probably will if nobody helps me out..

g
gordon

So I think after further investigation and with Marks answer I am going to provide an answer myself. Here is goes:

Out of the box Android only has the two locales zh_rCN, which is simplified chinese and zh_rTW, which is traditional chinese. As you can see from the Settings app these are the only supplied locales:

https://android.googlesource.com/platform/packages/apps/Settings/+/master/res/

However any other creator of an Android system e.g. for a phone sold in China or so could change what they add. The overall theoretically supported set can be found by looking at the list of locales found in the icu4c app:

https://android.googlesource.com/platform/external/icu4c.git/+/master/data/locales/ As you can see for Chinese there are

zh.txt

zh_CN.txt

zh_HK.txt

zh_Hans.txt

zh_Hans_CN.txt

zh_Hans_HK.txt

zh_Hans_MO.txt

zh_Hans_SG.txt

zh_Hant.txt

zh_Hant_HK.txt

zh_Hant_MO.txt

zh_Hant_TW.txt

zh_MO.txt

zh_SG.txt

zh_TW.txt

Hant is the ISO code for traditional and Hans for simplified chinese. So theoretically this would mean we could have

simplified chinese in China, HongKong, Macau and Singapore

traditional chinese in HongKong, Macau and Taiwan

However keep in mind that the Settings app would have to be modified to have the different selections of locale. So at this stage simplified chinese translates to zh_rCN and traditional zh_rTW and you should be apart for users that have such a modified Android image that supports other locales.

In any case to be on the save side you can use the Configuration class to get the locale e.g. in your Application class with getResources().getConfiguration().locale.

You could e.g. log that and send the data to your tracking system (whatever you use) or you could check for supported ones and throw an exception with the locale setting in the message and that would then show up in your market interface... it would however mean a minimum of one crash of your app (if you are on the ball you can publish and update a few minutes later ..)

So to recap .. the minimum setup would be mirroring what is done in the settings app (only have zh-rCN and zh-rTW), but if you want to provide for default locales for Singapore, HongKong, Macau supplying traditional chinese as default you can do that too and it should work. I have however no evidence that such a configuration is used anywhere..

Hope that helps.


Since Android 7.0, if a user select zh-rSG, it will look for zh-rSG, if it can't find it, it will instead look for zh-rCN or any other locale with the simplified script. – The same goes if a user select sv-rFI, it will look for, in order: sv-rFI, sv, sv-rSE
The current repository does provide values-zh-rHK.
values-zh-rCN will not compile. It says invalid resource directory name.
C
Community

Updated answer

The question and previous answers are pretty old. It is hard to tell how correct they still are. This answer is for other people coming here who just want to support both Traditional and Simplified Chinese in their app, but are not overly concerned about the details of locality.

Add string.xml files for Traditional and Simplified Chinese

In Android Studio right click the res folder and choose New > Android resource file. Type in strings for the File name and choose Locale from the list.

https://i.stack.imgur.com/vF4d4.png

Scroll down to zh for the language. Add one strings file for CN: China (simplified) and one for TW: Taiwan (traditional). If you are only using one then just choose Any Region.

As I understand the documentation, lookup will first check if there is an exact match for language and region (Example: Chinese-Traditional-Taiwan). Then it will fall back to broader checks. (This system is as of Android 7.0.)

1. zh-Hant-TW   // Chinese - Traditional - Taiwan
2. zh-Hant      // Chinese - Traditional
3. zh           // Chinese

I'm not completely sure about how all this works so please read this and this documentation.

Following this method in my own app, switching the system settings between Traditional and Simplified changed the app display also.

Converting between Traditional and Simplified

If you already have one translation in either Traditional or Simplified, you can auto convert the text online. Start with Google Translate to convert to the other (just paste in your strings.xml file). It does a fairly good job already. Then do it again several times using one of the many online converters. Use a text comparing tool or site (text-compare.com is good) to compare the result of the conversions. That will help you catch any mistakes that Google made.


A
Angel Koh

https://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources

Android 7.0 (API level 24) introduced support for BCP 47 language tags, which you can use to qualify language- and region-specific resources. A language tag is composed from a sequence of one or more subtags, each of which refines or narrows the range of language identified by the overall tag.

you can now define traditional or simplified using the BCP 47 tags. (To use a BCP 47 language tag, concatenate b+ and a two-letter ISO 639-1 language code, optionally followed by additional subtags separated by +.)

more specifically: -

Simplified:

values-b+zh (defaults to simplified) 
values-b+zh+Hans or
values-b+zh+Hans+CN (if you want to include regions)

Traditional:

values-b+zh+Hant or
values-b+zh+TW (defaults to traditional)
values-b+zh+Hant+TW (if you want to include regions)

you can look at https://tools.ietf.org/html/bcp47 for other interesting combinations. including: -

  zh-cmn-Hans-CN (Chinese, Mandarin, Simplified script, as used in
  China)

  cmn-Hans-CN (Mandarin Chinese, Simplified script, as used in
  China)

  zh-yue-HK (Chinese, Cantonese, as used in Hong Kong SAR)

  yue-HK (Cantonese Chinese, as used in Hong Kong SAR)

  zh-nan-TW (min nan Chinese, as used in Taiwan)

  zh-hak-CN (Chinese, Hakka, China).

remember to add -b and replace the - in each tag with +


This should be the correct answer, especially when you want to provide only 2 translations: Hans and Hant for all regions. Hans is used in Mainland China and Singapore, and Hant is used in Hong Kong and Taiwan. And if you want to provide audio content through text to speech, the 2nd part of this answer may be good.
K
Kerem Baydoğan

I have created this table, hope it helps someone.

CODE LANG FORM REGION zh Chinese - - zh_Hans Chinese Han Simplified - zh_Hans_CN Chinese Han Simplified China zh_Hans_HK Chinese Han Simplified Hong Kong SAR China zh_Hans_MO Chinese Han Simplified Macau SAR China zh_Hans_SG Chinese Han Simplified Singapore zh_Hant Chinese Han Traditional - zh_Hant_HK Chinese Han Traditional Hong Kong SAR China zh_Hant_MO Chinese Han Traditional Macau SAR China zh_Hant_TW Chinese Han Traditional Taiwan


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now