What is Globalization and Localization in ASP.Net

Before starting, let's get these two terms straight.

Globalization is the process of designing the application in such a way that it can be used by users from across the globe (multiple cultures).

Localization, on the other hand, is the process of customization to make our application behave as per the current culture and locale. These two things go together.

To do globalization we need to use Resource Files.

Let's Start
 
To add Resource Files to our project:

  1. Right-click on the Root Folder Add New Item.
  2. Select Resource File from the above Templates.
  3. Name it "Resource.resx"
  4. You need other Resource Files for storing data depending on the Culture
  5. Add another file with the following names of each resource, such as "Resource.en-GB.resx" else you can give a different name but after the (Dot ) because they are sub-files of "Resource.resx".
     
    • For English you can add "Resource.en-GB.resx"
    • For Marathi you can add "Resource.mr-IN.resx"
    • For Hindi you can add "Resource.hi-IN.resx"
    • For Arabic you can add "Resource.ar-SA.resx"

Localization and Globalization1

Localization and Globalization2

Localization and Globalization3
 

1. Let us add some data in these files; open "Resource.resx"

Add a name under the "Resource.resx" Filename Column and keep the Value column blank as in the following:

Save And Close Resource.resx
Save And Close Resource.resx

2.  Open "Resource.en-GB.resx"

  • Add a name under the "Resource.en-GB.resx" Filename and the value that you want to display in the value field.

    Open Resource.en-GB.resx

3. Open Resource.hi-IN.resx

  • Add a name under the "Resource.hi-IN.resx" Filename and the Value that you want to display in the value field.

    Open Resource.hi-IN.resx

4. Open "Resource.mr-IN.resx"

  • Add a name under the "Resource.mr-IN.resx" Filename and the Value that you want to display in the value field.

    Open Resource.mr-IN.resx

5. Open "Resource.ar-SA.resx"

  • Add a name under the "Resource.ar-SA.resx" Filename and the Value that you want to display in the value field.

    Open Resource.ar-SA.resx 
  • Add a new page and name it "LocalPage.aspx"
  • Add a DropDownList in the "LocalPage.aspx"

On LocalPage.aspx

  1. <asp:DropDownList ID="DrpLanguages" AutoPostBack="true"  
  2.  runat="server" Width="200px"  
  3. OnSelectedIndexChanged="DrpLanguages_SelectedIndexChanged">  
  4. <asp:ListItem Text="Select Languages" Value="0"></asp:ListItem>  
  5. <asp:ListItem Text="English" Value="en-GB"></asp:ListItem>  
  6. <asp:ListItem Text="Hindi" Value="hi-IN"></asp:ListItem>  
  7. <asp:ListItem Text="arabic" Value="ar-SA"></asp:ListItem>  
  8. </asp:DropDownList> 

LocalPage.aspx.cs

  1. protected void DrpLanguages_SelectedIndexChanged(object sender,EventArgs e)  
  2. {  
  3.     Session["Culture"] = DrpLanguages.SelectedValue;  
  4.     Response.Redirect("Runpage.aspx");  
  5. } 
  • Add another page name ("Runpage.aspx")

  • Add a Label to this "Runpage.aspx"

Add two Labels

For accessing the resource file you need to add syntax like this:

  1. Text="<%$Resources:Resource,AccCode%>"  
  2. "<%$Resources:Your Main Resource file Name, Add Name Of Resource field which you want to display %>"  
  3. <asp:Label ID="lblresdisplay" Font-Size="Large" runat="server"                   Text="<%$Resources:Resource,AccCode%>"></asp:Label>  
  4. <asp:Label ID="Label1" Font-Size="Large" runat="server" Text="<%$Resources:Resource,AccCode%>"></asp:Label> 

To accomplish this, what we need to do is to override the InitializeCulture function and set the UICulture to the user selected language.

Runpage.aspx.cs

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.Globalization;  
  14. using System.Threading;  
  15. public partial class Runpage : System.Web.UI.Page  
  16. {  
  17.     protected void Page_Load(object sender, EventArgs e)  
  18.     {  
  19.     }  
  20.     protected override void InitializeCulture()  
  21.     {  
  22.         base.InitializeCulture();  
  23.         System.Threading.Thread.CurrentThread.CurrentCulture = new   System.Globalization.CultureInfo(Session["Culture"].ToString());  
  24.         System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Session["Culture"].ToString());  
  25.     }  
  26. }

Final Output

Output Localization and Globalization1

Output1 Localization and Globalization1
 


Similar Articles