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:
- Right-click on the Root Folder Add New Item.
- Select Resource File from the above Templates.
- Name it "Resource.resx"
- You need other Resource Files for storing data depending on the Culture
- 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"
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
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.
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.
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.
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.
- Add a new page and name it "LocalPage.aspx"
- Add a DropDownList in the "LocalPage.aspx"
On LocalPage.aspx
<asp:DropDownList ID="DrpLanguages" AutoPostBack="true"
runat="server" Width="200px"
OnSelectedIndexChanged="DrpLanguages_SelectedIndexChanged">
<asp:ListItem Text="Select Languages" Value="0"></asp:ListItem>
<asp:ListItem Text="English" Value="en-GB"></asp:ListItem>
<asp:ListItem Text="Hindi" Value="hi-IN"></asp:ListItem>
<asp:ListItem Text="arabic" Value="ar-SA"></asp:ListItem>
</asp:DropDownList>
LocalPage.aspx.cs
protected void DrpLanguages_SelectedIndexChanged(object sender,EventArgs e)
{
Session["Culture"] = DrpLanguages.SelectedValue;
Response.Redirect("Runpage.aspx");
}
Add two Labels
For accessing the resource file you need to add syntax like this:
Text="<%$Resources:Resource,AccCode%>"
"<%$Resources:Your Main Resource file Name, Add Name Of Resource field which you want to display %>"
<asp:Label ID="lblresdisplay" Font-Size="Large" runat="server" Text="<%$Resources:Resource,AccCode%>"></asp:Label>
<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
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using System.Threading;
public partial class Runpage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void InitializeCulture()
{
base.InitializeCulture();
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(Session["Culture"].ToString());
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Session["Culture"].ToString());
}
}
Final Output