Reputation: 417
I'm working on WPF application and I have localized resources (en,fr,zh) in .resx files. Following test code is used to display the localized string. It works fine for english, french but fails in Chinese. In chinese it shows english text only. I tried using all variants of Chinese culture such as zh-CN, zh-Hans, zh-Hant and the "old" zh-CHS, zh-CHT but no luck.
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);
}
}
Resource fiels are named as Resources.resx, Resources.fr.resx, Resources.zh.resx Any help would be appreciated, Rajesh
Upvotes: 2
Views: 3444
Reputation: 117
I had a similar problem. The current culture was set at some point on start to zh-CN but was overwritten by English. Solution in my case- to install the Chinese language from Windows settings.
Upvotes: 0
Reputation: 6640
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);
}
Upvotes: 1