Jitesh Byahut 4/21/2014 2931

Culture And Regions In C#

When the end user request an Asp.Net page, this web page is executed on a thread from the thread pool. The thread has a Culture associated with it. You can get and set information about the culture of thread programmatically and check the particular information about culture.

See Current culture on any page in application

For Culture there are two properties in Page Class :

1. Culture

2. UICulture

The UICulture property is used to specify which resource files are loaded for the page. The resource files can contain all the text content of your pages translated into a particular language. You can set this property to any standard culture name.

The Culture property, on the other hand, determines how strings such as dates, numerals, and currency amounts are formatted. It also determines how values are compared and sorted. For example, by modifying the Culture property, you can display dates with language-specific month names such as January (English), Januar (German), or Enero (Spanish).

CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;

 

        string cultureName = ci.Name;

        string cultureParentName = ci.Parent.Name;

        string displayName = ci.DisplayName;

        string englishName = ci.EnglishName;

        string nativeName = ci.NativeName;

        string isoLanguage = ci.ThreeLetterISOLanguageName;

        string datetimeFormat = ci.DateTimeFormat.FullDateTimePattern;

       

 

 

        Response.Write("cultureName : " + cultureName + "</br>");

        Response.Write("cultureParentName : " + cultureParentName + "</br>");

        Response.Write("displayName : " + displayName + "</br>");

        Response.Write("englishName : " + englishName + "</br>");

        Response.Write("nativeName : " + nativeName + "</br>");

        Response.Write("datetimeFormat : " + datetimeFormat + "</br>");

        Response.Write("englishName : " + englishName + "</br>");

 

        // Output

        // cultureName : en-IN

        // cultureParentName : en

        // displayName : English (India)

        // englishName : English (India)

        // nativeName : English (India)

        // datetimeFormat : dd MMMM yyyy HH:mm:ss

        // englishName : English (India)

For example, if your application set to es-sn culture, then your page is displaying in that culture means, calendar, numeric values, date time and more displaying into that culture. See below image :

Now, we learn how to set culture in our application:

Defining a common culture for application in Web.Config, that reflect in all pages in application.

<configSections>

     <system.web>

         <globalization culture="ru-RU" uiCulture="ru-RU"/>

    </system.web>

</configSections>

We can change the thread's culture of any particular page.

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-RU");

        CultureInfo ci1 = System.Threading.Thread.CurrentThread.CurrentCulture;

        string cultureName1 = ci1.Name;

        Response.Write("cultureName1 : " + cultureName1);

        // Output

        // cultureName1 : ru-RU

We can define culture on page by directive

<%@ Page Language="C#" Culture="ru-RU" UICulture="ru-Ru" CodeFile="Default.aspx.cs" Inherits="Default" %>

Changing the culture to the end user's section

<%@ Page Language="C#" Culture="auto" UICulture="auto" CodeFile="Default.aspx.cs" Inherits="Default" %>

If the culture of the current end user prefers is not present then use any particular default culture

<%@ Page Language="C#" Culture="auto:en-US" UICulture="auto:en-US" CodeFile="Default.aspx.cs" Inherits="Default" %>


Please give your feedback for improving this page