HOW TO: Set Current Culture Programmatically in an ASP.NET Application

This step-by-step article describes how to change the
current culture and current UI culture in an ASP.NET application. In the .NET
Framework, the CultureInfo class from the System.Globalization namespace provides
culture-specific information such as the associated language, country/region,
calendar, and cultural conventions. The CurrentCulture property represents the
culture that the current thread uses. The CurrentUICulture property represents the current
culture that Resource Manager uses to look up culture-specific resources at
run time. There are three ways to set the Culture information in an ASP.NET
application, Application Level, Page Level, and Thread Level.

Application Level

Specify Application Level Culture information in the
Web.config file. To do this, follow these steps:

  1. Start Notepad (or any other text editor).
  2. Paste the following code in Notepad:
    
      
        
      
    

  3. Save the text file as Web.config in the root folder of
    your Web server. For example, C:InetpubwwwrootWeb.config.
  4. Start another instance of Notepad. Paste the following code
    in Notepad:
    <%@Page Language="C#" %>
    <% @Import Namespace="System.Globalization" %>
    
          
       
    
       
    
    

  5. Save this text file as Application.aspx in the root folder
    of your Web server.
  6. Start Internet Explorer, and then open
    Application.aspx.

Page Level

The Page Level Culture-specific information settings override the
Application Level Culture-specific information settings. Specify Page Level Culture information by using the @Page
directive. To do this, follow these steps:

  1. Follow steps 1, 2, and 3 of the “Application Level” section to
    create the Web.config file.
  2. Start another instance of Notepad. Paste the following code
    in Notepad:
    <%@Page Culture="fr-FR" Language="C#" %>
    <% @Import Namespace="System.Globalization" %>
    
       
       
          
    
       
    

  3. Save this text file as Page.aspx in the root folder of your
    Web server.
  4. Start Internet Explorer, and then open Page.aspx. Note that
    the Current Culture setting is French, although Japanese is specified as the current
    culture in the Web.config file.

Thread Level

The Thread
Level Culture-specific information settings override the Page Level
Culture-specific information settings. Specify the Thread Level Culture-specific information by setting
the CurrentCulture property or the CurrentUICulture property of the current thread. To do this, follow these steps:

  1. Follow the steps 1, 2, and 3 of the “Application Level” section to
    create the Web.config file.
  2. Start another instance of Notepad. Paste the following code
    in Notepad:
    <% @Page Culture="fr-FR" Language="C#" %>
    <% @Import Namespace="System.Globalization" %>
    <% @Import Namespace="System.Threading" %>
    
       
       
          
    
       
       
    

  3. Save this text file as Thread.aspx in the root folder of your
    Web server.
  4. Start Internet Explorer, and then open Thread.aspx. Note that
    the Current Culture setting is German, although French is specified at the page level and
    Japanese is specified in the Web.config file.

For more information, see the Microsoft .NET
Framework SDK Documentation or visit the following Microsoft Web sites:

Read More:
HOW TO: Set Current Culture Programmatically in an ASP.NET Application

Comments are closed.

Recent Comments

    Archives

    Categories