Introduction
Now a days, when surfing websites, is there one common thing that you notice? Many websites are bilingual or multilingual. The reason behind this is that when we upload a website to the internet world, all over the world people can surf our website content. And people would appreciate if content is in their own regional language. For implementing multilingual functionality in your website, you need to use globalization in your website.
So let’s start implementation of globalization in our website.
- Open Visual Studio
- "File" >> "New">> "WebSite".
- Select "Empty WebSite", Click "Ok".
- Right-click on the solution in the Solution Explorer then select "Add" >> "Add ASP.NET Folder" >> "App_GlobalResources".
- Select App_GlobalResource >> Add >> Resource File
Resource Files
A resource file is an XML file that contains the
string
s that you want to translate into various languages or paths to images. The resource file contains key/value pairs. Each pair is an individual resource. Key names are not case sensitive. For example, a resource file might contain a resource with the key Button1
and the valueSubmit
.In this example, I will create the following 3 resource files:
- For English: Resource.resx
- For Hindi: Resource.hi.resx
- For Spanish: Resource.es.resx
So first, create 3 resource files, and please make all keys names the same and change the values.
1. Resource.resx (for English)
2. Resource.hi.resx (for Hindi)
3. Resource.es.resx (for Spanish)
Add a new MasterPage, right-click on the solution in the Solution Explorer then select "Add" >> "MasterPage".
MasterPage.master
Collapse | Copy Code
<%@ Master Language="C#"
AutoEventWireup="true" CodeFile="MasterPage.master.cs"
Inherits="MasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tbody><tr>
<td>
<asp:Label ID="lblChageLang"
runat="server" Text="Change Languages">
</td>
<td>
<asp:DropDownList ID="ddlLanguage" runat="server"
OnSelectedIndexChanged="ddlLanguage_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Text="English" Value="en-Us">
<asp:ListItem Text="Hindi" Value="hi-IN">
<asp:ListItem Text="Spanish" Value="es-MX">
</td>
</tr>
</tbody></table>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</div>
</form>
</body>
</html>
MasterPage.master.cs
Collapse | Copy Code
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["SelectedLang"] != null)
{
ddlLanguage.SelectedValue = Session["SelectedLang"].ToString();
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(ddlLanguage.SelectedValue);
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(ddlLanguage.SelectedValue);
}
else
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(ddlLanguage.SelectedValue);
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(ddlLanguage.SelectedValue);
}
}
}
protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlLanguage.SelectedValue == "hi-IN")
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("hi-IN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("hi-IN");
Session["SelectedLang"] = ddlLanguage.SelectedValue;
Server.Transfer(Request.Path);
}
else if (ddlLanguage.SelectedValue == "es-MX")
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("es-MX");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-MX");
Session["SelectedLang"] = ddlLanguage.SelectedValue;
Server.Transfer(Request.Path);
}
else
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Session["SelectedLang"] = ddlLanguage.SelectedValue;
Server.Transfer(Request.Path);
}
}
}
In
MasterPage
, you can see there is a dropdownlist , where there are 3 list items for the English, Hindi and Spanish languages. I put a dropdownlist in the masterpage so we can see this dropdown in all pages.Default2.aspx
Collapse | Copy Code
<%@ Page Title="" Language="C#"
MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<asp:Content ID="Content1"
ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2"
ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table>
>tbody>>tr>
>td>
<asp:Label ID="lblName"
runat="server" Text="<% $Resources:Resource,Name %>">
>/td>
>td>
<asp:TextBox ID="txtName"
runat="server">
>/td>
>/tr>
>tr>
>td>
<asp:Label ID="lblFname"
runat="server" Text="<% $Resources:Resource,FName %>">
>/td>
>td>
<asp:TextBox ID="txtFname"
runat="server">
>/td>
>/tr>
>tr>
>td>
<asp:Label ID="lblMname"
runat="server" Text="<% $Resources:Resource,MomName %>">
>/td>
>td>
<asp:TextBox ID="txtMName"
runat="server">
>/td>
>/tr>
>tr>
>td>
<asp:Label ID="lblAddress"
runat="server" Text="<% $Resources:Resource,Address %>">
>/td>
>td>
<asp:TextBox ID="txtAddress"
runat="server">
>/td>
>/tr>
>tr>
>td>
<asp:Label ID="lblPhone"
runat="server" Text="<% $Resources:Resource,Phone %>">
>/td>
>td>
<asp:TextBox ID="txtPhone"
runat="server">
>/td>
>/tr>
>/tbody>>/table>
In the preceding page, look at the label
Text
. Text=”<% $Resources.Resource,Address %>”
Collapse | Copy Code
<asp:Label ID="lblAddress" runat="server"
Text="<% $Resources:Resource,Address %>"></asp:Label>
How It Works
As you already know, a resource file is a collection of key and value pairs. In each resource file, we define a culture. For example, for the Hindi resource file, I use the name Resource.hi.resx. When you define a key in a resource file, all keys would be the same except the value will change based on the language. Like in all 3 resource files above, the address key is common but the value is different in all the files. When you create a label text file
<%Resources:Resource,Address%>
, here the address is the key, based on the selected dropdown value, you see the changes.Press F5, and run your code.
Source collected from:
http://www.codeproject.com/Tips/832297/Globalisation-in-ASP-NET
No comments:
Post a Comment