0

I have this C# code:

Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);    
dateScale.EndDate.ToString("dd/MMMM", CultureInfo.CurrentCulture);

If I set culture to be "zh-HK", in ASP.NET the output is in English. But when the same logic is ran as a unit test (so same as running as a WinForms app), the output is in Chinese.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
daxu
  • 3,514
  • 5
  • 38
  • 76
  • 2
    Where did you set the thread culture? Web applications use a large number of threads. Setting it on one thread doesn't guarantee it will be there for the next request. Besides, why are you passing the current culture to ToString()? It's the default. – Panagiotis Kanavos Dec 11 '13 at 12:43
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 11 '13 at 14:11

2 Answers2

0

The server where you are running your asp.net website have the correct Language Packs installed?

Default .NET framework instalations, have language neutral binaries. I belive that for chinese you need to install Language specific Language Packs.

You can get more info on here

http://msdn.microsoft.com/en-us/library/vstudio/5a4x27ek(v=vs.100).aspx

Some examples of these packages are dotNetFx40LP_Full_x86_x64de.exe (for the German - Germany culture) and dotNetFx40LP_Full_x86_x64ja.exe (for the Japanese culture).

UPDATE: As Panagiotis Kanavos wrote, this is only for string output, like days of the week. Why are you setting the culture in the Thread? You should specify the culture in the web.config.

<globalization uiCulture="zh-HK" culture="zh-HK" />
Paulo Correia
  • 616
  • 5
  • 15
0

Web applications use multiple threads to service a large number of requests. Setting the culture in one thread doesn't mean it will be available in others.

Furthermore, the culture used for each request depends on globalization settings in web.config, page-level settings and the end user's language preferences. Even if you set the culture for one request, it will be reset when the thread is reused to service another request.

Actually, there is no reason to change the current thread's culture if you want to format for a specific culture. The following code will work without problem:

var culture = new CultureInfo("zh-hk");    
Console.WriteLine(DateTime.Now.ToString("dd/MMMM", culture));

This returns 11/十二月 (.NET Fiddle here).

Check How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization for the settings you need to set to ensure a specific culture is used for globalization.

First, if you want to use "zh-hk" for all pages, you can add the following setting in web.config:

<system.web>
  <globalization uiCulture="zh" culture="zh-HK" />
</system.web>

Second, if you want to set the culture programmatically on a page, depending on some criteria (eg a query parameter or user profile setting), you can override InitializeCulture and set the culture you want in the UICulture and Culture properties.

Finally, if you want to create language-specific pages, you can set the culture on each page's Page directive:

<%@ Page UICulture="zh" Culture="zh-HK" %>
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • It is certainly true and better to create a culture and use that. var culture = new CultureInfo("zh-hk"); Console.WriteLine(DateTime.Now.ToString("dd/MMMM", culture)); But this doesn't solve the issue, as it still outputs English other than Chinese. The reason for me to set this in code other than page level is that I am writing a component that suppose to be culture aware. This component can be hosted in a asp.net page or wcf service for example. Base on the parameters user passing in, I need to output the right text for the culture. – daxu Dec 11 '13 at 13:50
  • The [.NET Fiddle](http://dotnetfiddle.net/wdbzuZ) of the example returns `11/十二月`. What did you try and what did you get back? Anyway in your case, you should use the 2nd option, override InitializeCulture and set the page's Culture properties – Panagiotis Kanavos Dec 11 '13 at 14:14
  • The net fiddle example uses console.write, which will return chinese word. But if you put the same logic in asp.net, the output will be english. – daxu Jan 08 '14 at 21:47
  • What code did you try? String formating occures in ToString(), not Console.WriteLine, so there is no logic to change. Besides, I did reference the various ways you can set the culture in ASP.NET without explicitly using it in every conversion. – Panagiotis Kanavos Jan 09 '14 at 08:46
  • It works for me for all different languages except the Chinese. Is there something special needs to b done for it? – Mark Jul 02 '21 at 13:35
  • @Mark not in the original question. This question was asked 8 years ago about ASP.NET WebForms. We're in the ASP.NET Core age now. The same code in .NET Core returns `02/7月` which is clearly a bug - but .NET Fiddle runs on Linux, so this could be a Linux/ICU problem. What runtime are you using? – Panagiotis Kanavos Jul 02 '21 at 14:56
  • I think it is .NET 4.8. I have an ASP page where other languages work just fine by specifying UICulture = "...". For Chinese I tried "zh-CN", "zh_HK", "zh_HANS", nothing works. – Mark Jul 02 '21 at 15:02