Multi language Website using ASP.NET
Their are 3 Methods for developing multi-language websites:
1. Replication
2. Generalized approach
3. Using Concept of Localization and Globalization
Replization: This is basic approach used in static websites. In this approach you need to
create replica of each web page in different languages. Suppose you have 100 web pages in
your web site and you want to provide same content in 3 languages then you have to create
300 pages for it.
This is totally incorrect approach.
Generalized approach: This approach is supported in ASP.NET version 1.1, 2.0, 3.5 and
4.0. Means this approach is supported by all version of ASP.NET.
Localization and Globalization: This approach was introduced in ASP.NET version 3.5 and
4.0
Both of these approaches has their on advantages. Buth their is no disadvantage.
Now starting with Generalized approach:
First you need to have language name and their code:
Language Code
--------------------------------------------------------
English en-us
French fr-be
German de-at
To get more language code: Select Internet option in internet explorer > Select Languages
Step 1: Create new website.
Add Three text file and name them:
a. abc.en-us.txt
b. abc.fr-be.txt
c. abc.de-at.txt
In this naming convention.
abc: Base name
en : Culture code
us : Region Code
txt: File Extension
Step 2: Specify key and their value in these files:
a. abc.en-us.txt
a=This is Techsapphire
b=English
b. abc.fr-be.txt
a=c'est www.techsapphire.in
b=French
c. abc.de-at.txt
a=Dies ist www.techsapphire.in
b=German
Step 3: Convert these files into resource files (File with extension *.resources).
a. Open Visual Studio Command prompt.
b. Move to web directory using command prompt.
c. Use "resgen" utility to convert these files to resouces.
Example:
>resgen abc.en-us.txt
>resgen abc.fr-be.txt
>resgen abc.de-at.txt
These command will create 3 files:
a. abc.en-us.resources
b. abc.fr-be.resources
c. abc.de-at.resources
Step 4: Add Global Application Class to you web project:
Global.asax
Add name space to this file:
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Resources" %>
<%@ Import Namespace="System.Threading" %>
Code:
void Application_Start(object sender, EventArgs e)
{
Application["aa"] = ResourceManager.CreateFileBasedResourceManager("abc",
Server.MapPath("."), null);
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
//This code is to set deault language either to culture according to browser
language. Or to open website in default language if the culture of browser doesn't match
to language support provided
try
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(Request.UserLanguages
[0].ToString());
}
catch
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
}
Thread.CurrentThread.CurrentUICulture =
Thread.CurrentThread.CurrentCulture;
}
Step5: Code Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="en-us">English</asp:ListItem>
<asp:ListItem Value="fr-be">French</asp:ListItem>
<asp:ListItem Value="de-at">German</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Label ID="lb1" runat="server">
</asp:Label><br />
<asp:Label ID="lb2" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System.Globalization;
using System.Resources;
using System.Threading;
public partial class _Default : System.Web.UI.Page
{
ResourceManager rm;
protected void Page_Load(object sender, EventArgs e)
{
rm = (ResourceManager)(Application["aa"]);
if (!IsPostBack)
{
lb1.Text = rm.GetString("a").ToString();
lb2.Text = rm.GetString("b").ToString();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
setlang(DropDownList1.SelectedValue);
}
private void setlang(string d)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(d);
lb1.Text = rm.GetString("a").ToString();
lb2.Text = rm.GetString("b").ToString();
}
}
Step 5: Test your website by changing your browser language.
(Note: In browser the language specified at top of the list is default language.)
Concept of Localization and Globalization:
In this localized resources are page specific and global resources are project specific.
In this approach you don't need to create txt files and then converting them to resource
file. You could directly create resouce file. You could also specify key attributes.
Step 1: Crete New Website.
Add New ASP.NET Folder:
a. App_LocalResources
b. App_GlobalResources
Step 2: Add 3 Resource file to App_LocalResources (As local resources are page specific
so base name here would be page base name):
a. Default.aspx.resx > This one will also be used as default resource file
b. Default.aspx.fr-be.resx
c. Default.aspx.de-at.resx
Add Key and their properties in resource file:
Default.aspx.resx
String Value Comment
--------------------------------------------------
ab.text This is English
ab.tooltip This is English
other Other English
Default.aspx.fr-be.resx
String Value Comment
--------------------------------------------------
ab.text This is French
ab.tooltip This is French
other Other French
Default.aspx.de-at.resx
String Value Comment
--------------------------------------------------
ab.text This is German
ab.tooltip This is German
other Other German
Add Resource file with any name to App_GlobalResources
Anyname.resx
String Value Comment
--------------------------------------------------
Glob Other Global
Step 3: Add Default.aspx
(Note: Add Culture="Auto" Uiculture="Auto" to File declaration. As Specified in
this code)
<%@ Page Culture="Auto" UICulture="Auto" Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lb1" runat="server" meta:Resourcekey="ab">
</asp:Label><br />
<asp:Label ID="lb2" runat="server" Text='<%$ Resources:other %>'>
</asp:Label><br />
<asp:Label ID="lb3" runat="server" Text='<%$ Resources:Anyname,glob %>'></asp:Label>
<br />
<br />
<asp:Label ID="lb4" runat="server">
</asp:Label><br />
<asp:Label ID="lb5" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lb4.Text = GetGlobalResourceObject("anyname", "glob").ToString();
lb5.Text = GetLocalResourceObject("other").ToString();
}
}
Download Project File