Skip to main content
A newer version of this page is available. .

How to: Set a Culture for an ASP.NET Web Page

  • 3 minutes to read

To localize a website, it must first be supplied with the required localization sources using the Satellite Resource Assemblies approach. The current site culture is defined by the Culture and UICulture properties, which can be specified in two different ways.

Localize a Website for a Specific Culture (regardless of operating system settings)

To localize a website for a certain culture, set the Culture and UICulture properties to the corresponding culture name. For a list of culture names, see the National Language Support (NLS) API Reference topic in MSDN. The properties can be set using the following approaches.

Declarative approaches

  • To set the culture for all pages, add a globalization element to the Web.config file, and then set the Culture and uiCulture attributes.

    <system.web>
         <globalization uiCulture="es" culture="es" />
         ...
    </system.web>
    
  • To set the culture for an individual page, set the Culture and UICulture attributes of the @ Page directive.

    <%@ Page UICulture="es" Culture="es" %>
    

Programmatic approach

  • You can set the Culture and UICulture property values at runtime. For this purpose, override the InitializeCulture method where you must specify the Culture and UICulture properties. The programmatic approach can be useful if you need to change an application’s language at runtime.

    Example

    This example demonstrates how to allow end users to change the site language at runtime. For this purpose, the InitializeCulture method is overridden to set the Culture and UICulture properties according to query values.

    The image below shows the result.

    Localization_InitializeCulture

    Note

    To work properly, the project requires satellite resource assemblies.

    <style type="text/css">
         .lang-btn
         {
              text-decoration: none;
         }
    </style>
    ...
    <a class="lang-btn" href="Default.aspx?lang=en">
         <img src="Images/english.png" alt="English" />
    </a>
    <a class="lang-btn" href="Default.aspx?lang=de">
         <img src="Images/german.png" alt="Deutsch" />
    </a>
    <a class="lang-btn" href="Default.aspx?lang=es">
         <img src="Images/spanish.png" alt="Spanish" />
    </a>
    
    <dx:ASPxGridView ID="ASPxGridView1" ...
    </dx:ASPxGridView>
    
    protected override void InitializeCulture() {
         var lang = Request.QueryString["lang"];
         if (!string.IsNullOrEmpty(lang)) {
              Culture = lang;
              UICulture = lang;
         }
    }
    

Localize a Website for Numerous Cultures (based on the operating system settings)

This is the most common approach to localization. To apply it, provide multiple localization sources within your website for each culture to support. Then, set the Culture and UICulture properties to auto. After that, the program will automatically determine the operating system’s culture info and load assemblies appropriate for this culture, so you do not have to write any code. You can set the Culture and UICulture property in any of the ways described above. For example:

<%@ Page UICulture="auto" Culture="auto" %>

End-users can manually set the culture in their browsers. For example, in Microsoft’s Internet Explorer, users can click Tools | Internet Options. In the invoked dialog, they can select the General tab, click Languages, and then set their language preference.

LocalizationIELanguage

See Also