Globalisation in ASP.Net

Currently, 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 their own regional language. For implementing multilingual functionality in your website, you need to use globalization in your website.



So let’s start the implementation of globalization in our website.

  1. Open Visual Studio
  2. "File" >> "New" >> "WebSite".
  3. Select "Empty WebSite", Click "Ok".
  4. Right-click on the solution in the Solution Explorer then select "Add" >> "Add ASP.NET Folder" >> "App_GlobalResources".
  5. Select App_GlobalResource >> Add >> Resource File

Resource Files

A resource file is an XML file that contains the strings 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 value Submit.

In this example, I will create the following 3 resource files:

  1. For English: Resource.resx
  2. For Hindi: Resource.hi.resx
  3. 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 Spenish):


Add a new MasterPage, right-click on the solution in the Solution Explorer then select "Add" >> "MasterPage".

MasterPage.master

  1. <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs"   
  2. Inherits="MasterPage" %>  
  3. <!DOCTYPE html>  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title></title>  
  7.     <asp:ContentPlaceHolder ID="head" runat="server">  
  8.     </asp:ContentPlaceHolder>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12.         <div>  
  13.             <table>  
  14.                 <tr>  
  15.                     <td>  
  16.                         <asp:Label ID="lblChageLang" runat="server" Text="Change Languages">  
  17. </asp:Label>  
  18.                     </td>  
  19.                     <td>  
  20.                         <asp:DropDownList ID="ddlLanguage" runat="server"   
  21. OnSelectedIndexChanged="ddlLanguage_SelectedIndexChanged" AutoPostBack="True">  
  22.                             <asp:ListItem Text="English" Value="en-Us"></asp:ListItem>  
  23.                             <asp:ListItem Text="Hindi" Value="hi-IN"></asp:ListItem>  
  24.                             <asp:ListItem Text="Spenish" Value="es-MX"></asp:ListItem>  
  25.                         </asp:DropDownList>  
  26.                     </td>  
  27.                 </tr>  
  28.             <br/>  
  29.             </table>  
  30.             <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">  
  31.             </asp:ContentPlaceHolder>  
  32.         </div>  
  33.     </form>  
  34. </body>  
  35. </html> 

MasterPage.master.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Globalization;  
  4. using System.Linq;  
  5. using System.Threading;  
  6. using System.Web;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. public partial class MasterPage : System.Web.UI.MasterPage  
  10. {  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.         if (!IsPostBack)  
  14.         {  
  15.             if (Session["SelectedLang"] != null)  
  16.             {  
  17.                 ddlLanguage.SelectedValue = Session["SelectedLang"].ToString();  
  18.                 Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture  
  19. (ddlLanguage.SelectedValue);  
  20.                 Thread.CurrentThread.CurrentUICulture = new CultureInfo  
  21. (ddlLanguage.SelectedValue);  
  22.             }  
  23.             else  
  24.             {  
  25.                 Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture  
  26. (ddlLanguage.SelectedValue);  
  27.                 Thread.CurrentThread.CurrentUICulture = new CultureInfo  
  28. (ddlLanguage.SelectedValue);  
  29.             }  
  30.         }  
  31.     }  
  32.     protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)  
  33.     {  
  34.         if (ddlLanguage.SelectedValue == "hi-IN")  
  35.         {  
  36.             Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("hi-IN");  
  37.             Thread.CurrentThread.CurrentUICulture = new CultureInfo("hi-IN");  
  38.             Session["SelectedLang"] = ddlLanguage.SelectedValue;  
  39.             Server.Transfer(Request.Path);  
  40.         }  
  41.         else if (ddlLanguage.SelectedValue == "es-MX")  
  42.         {  
  43.             Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("es-MX");  
  44.             Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-MX");  
  45.             Session["SelectedLang"] = ddlLanguage.SelectedValue;  
  46.             Server.Transfer(Request.Path);  
  47.         }  
  48.         else  
  49.         {  
  50.             Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");  
  51.             Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");  
  52.             Session["SelectedLang"] = ddlLanguage.SelectedValue;  
  53.             Server.Transfer(Request.Path);  
  54.         }  
  55.     }  
  56. } 

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

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"   
  2. CodeFile="Default2.aspx.cs" Inherits="Default2" %>  
  3. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">  
  4. </asp:Content>  
  5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
  6.     <table>  
  7.         <tr>  
  8.             <td>  
  9.                 <asp:Label ID="lblName" runat="server" Text="<% $Resources:Resource,Name %>"></asp:Label>  
  10.             </td>  
  11.             <td>  
  12.                 <asp:TextBox ID="txtName" runat="server"></asp:TextBox>  
  13.             </td>  
  14.         </tr>  
  15.         <tr>  
  16.             <td>  
  17.                 <asp:Label ID="lblFname" runat="server" Text="<% $Resources:Resource,FName %>"></asp:Label>  
  18.             </td>  
  19.             <td>  
  20.                 <asp:TextBox ID="txtFname" runat="server"></asp:TextBox>  
  21.             </td>  
  22.         </tr>  
  23.         <tr>  
  24.             <td>  
  25.                 <asp:Label ID="lblMname" runat="server" Text="<% $Resources:Resource,MomName %>"></asp:Label>  
  26.             </td>  
  27.             <td>  
  28.                 <asp:TextBox ID="txtMName" runat="server"></asp:TextBox>  
  29.             </td>  
  30.         </tr>  
  31.         <tr>  
  32.             <td>  
  33.                 <asp:Label ID="lblAddress" runat="server" Text="<% $Resources:Resource,Address %>"></asp:Label>  
  34.             </td>  
  35.             <td>  
  36.                 <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>  
  37.             </td>  
  38.         </tr>  
  39.         <tr>  
  40.             <td>  
  41.                 <asp:Label ID="lblPhone" runat="server" Text="<% $Resources:Resource,Phone %>"></asp:Label>  
  42.             </td>  
  43.             <td>  
  44.                 <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>  
  45.             </td>  
  46.         </tr>  
  47.     </table>  
  48. </asp:Content> 

In the preceding page, look at the label Text . Text=”<% $Resources.Resource,Address %>”

  1. <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.



Similar Articles