Chinese resources don't work with .Net 3.5

时间:2016-04-15 15:04:15

标签: c# localization .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:

  • Can anyone else reproduce this?
  • Is this a known bug?
  • Is there a workaround?

1 个答案:

答案 0 :(得分:1)

感谢Hans'评论(这是正确的答案)我现在有完整的答案。

似乎在.Net 3.5中," zh"不被认为是中立的中国文化。

在.Net 4.x中," zh"被公认为中立的中国文化。

我们可以通过比较CultureInfo.IsNeutralCulture的{​​{3}}和the .Net 3.5 documentation来看到差异。

.Net 3.5文档列出了以下中性和特定资源名称:

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

.Net 4.0文档更改为:

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

我们可以看到.Net 4.0获得了" zh"作为一个中立的中国文化。

所以这不是.Net 3.5中的错误,而是一个记录在案的功能。

如果您需要兼容.Net 3.5和.Net 4.x,您似乎需要使用" zh-CHS" (对于简体中文),虽然目前尚不清楚这是否会继续无限期地发挥作用。