What is the use of Culture ,UICulture and CultureInfo class in asp.net web applications

By
CultureInfo Class :- 
  • The CultureInfo Class resides in the System.Globalization namespace.
  • The CultureInfo class contains culture specific information,such as the language,country,and cultural conventions that are associated with a specific culture.
  • The CultureInfo class is used to performing culture -specific operations such as formatting dates and numbers,casting,and comparing strings etc.
  • To find the all culture list, you can use CultureInfo.GetCultures method in culture info class.
  • See below application,here i have used cultureInfo Class to display the culture list in dropdownlist control.
A Web page in a web application consists of two culture value:-
  1. Culture
  2. UICulture
Culture :-
  • The Culture value determines the functions,such as date and currency.
  • The Culture values are used to format data and numbers in a web page. 
UICulture:- 
  • The UICulture value determines the resources (resource file) that are loaded on the web page of a web application.
  • The resources in a web application represent data,such as strings or images ,displayed in the UI of the web application.
  • A resource file is a XML file which contains culture specific information.
There are three methods used to Set the Culture and UICulture values in a web application as given below:-
  • Write the code in web.config file
  • Write the code in  inline page ( using Page Directive)
  • Write the code in code behind file 
You can set the Culture and UICulture values in a web application to override the user settings or operating system settings
1.) Write the code in web.config file
To set the Culture and UICulture value through the web.config file as given below.
culture="en-us"
uiculture="fr-FR"
                      OR
culture="auto"
uiculture="auto"

If you don't specify any culture on the web page ,you can enable this auto culture handling technique on the web page.ASP.NET provides a auto-culture handling feature that enables you to localize a web application.
  • en-us means English -US language
  • fr-FR means French -Franc language 
2.) Write the code in  inline page ( using Page Directive)
To set the Culture and UICulture value in source code of the web page, you need to include the following values to the page directives as given below.
<%@ Page Language ="C#" culture="en-us" uiculture="fr-FR" CodeFile="Default.aspx.cs" Inherits="_Default" %>
                                                               OR
<%@ Page Language = "C#" culture="auto" uiculture="auto" CodeFile="Default.aspx.cs" Inherits="_Default" %>

3.) Write the code in code behind file 
In the code-behind file, you can set the default culture for a web application.ASP.NET provides the CurrentCulture and CurrentUICulture properties to set the default values for the Culture and UICulture attributes in the code behind file.
using System.Globalization;
using System.Threading;
Thread.CurrentThread.CurrentCulture=CultureInfo.CreateSpecificCulture("fr-FR")
Thread.CurrentThread.CurrentCulture= new CultureInfo("fr-FR");
Display the all Culture values in Dropdown list control
Now here ,I will show you "how to display all culture values in dropdown list control in asp.net using c#" .
Step 1 :-First open your visual studio --> File--> New--> Web Site --> Select ASP.NET Empty Website --> OK.Open Solution Explorer--> Add a New Web Page (Default.aspx)--> Now Drag and DropDropDownList Control from the toolBox on the page as shown below:-

Step 2 :- Now Writes the c# codes on default.aspx.cx page as given below:-
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Threading;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      CultureInfo[] mculture =CultureInfo.GetCultures(CultureTypes.AllCultures);
      foreach (CultureInfo s in mculture)
        {
         
            DropDownList1.Items.Add(s.ToString());
        }
    }
}

Step 3 :- Now Run the Application(press F5)-->You will see following output as shown below:-
Note:- 

  • I will explain each concepts with an example in our coming post.
  • I will also explain,how to use Local resource and Global resource files in asp.net web application with an example. 

For More...
  1. oops concepts in c#.net
  2. Create captcha in asp.net application easily
  3. Learn Complete C# Concepts with examples
  4. Learn Sql server concepts with examples
  5. Learn .NET Interviews Questions and Answers for job seekers

0 comments:

Post a Comment

Powered by Blogger.