When I was working on multilanguage/culture web application, I knew about UICulture setting but didn’t know about culture until there is a need for it. So I thought it’s good topic to share.
This blog post is all about
- difference between UICulture and Culture (UICulture vs Culture)
- when you need to set UICulture and Culture
- how to set UICulture and Culture declaratively and programmatically.
UICulture value determines what resource file ASP.NET needs to load. That drives all the content of the website.
Culture value determines the results of culture-dependent functions, such as the date, number, and currency formatting, and so on.
It is NOT necessary that you need to have both values are same. You can set different values as per your application needs. Good example is that Web Auction web site. You won’t to display english text for english speaking country but you might want to display spanish text for spanish speaking country. This setting is UICulture. But there is one thing that would stay constant “currency” for both english or spanish speaking country. This setting is Culture.
You can set UICulture and Culture properties to ASP.NET web page.
<%@ Page UICulture="es" Culture="es-MX" %>
You can also set UICulture and Culture for all pages by adding globalization section in web.config.
<globalization uiCulture="es" culture="es-MX" />
And if your site has a feature to allow user to choose their preferred language(on website landing page you ask user to pick their preffered language), you need to overrider InitializeCulture method of page class and set appropriate value.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
Note: In above example I set “es” to uiCulture. You can’t set just language like “es” to culture because that is NOT a valid culture. It has to be specific culture. FYI “es” is a neutral culture and “es-MX” is a specific culture.
One more important things I need to discuss is about resource fallback process. In my case I had specified “en-US” for UICulture settings and the resource file that I had ResourceFile.resx. .NET first looks for ResourceFile.en-US.resx resource file because I set en-US UICulture in web.config. If it doesn’t find ResourceFile.en-US.resx, then it looks for ResourceFile.en.resx(netural culture file). Eventhough it doesn’t find ResourceFile.en.resx, then it looks for ResourceFile.resx. So you can see in this case it is searching unnecessary bcoz I didn’t think of that. In my case I had all the english text in .resx file and I set “en-US” uiculture in web.config. So I have to rename all my .resx resource file to .en-US.resx file to reduce extra search steps.