2

我正在开发 WPF 应用程序,并且我在 .resx 文件中有本地化资源(en、fr、zh)。以下测试代码用于显示本地化字符串。它适用于英语,法语,但在中文中失败。在中文中它只显示英文文本。我尝试使用中国文化的所有变体,例如 zh-CN、zh-Hans、zh-Hant 和“旧”的 zh-CHS、zh-CHT,但没有运气。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
       // CultureInfo culture = CultureInfo.CreateSpecificCulture("zh-CHT");
        CultureInfo culture = new CultureInfo("zh-CN");
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;
        MessageBox.Show(Properties.Resources.address);
    }
}

资源字段被命名为 Resources.resx、Resources.fr.resx、Resources.zh.resx 任何帮助将不胜感激,Rajesh

4

2 回答 2

1

It's because zh-CHS is Neutral Culture. In ASP.NET 2.0 it\s not possible to set CurrentCulture to Neutral Culture like zh-CHS. But you can set it to a Specific Culture like zh-CN. If you need to use a Neutral Culture, like zh-CHS, you can set CurrentUICulture property instead. CurrentUICulture is the one that is responsible for getting text from Resource files. CurrentCulture is responsible for Number, DateTime and Currency formatting.

BTW, in ASP.NET 4.0 you can set CurrentCulture to Neutral Culture, but not in v2.0. In my code, where the site could run on either .NET 2 or .NET 4 I do it like this:

    public static void SetCurrentCulture(string cultureName)
    {
        try
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
        }
        catch (Exception)
        {
            // Ignore if exception happens: In ASP.NET 2.0 setting CurrentCulture = Neutral culture (like zh-CHS) throws an exception:
            // Culture 'zh-CHS' is a neutral culture. It cannot be used in formatting and parsing and therefore cannot be set as the thread's current culture.
        }
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
        HttpCookie cultureCookie = new HttpCookie("CultureCookie", cultureName);
        HttpContext.Current.Response.Cookies.Set(cultureCookie);
    }
于 2014-09-10T16:00:31.910 回答
0

我有一个类似的问题。当前的文化是在 zh-CN 开始时设置的,但被英语覆盖了。就我而言,解决方案是从 Windows 设置中安装中文。

于 2015-06-19T14:06:35.457 回答