ASP.NET Globalization: Culture=”auto” page directive with neutral culture crash

asp.netcultureinfoglobalization

I'm running into a case where an ASP.NET application using the built-in globalization facilities is crashing.

On an ASP.NET page with the Culture="auto" directive, a user with a neutral culture as their browser language (such as "zh-Hans") will produce the following exception:

Culture 'zh-Hans' is a neutral culture. It cannot be used in
formatting and parsing and therefore
cannot be set as the thread's current
culture.

at System.Globalization.CultureInfo.CheckNeutral(CultureInfo
culture)
at System.Threading.Thread.set_CurrentCulture(CultureInfo
value)
at System.Web.UI.Page.set_Culture(String
value)
at ASP.somePage_aspx.__BuildControlTree(somePage_aspx __ctrl)
at ASP.somePage_aspx.FrameworkInitialize()

Any ideas? Garbage fed into the Culture/UICulture parameters generally seem to be ignored, but this case is causing an unhandled exception.

Best Answer

I was having the same problem and after bonking my head against a wall for a while found the answer right under my nose.

The issue I had was in not understanding the difference between CurrentCulture and CurrentUICulture. The difference being CurrentCulture is used to format dates, numbers and perform sorting, CurrentUICulture is used to lookup culture specific strings from a resource.

I had some code that looked like

return input.ToString("C", System.Globalization.CultureInfo.CurrentUICulture);

when it should be been

return input.ToString("C", System.Globalization.CultureInfo.CurrentCulture);

When you start trying to format culture specific items with a non-specific culture you will get the System.NotSupportedException.