Sometimes we need to design our website or web application in such a way that different people of different language and culture can read it. We can do it to design our website or web application in each language. But this is time consuming, costly, and difficult to manage. Globalization and Localization of ASP.NET can help us to solve this problem. By using this we can easily design our website or web application for multiple language and culture.
The topics in this section describe how to create ASP.NET website or web application that can be adapted to different languages and cultures in step by step. Summary of the articles:
- Globalization and Localization
- Resource Files
- Creation of Multiple Language Project in Asp.NET
Globalization and Localization
Globalization is the process of designing and developing applications that function for multiple cultures. Localization is the process of customizing application for a given culture and locale.
Resource Files
If we want to fulfill our target in traditional way then we need to write a lot of switch cases or if statements which is time consuming. To avoid this ASP.NET provides us a Resource file which is an XML file with .resx extension. Resource files can either be a global resource, which is accessible throughout the site and placed in App_GlobalResources folder in the application or a local resource which is specific to a page only and is placed in App_LocalResources folder.
We are going to display Good Morning in five different languages (English, Spanish, German, Chinese, and Bengali). We will show these five languages in a DropDownList. If we select any language then Good Morning will be translated into selected language.
Creation of Multiple Language Project in Asp.NET
Step: 1
Create a New Project.
Step: 2
Add a DropDownList in Master Page. Add some language name and its code on that DropDownList. You can use the following ASP code.
<asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="True" onselectedindexchanged="ddlLanguage_SelectedIndexChanged"> <asp:ListItem>-Select-</asp:ListItem> <asp:ListItem Value="en-US">English</asp:ListItem> <asp:ListItem Value="bn-BD">Bangla</asp:ListItem> <asp:ListItem Value="zh-CN">Chinese</asp:ListItem> <asp:ListItem Value="de-DE">German</asp:ListItem> <asp:ListItem Value="es-ES">Spenish</asp:ListItem> </asp:DropDownList>
Step: 3
Write the following C# code under the SelectedIndexChanged event of DropDownList in .cs page of Master Page.
protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e) { string Language = ddlLanguage.SelectedValue; if (Language != "none") { ChangeLanguage(Language); } Response.Redirect(Request.UrlReferrer.AbsoluteUri); } protected void ChangeLanguage(string LanguageCode) { HttpCookie LangCookie = new HttpCookie("Language"); LangCookie.Value = LanguageCode; LangCookie.Expires = DateTime.MaxValue; Response.Cookies.Add(LangCookie); }
Step: 4
Write the following code in Global.asax page. Generally Global.asax is created during project creation time. If not, then add a Global.asax in your project.It may required to include the following namespaces.
using System.Globalization; using System.Threading; protected void Application_BeginRequest(object sender, EventArgs e) { string Lang; HttpCookie LangCookie = Request.Cookies["Language"]; if (LangCookie != null && LangCookie.Value != string.Empty) { Lang = LangCookie.Value; Thread.CurrentThread.CurrentUICulture = new CultureInfo(Lang); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Lang); } }
Step: 5
Add a Label in your Default.aspx [you may use any page name] page and change its Caption or Text as “Good Morning”.
Step: 6
Open Default.aspx page and click Generate Local Resource from Tools menu. A Default.aspx.resx page will be created in App_LocalResources folder. If you open Default.aspx page you will find that some extra code is generated in your page. Code for Label is like that.
<asp:Label ID="Label1" runat="server" Text="Good Morning" meta:resourcekey="Label1Resource1"> </asp:Label>
If the code culture=”auto” uiculture=”auto” is generated in the beginning of page please delete them. Save the page.
Step: 7
Open the Resource file Default.aspx.resx page. This page is for English. For each language you need to create separate Resource file. Now copy paste the Default.aspx.resx page and rename it as like.
Default.aspx.bn-BD.resx (for Bengali)
Default.aspx.de-DE.resx (For German)
Default.aspx.es-ES.resx (for Spanish)
Default.aspx.zh-CN.resx (for Chinese)
Step: 8
Open each page and change the value of Label1Resource1.Text as like
শুভ সকাল (for Bengali)
guten Morgen (For German)
¡buenos días (for Spanish)
早安 (for Chinese)
Step: 9
Run the application and select one language form the DropDownList. Good Morning will be changed according to your selected language.
In this way we can design our web application or website for multiple language and culture in ASP.NET and C#.
I’m extremely impressed with your writing skills as well as with
the layout on your weblog. Is this a paid theme or did you customize it yourself?
Either way keep up the nice quality writing, it’s rare to
see a great blog like this one these days.
Thank you very much.