The community is working on translating this tutorial into Hebrew, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".
Changing the active culture
In a previous chapter, we had a look at the CultureInfo class, and we briefly discussed how to change it for a page. However, since changing the current culture is so essential, this chapter will contain an in-depth look of the different ways to accomplish this.
Automatic
Both Culture and UICulture is automatically set based on the browser settings of your visitor. Try running the following page:
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CultureInfo demo</title>
</head>
<body>
<form id="MainForm" runat="server">
<% Response.Write("Your current culture: " + System.Globalization.CultureInfo.CurrentCulture.DisplayName); %>
</form>
</body>
</html>
Now, access your browsers language settings:
Internet Explorer: Click the Tools button, select Internet Options, then click the Languages button.
Firefox: Click the Tools menu, select Options, select the Content tab and then click the Choose button in the Languages group.
Add another language, and then move it to the top of the list. Close the dialog and reload the page (F5). You will now see the name of the culture matching your newly selected language.
The Page directive/class
As we saw in the previous chapter, we can simply define both Culture and UICulture by accessing the properties in the Page directive:
<%@ Page Language="C#" Culture="en-US" UICulture="en-US" %>
Since the Page directive is just a shortcut to the Page class, this can be done from CodeBehind as well. However, we have to do it at a certain point, before the page is being rendered, to make sure that it has the desired effect. This is where the InitializeCulture() method comes into play, a method that is called by ASP.NET pretty early in the Page life cycle, which you can override:
public partial class CultureInfoTest : System.Web.UI.Page
{
protected override void InitializeCulture()
{
Page.Culture = "en-GB";
Page.UICulture = "en-GB";
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Page.Culture);
}
}
You should be aware that the Page.Culture and Page.UICulture properties them self are merely shortcuts to the System.Threading.Thread.CurrentThread.CurrentCulture and System.Threading.Thread.CurrentThread.CurrentUICulture.
Globally
You can set the Culture and UICulture for all your pages, via the web.config file. Use the globalization node, which is a child node of the system.web node, like this:
<globalization uiCulture="en-US" culture="en-US" />
If needed, you can still override this setting on individual pages.