Blue Theme Orange Theme Green Theme Red Theme
 
MindFusion's Components
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » ASP.NET & Web Forms » Globalization and Localization in .NET

Globalization and Localization in .NET

Localization is a technique to implement local and culture-oriented applications. This articles explains usage of System.Globalization to implement localization support in .NET applications.

Technologies: .NET 1.0/1.1, .NET Compact Framework, ASP.NET 1.0, Windows Forms,Visual C# .NET
Total downloads :
Total page views :  58324
Rating :
 4.67/5
This article has been rated :  6 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
ArticleAd
Become a Sponsor




Globalization, Localization

To implementing a multilingual user interface, you design the user interface to open in the default UI language and offer the option to change to other languages.

Globalization is the first step in the process. A globalized application supports localized user interfaces and regional data for all users. Truly global applications should be culture-neutral and language-neutral. A globalized application can correctly accept, process, and display a worldwide assortment of scripts, data formats, and languages.

While your globalized application may possess such flexibility, ensure that you have separated the application's resources that require translation from the rest of the application's code. If you correctly test for localizability before proceeding to the localization step, you should not have to modify your application's source code during localization.

Globalization in .Net - CultureInfo, ResourceManager and Resgen

In .Net System.Globalization namespace contains classes that define culture-related information, including the language, the country/region, the calendars in use, the format patterns for dates, currency, and numbers, and the sort order for strings. There are around 20 classes in System.Globalization. Here we are only concern to CutureInfo class.

CutureInfo class represents information about a specific culture including the names of the culture, the writing system, and the calendar used, as well as access to culture-specific objects that provide information for common operations, such as formatting dates and sorting strings.

The System.Resources namespace provides classes and interfaces that allow developers to create, store, and manage various culture-specific resources used in an application. One of the most important classes of the System.Resources namespace is the ResourceManager class. The ResourceManager class allows the user to access and control resources stored in the main assembly or in resource satellite assemblies

The Resource File Generator (Resgen.exe) converts .txt files and .resx (XML-based resource format) files to common language runtime binary .resources files that can be embedded in a runtime binary executable or compiled into satellite assemblies.

Creating  .resources files

To proceed with our example, let us first create the .resources files. Here I am using six languages English, French, German, Spanish, Italian and Portuguese (Thanks to Google for this and don''t blame me there is anything wrong in conversion). To create .resources files. Create the following six .txt files in any folder as follows.

resource.en-US.txt - which contains the below English text.

0001=Please Enter User Name
0002=Please Enter Password
0003=User Login
0004=Submit
0005=Cancel
0006=Welcome
0007=How are you today?
0008=Select Language

resource.fr-FR.txt - which contains the below French text.

French.jpg

resource.de-DE.txt - which contains the below German text.

German.jpg

resource.es-ES.txt - which contains the below Spanish text.

Spanish.jpg

resource.it-IT.txt - which contains the below Italian text.

Italian.jpg

resource.pt-PT.txt - which contains the below Portuguese text.

Pourtoguese.jpg

Now open the visual studio command prompt and run the resgen.exe with .txt file as parameter as shown in image below.

Globalization1.gif

You will get resource.en-US.resources file after running the Resgen for resource.en-US.txt. Similarly you will get six .resources files for each .txt files.

Creating a Web Application to use .resources files.

Now open the visual studio and open the new c# web application. Add a new a web page Default.aspx. Now open the Default.aspx in HTML mode and paste the following code below the @ page directive.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

 <HEAD>

  <title>Default</title>

  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

  <meta name="CODE_LANGUAGE" Content="C#">

  <meta name="vs_defaultClientScript" content="JavaScript">

  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

 </HEAD>

 <body MS_POSITIONING="GridLayout">

  <form id="Form1" method="post" runat="server">

   <asp:RadioButtonList id="rdoLanguage" style="Z-INDEX: 101; LEFT: 60px; POSITION: absolute; TOP: 24px"

    runat="server" AutoPostBack="True" Font-Names="Verdana" Font-Size="Smaller">

    <asp:ListItem Value="en-US" Selected="True">English</asp:ListItem>

    <asp:ListItem Value="fr-FR">French</asp:ListItem>

    <asp:ListItem Value="de-DE">German</asp:ListItem>

    <asp:ListItem Value="es-ES">Spanish</asp:ListItem>

    <asp:ListItem Value="it-IT">Italian</asp:ListItem>

    <asp:ListItem Value="pt-PT">Portuguese</asp:ListItem>

   </asp:RadioButtonList>

   <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 76px; POSITION: absolute; TOP: 180px" runat="server"

    Font-Names="Verdana" Width="68px" Height="24px" Text="-->" Font-Bold="True" Font-Size="8"></asp:Button>

  </form>

 </body>

</HTML>

See the values of List Item en-US for English, fr-FR for French, de-DE for German, es-ES for Spanish, it-IT for Italian and pt-PT for Portuguese. Resources Files will be read using these values, as you will see this later.

Now Switch to design mode, you will see the following output in design mode

Globalization2.gif

Now in design mode click on the (-->) button. Default.aspx.cs will be opened. Write the following code in Button1_Click event

private void Button1_Click(object sender, System.EventArgs e)

{

          Server.Transfer("Login.aspx");

}

Add new Web Form to the project and rename it to Login.aspx. Now open the Login.aspx in HTML mode and paste the following code below the @ page directive.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

 <HEAD>

  <title>Login</title>

  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

  <meta content="C#" name="CODE_LANGUAGE">

  <meta content="JavaScript" name="vs_defaultClientScript">

  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

 </HEAD>

 <body MS_POSITIONING="GridLayout">

  <form id="Form1" method="post" runat="server">

   <TABLE id="Table1" style="Z-INDEX: 107; LEFT: 8px; WIDTH: 455px; POSITION: absolute; TOP: 8px; HEIGHT: 48px"

    cellSpacing="1" cellPadding="1" width="455" border="0">

    <TR>

     <TD align="center" colspan="2"><asp:Label id="Label3" runat="server" Width="268px" Height="9px" Font-Names="Verdana" Font-Size="9pt"

       Font-Bold="True">User Login</asp:Label></TD>

    </TR>

    <TR>

     <TD align="right"><asp:Label id="Label1" runat="server" Width="272px" Height="8px" Font-Names="Verdana" Font-Size="8">Label</asp:Label></TD>

     <TD><asp:TextBox id="TextBox1" runat="server" Width="166px" Height="22px" Font-Names="Verdana" Font-Size="8"></asp:TextBox></TD>

    </TR>

    <TR>

     <TD align="right"><asp:Label id="Label2" runat="server" Width="268px" Height="9px" Font-Names="Verdana" Font-Size="8">Label</asp:Label></TD>

     <TD><asp:TextBox id="Textbox3" runat="server" sWidth="166px" Height="24px" Font-Names="Verdana" Font-Size="8"

       Width="164px"></asp:TextBox></TD>

    </TR>

    <TR>

     <TD align="right">

      <asp:Button id="Button1" runat="server" Text="Submit" Font-Names="Verdana" Font-Size="8"></asp:Button></TD>

     <TD>

      <asp:Button id="Button2" runat="server" Text="Cancel" Font-Names="Verdana" Font-Size="8"></asp:Button></TD>

    </TR>

   </TABLE>

  </form>

 </body>

</HTML>

Now Switch to design mode, you will see the following output in design mode

Globalization3.gif

Now in design mode click on the submit button. Login.aspx.cs will be opened. Write the following code in Button1_Click event

private void Button1_Click(object sender, System.EventArgs e)

{

          Server.Transfer("Welcome.aspx");

}

Similarly for the cancel button, add the following code.

private void Button2_Click(object sender, System.EventArgs e)

{

          Server.Transfer("Default.aspx");

}

On the Page_Load event of the Login.aspx.cs add the following code

private void Page_Load(object sender, System.EventArgs e)

{

          if(!Page.IsPostBack)

          {

                   //Retrive the Language from Default page

                   string strCulture = Request.Params["rdoLanguage"].ToString();

                   //Set the Current culuture to session variable

                   Session["SelectCulture"]= strCulture;

                   //create the culture based upon session culuture

                   CultureInfo objCI = new CultureInfo(strCulture);

                   Thread.CurrentThread.CurrentCulture = objCI;

                   Thread.CurrentThread.CurrentUICulture = objCI;

                   //Read the rsources files

                   String strResourcesPath= Server.MapPath("bin");

                   ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("resource", strResourcesPath, null);

                   //Set the values read from resources to contorls

                   Label1.Text=rm.GetString("0001")+": ";

                   Label2.Text=rm.GetString("0002")+": ";

                   Label3.Text=rm.GetString("0003");

                   Button1.Text=rm.GetString("0004");

                   Button2.Text=rm.GetString("0005");

          }

}

Here you can see that how we have created the CulutureInfo object and passed the current selected culture string as a parameter to CulutureInfo. After that set the current page's culture to selected culture by using Thread.CurrentThread.CurrentCulture. Thread.CurrentThread.CurrentUICulture sets the current culture used by the Resource Manager to look up culture-specific resources at run time

Add new Web Form to the project and rename it to Welcome.aspx. Now open the welcome.aspx in HTML mode and paste the following code below the @ page directive.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

 <HEAD>

  <title>Welcome</title>

  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

  <meta name="CODE_LANGUAGE" Content="C#">

  <meta name="vs_defaultClientScript" content="JavaScript">

  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

 </HEAD>

 <body MS_POSITIONING="GridLayout">

  <form id="Form1" method="post" runat="server">

   <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 20px; POSITION: absolute; TOP: 36px" runat="server"

    Text="Select Another Language" Font-Size="8" Font-Names="Verdana" Width="152px"></asp:Button>

   <asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 20px; POSITION: absolute; TOP: 12px" runat="server"

    Font-Size="8" Font-Names="Verdana" Width="392px">Label</asp:Label>

  </form>

 </body>

</HTML>

Now Switch to design mode, you will see the following output in design mode

Globalization4.gif 

Now in design mode click on the (Select Another Language) button. Welcome.aspx.cs will be opened. Write the following code in Button1_Click event

private void Button1_Click(object sender, System.EventArgs e)

{

          Server.Transfer("Default.aspx");

}

On the Page_Load event of the Welcome.aspx.cs add the following code

private void Page_Load(object sender, System.EventArgs e)

{

          if(!Page.IsPostBack)

          {

                   string strCulture = Session["SelectCulture"].ToString();

                   CultureInfo objCI = new CultureInfo(strCulture);

                   Thread.CurrentThread.CurrentCulture = objCI;

                   Thread.CurrentThread.CurrentUICulture = objCI;

                   //Read the rsources files

                   String strResourcesPath= Server.MapPath("bin");

                   ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("resource", strResourcesPath, null);

                   string strUserName = Request.Params["TextBox1"].ToString();

                   Label1.Text=rm.GetString("0006")+", " + strUserName + "! " + rm.GetString("0007");

                   Button1.Text=rm.GetString("0008");

          }

}

Now coding part is complete. Copy the all six .resources files created in first step to the bin directory of your web application.

In the solution explorer right click Default.aspx page and set it as start up page. Now run the application.If everything is right in its place, you will see the following out

Globalization5.gif 

Select the language as English and click on the button. You will see the Login page

Globalization6.gif

Enter Any User/Pass and click on submit button. You will see the following output.

Globalization7.gif

Now click on Select Language and select French Language. You will see the Login page as follows

Globalization8.gif

Enter Any User/Pass and click on submit button. You will see the following output in French

Globalization9.gif

Similarly you can check for German, Spanish, Italian and Portuguese for this example.

Conclusion

Here we have seen that how System.Globalization helps us for localization the application   without modify application's source code. You can also explore System.Globalization for more languages and objects like RegionInfo, DateFormats etc.


Login to add your contents and source code to this article
 Article Extensions
Contents added by sanjay kumar on May 17, 2009
 [Top] Rate this article
 About the author
 
Anand Thakur

 

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
aspIndhu7/5/2006
details about asp.net
Reply | Email | Delete | Modify | 
 
 
Re: aspDotNetGuts8/23/2008

Explanation of Globalization and Localization Practically

http://dotnetguts.blogspot.com/2007/02/ease-of-applying-globalization-and.html

Reply | Email | Delete | Modify | 
put code in Global.aspx filesheir7/5/2006

Hi,

Is it not better to put the resourcemanager into the Application storage in the Global.aspx file in the Application_Start event.  So the RM is created once for the entire life of the application and is just used afterwards.

Also the code for setting the resource dll can be set in the Application_OnAcquireRequestState event of the Global.aspx file,

where the Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture can be set to the new culture.

 

Just my $0.02

sheir

 

 

Reply | Email | Delete | Modify | 
need help ......murlidakhare8/13/2006

 hello,

how r u, i need help.

i have to apply multi language to site for dutch, english, german, whole content is coming from DB which is in english, but it should be displayed in languages selected language.

how can i do it.

thanx

Murli Dakhare

murlidakhare@gmail.com 

Reply | Email | Delete | Modify | 
 
 
Re: need help ......Avinash12/3/2007

hi,

Have  u got solution for this...

Regards,

Avi

Reply | Email | Delete | Modify | 
Resource Load Combo Box Code?Toni10/10/2007
Hi, Can anyone help me??? I am looking for the resource load code for a combox box. For instance the code for a label is: this.label5.Text = res.GetString("Component List"); Thanks,
Reply | Email | Delete | Modify | 
nice exampleaditya3/11/2008
this code worked in first go, no compilation errors no run time errors
Reply | Email | Delete | Modify | 
Best Localization Plug-in for Visual Studio.Alexander9/24/2008
RGreatEx is a productivity plug-in for JetBrain ReSharper. Once installed, it adds a set of new refactorings and coding assistance features to Visual Studio, allowing developers to localize a project faster and more conveniently than before, improve coding with new time-saving abilities and generate safer code. RGreatEx integrates into Visual Studio seamlessly and works with it without errors. Supported languages are C#, VB.NET and ASP.NET. The power of RGreatEx lies in its pain-free way to add localization support to a .NET application. This can be achieved thanks to the new refactorings, such as “Move to resource” and “Rename resource”. The tool can automatically analyze strings, resources and their usage, detect strings that should be extracted to resources and resources that haven’t been localized yet. The use of RGreatEx helps to save up to 95% of development time as there’s no need to re-write the entire code to integrate localization support to a project. Adding localization support requires little efforts. Simply select a string and choose “Move string to resource” from the quick fixes, select a resource file where you want to extract and refactoring settings. Then you copy your resource file to a resource file for localization – for instance if you have Resources.resx you need to make a copy with the name – Resources.xx-xx.resx (where xx-xx – is your specific culture). The final step is to translate values in Resources.xx-xx.resx by using the Translate resource quick fix. In addition to localization, RGreatEx will help you make coding much easier and enjoyable. You’ll be able to manipulate strings on the fly thanks to the set of new context actions and highlighting options and produce safer resx code. Unique to RGreatEx is an opportunity to select strings from a file, folder, or a project, choose a resource file and keys for the selected strings and move them to resources – all in one go. Download RGreatEx from www.safedevelop.com.
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved