How to: Set up a regional and UI culture for ASP. NET web page globalization (msdn)

Source: Internet
Author: User

On the ASP. NET webpage, you can set two regional values, namely, the Culture and UICulture attributes.CultureDetermines the result of the function related to the region, such as the date, number, and currency format.UICultureDetermines which resources are loaded on the page.

Note:

CultureAndUICultureThe attribute is an Internet standard string that uses the identification language (for example,EnRepresenting English,EsRepresenting Spanish,DeA standard Internet string (for example,USRepresenting the United States,GBRepresenting the UK,MXMexico,DEFor Germany. Some examples include:En-USEnglish/USA,En-GBEnglish/UK,Es-MXSpanish/Mexico. For more information, see CultureInfo.

These two regional settings do not need to have the same value. Depending on your application, it may be important to set them separately. The Web auction site is such an example. For each Web browser,UICultureAttributes may change, whileCultureRemain unchanged. Therefore, prices are always displayed in the same currency and format.

CultureThe value can only be set to a specific culture, as shown in figureEn-USOrEn-GB. In this way, no identifier is requiredEn(For this string,En-USAndEn-GBHas different currency symbols.

Users can set the culture and UI culture in their browsers. For example, on the "Tools" menu of Microsoft Internet Explorer, you can click "Internet Options", "general" tab, and "language", and set their language preferences. IfEnableClientBasedCultureSet propertyTrueASP. NET can automatically set the webpage culture and UI culture based on the value sent by the browser.

It is not the best practice to determine the UI culture of a webpage based on browser settings. Browsers used by users are generally not set as their preferences (for example, in an Internet cafe ). You should provide users with an explicit selection of the language, language, and culture of the page (CultureInfoName.

Set the culture and UI culture of ASP. NET web pages in declarative Mode
  • To set the culture and UI culture of all pages, addGlobalizationAnd then setUicultureAndCultureProperties, as shown in the following example:

    Copy code

    <globalization uiculture="es" culture="es-MX" />
  • To set the culture and UI culture of a single Page, setCultureAndUICultureProperties, as shown in the following example:

    Copy code

    <%@ Page UICulture="es" Culture="es-MX" %>
  • To enable ASP. NET to set the culture and UI culture to the first language specified in the current browser settings, SetUICultureAndCultureSetAuto. You can also set this valueAuto:Culture_info_name, WhereCulture_info_nameIs the regional name. For a list of regional names, seeCultureInfo. You can perform this setting in the @ Page command or the Web. config file.

Set the culture and UI culture of ASP. NET web pages programmatically
  1. Override the InitializeCulture method of the page.

  2. In the override method, determine the language and culture to set for the page.

    Note:

    InitializeCultureThe method is called in a very early period of the page lifecycle. At this time, no controls have been created for the page, and no properties have been set for the page. Therefore, to read the values passed from the control to the page, you must use the Form set to directly obtain these values from the request.

  3. You can set the culture and UI culture in one of the following ways:

    • ClickCultureAndUICultureSet the attribute to a language and a regional string (for exampleEn-US). These two properties are the internal properties of the page and can only be used in the page.

    • Set the CurrentUICulture and CurrentCulture attributes of the current thread to the UI culture and culture respectively.CurrentUICultureThe attribute uses a language and regional information string. To setCurrentCultureAttribute, Please createCultureInfoClass and call its CreateSpecificCulture method.

    The following code example shows an ASP. NET webpage that allows users to select their preferred language from the drop-down list. This page imports two namespaces, making it easier to use thread processing classes and global classes.

    Visual Basic

    Copy code

    <%@ Page Language="VB" uiculture="auto" %><%@ Import Namespace="System.Threading" %><%@ Import Namespace="System.Globalization" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML   1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><script runat="server">    Protected Overrides Sub InitializeCulture()        If Request.Form("ListBox1") IsNot Nothing Then            Dim selectedLanguage As String = _                Request.Form("ListBox1")            UICulture = Request.Form("ListBox1")            Culture = Request.Form("ListBox1")            Thread.CurrentThread.CurrentCulture = _                CultureInfo.CreateSpecificCulture(selectedLanguage)            Thread.CurrentThread.CurrentUICulture = New _                CultureInfo(selectedLanguage)        End If        MyBase.InitializeCulture()    End Sub</script>

     

    C #

    Copy code

    <%@ Page Language="C#" uiculture="auto" %><%@ Import Namespace="System.Threading" %><%@ Import Namespace="System.Globalization" %><script runat="server">protected override void InitializeCulture(){    if (Request.Form["ListBox1"] != null)    {        String selectedLanguage = Request.Form["ListBox1"];        UICulture = selectedLanguage ;        Culture = selectedLanguage ;        Thread.CurrentThread.CurrentCulture =             CultureInfo.CreateSpecificCulture(selectedLanguage);        Thread.CurrentThread.CurrentUICulture = new             CultureInfo(selectedLanguage);    }    base.InitializeCulture();}</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.