Matthew Watson
Matthew Watson

Reputation: 109577

Chinese resources don't work with .Net 3.5

We have a strange bug with our localised resources in Chinese, when using .Net 3.5. This problem affects console apps AND Windows Forms apps, and it's easy to reproduce.

Basically, the Chinese resources aren't being used, but other resources (for example, French and English) ARE being used. Furthermore, it works fine if we use .Net 4.x.

It goes wrong on Windows 7 and Windows 10 (I haven't tried Windows 8.x, but I expect that would go wrong too).

It's easy to reproduce with a Console application:

  1. Create a default .Net 3.5 Console application.
  2. Add a default resource file, "Resources.resx" and add to it a name/value pair: Test, I'm English
  3. Add a "Resources.fr.resx" file and add to it a name/value pair: Test, I'm French
  4. Add a "Resources.zh.resx" file and add to it a name/value pair: Test, I'm Chinese
  5. Add the code below to the Main() method

Code:

System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
Console.WriteLine(System.Threading.Thread.CurrentThread.CurrentUICulture);
Console.WriteLine(ConsoleApplication1.Properties.Resources.Test);

(The first line is merely a convenience so that we can test this code on a non-Chinese system. We can remove that line when running on an actual Chinese system because the CurrentUICulture locale is going to be already Chinese without having to modify it - and the bug still occurs.)

In Control Panel | Region, change Format to English (United States) and run the program. The output will be:

en-US
I'm English

Now change the region format to French (France) and run the program. The output will be:

fr-FR
I'm French

Now change the region format to Chinese (Simplified, China) and run the program. The output will be:

zh-CN
I'm English

Note that it's not working.

Now change the project properties to build a .Net 4 or later version, without changing anything else, and re-run it. Now the output is:

zh-CN
I'm Chinese

So it works with .Net 4!

My questions are:

Upvotes: 1

Views: 1155

Answers (1)

Matthew Watson
Matthew Watson

Reputation: 109577

Thanks to Hans' comment (which was the correct answer) I have the complete answer now.

It seems that in .Net 3.5, "zh" is NOT recognised as a neutral Chinese culture.

In .Net 4.x, "zh" IS recognised as a neutral Chinese culture.

We can see the difference by comparing the .Net 3.5 documentation for CultureInfo.IsNeutralCulture with the .Net 4.0 documentation.

The .Net 3.5 documentation lists the following neutral and specific resource names:

zh-CHS Chinese (Simplified)                    : neutral
zh-TW  Chinese (Taiwan)                        : specific
zh-CN  Chinese (People's Republic of China)    : specific
zh-HK  Chinese (Hong Kong S.A.R.)              : specific
zh-SG  Chinese (Singapore)                     : specific
zh-MO  Chinese (Macao S.A.R.)                  : specific
zh-CHT Chinese (Traditional)                   : neutral

The .Net 4.0 documentation changes to:

zh-Hans Chinese (Simplified)                    : neutral
zh-TW   Chinese (Traditional, Taiwan)           : specific
zh-CN   Chinese (Simplified, PRC)               : specific
zh-HK   Chinese (Traditional, Hong Kong S.A.R.) : specific
zh-SG   Chinese (Simplified, Singapore)         : specific
zh-MO   Chinese (Traditional, Macao S.A.R.)     : specific
zh      Chinese                                 : neutral
zh-Hant Chinese (Traditional)                   : neutral
zh-CHS  Chinese (Simplified) Legacy             : neutral
zh-CHT  Chinese (Traditional) Legacy            : neutral

We can see that .Net 4.0 has gained "zh" as a neutral Chinese culture.

So this is not a bug in .Net 3.5, but a documented feature.

If you need to be compatible with both .Net 3.5 and .Net 4.x it seems that you need to use "zh-CHS" (for Simplified Chinese), although it is not clear whether this will continue to work indefinitely.

Upvotes: 1

Related Questions