Building 🏗️ Multilingual Web Application Using .NET - Part One

Introduction

 
In this article, we will learn  a step by step process to create a multilingual webform application using Asp.net. Multilingual means we can  implement the website in multiple languages.
 
In today's world to grow customer ineraction, everyone needs to implement this procedure in their applications.
 
Prerequisites
  • Visual Studio
Note
Before going through this session, please visit my previous articles related to Asp.net for better understanding.
 
Step 1
 
First we need to add a webpage and design for a multilingual form.
 
Code Ref
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="UserSignUp.aspx.cs" Inherits="multilingualAsp.UserSignUp" %>  
  2. <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">  
  3.     <style>      
  4.         .button {      
  5.             background-color: #4CAF50;      
  6.             border: none;      
  7.             color: white;      
  8.             padding: 15px 32px;      
  9.             text-align: center;      
  10.             text-decoration: none;      
  11.             display: inline-block;      
  12.             font-size: 16px;      
  13.             margin: 4px 2px;      
  14.             cursor: pointer;      
  15.         }       
  16.   
  17.         .button1 {      
  18.             background-color:red;      
  19.             border: none;      
  20.             color: white;      
  21.             padding: 15px 32px;      
  22.             text-align: center;      
  23.             text-decoration: none;      
  24.             display: inline-block;      
  25.             font-size: 16px;      
  26.             margin: 4px 2px;      
  27.             cursor: pointer;      
  28.         }   
  29.         .button4 {      
  30.             border-radius: 9px;      
  31.         }      
  32.         input[type=text], select {      
  33.         width: 100%;      
  34.         padding: 12px 20px;      
  35.         margin: 10px 0;      
  36.         display: inline-block;      
  37.         border: 1px solid #ccc;      
  38.         border-radius: 4px;      
  39.         box-sizing: border-box;      
  40.         font-family: 'Montserrat', sans-serif;        
  41.         text-indent: 10px;        
  42.         color: blue;        
  43.         text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);        
  44.         font-size: 20px;        
  45.     }      
  46.      .UnderLine{  
  47.          text-decoration:underline;  
  48.          color:red;  
  49.      }  
  50.        
  51.     </style>  
  52.   
  53.       
  54.     <div style="text-align:right;">   
  55.                 <asp:DropDownList ID="ddLang" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddLang_SelectedIndexChanged">  
  56.                     <asp:ListItem Value="en-US" Text="English" />  
  57.                     <asp:ListItem Value="fr-FR" Text="français" />  
  58.                     <asp:ListItem Value="hi-IN" Text="हिंदी" />  
  59.                     <asp:ListItem Value="or-IN" Text="ଓଡିଆ" />  
  60.                 </asp:DropDownList>  
  61.             </div>  
  62.   
  63.     <div style="font-family: Arial Black;background-color:yellow; color:red; font-size:x-large;font-style:normal; text-align:center;">   
  64.         <asp:Label ID="lblMyName" runat="server" />  
  65.         </div>  
  66.     <div style="float:left">  
  67.             <table>  
  68.                 <tr>  
  69.                     <td><asp:Label ID="lblName" runat="server" style="font-weight:bold"/></td>     
  70.                     <td><asp:TextBox ID="txtName" runat="server" /></td>  
  71.                 </tr>  
  72.                 <tr>  
  73.                     <td><asp:Label ID="lblAddress" runat="server" style="font-weight:bold"/></td>     
  74.                     <td><asp:TextBox ID="txtAddress" runat="server" /></td>  
  75.                 </tr>  
  76.                 <tr>  
  77.                     <td><asp:Label ID="lblState" runat="server" style="font-weight:bold"/></td>      
  78.                     <td><asp:TextBox ID="txtState" runat="server" /></td>  
  79.                 </tr>  
  80.                 <tr>  
  81.                     <td><asp:Label ID="lblCountry" runat="server" style="font-weight:bold"/></td>     
  82.                     <td><asp:TextBox ID="txtCountry" runat="server" /></td>  
  83.                 </tr>  
  84.                 <tr>  
  85.                     <td></td>  
  86.                     <td>  
  87.                         <asp:Button ID="btnSave" runat="server" class="button button4" />  
  88.                         <asp:Button ID="btnCancel" runat="server" class="button1 button4"/>  
  89.                     </td>  
  90.                 </tr>  
  91.             </table>  
  92.         </div>  
  93.   
  94.      <div style="float:right">  
  95.             <h3>  
  96.                 <asp:Label ID="lblh1" runat="server" CssClass="UnderLine"/>  
  97.             </h3>  
  98.                 <h2>  
  99.                     <asp:Label ID="lblp1" runat="server" />  
  100.                 </h2>  
  101.             <hr>  
  102.   
  103.             <h3>  
  104.               <asp:Label ID="lblh2" runat="server" CssClass="UnderLine"/>  
  105.             </h3>  
  106.                <h2>  
  107.                    <asp:Label ID="lblp2" runat="server" />  
  108.                </h2>  
  109.          </div>  
  110. </asp:Content>  
Code Description
 
Here in the dropdown list we added a value that is very important for translating langulages. It must satisfy the ISO Language Code extension. For example: To make a Hindi translation we added value "hi-IN" code.
  1. <div style="text-align:right;">   
  2.                 <asp:DropDownList ID="ddLang" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddLang_SelectedIndexChanged">  
  3.                     <asp:ListItem Value="en-US" Text="English" />  
  4.                     <asp:ListItem Value="fr-FR" Text="français" />  
  5.                     <asp:ListItem Value="hi-IN" Text="हिंदी" />  
  6.                     <asp:ListItem Value="or-IN" Text="ଓଡିଆ" />  
  7.                 </asp:DropDownList>  
  8.             </div>  
LanguageISO Code 2ISO Code 3
Abkhazian ab abk
Afar aa aar
Afrikaans af afr
Albanian sq alb/sqi*
Amharic am amh
Arabic ar ara
Aragonese an arg
Armenian hy arm/hye*
Assamese as asm
Avestan ae ave
Aymara ay aym
Azerbaijani az aze
Bashkir ba bak
Basque eu baq/eus*
Belarusian be bel
Bengali bn ben
Bihari bh bih
Bislama bi bis
Bosnian bs bos
Breton br bre
Bulgarian bg bul
Burmese my bur/mya*
Catalan ca cat
Chamorro ch cha
Chechen ce che
Chinese zh chi/zho*
Church Slavic; Slavonic; Old Bulgarian cu chu
Chuvash cv chv
Cornish kw cor
Corsican co cos
Croatian hr scr/hrv*
Czech cs cze/ces*
Danish da dan
Divehi; Dhivehi; Maldivian dv div
Dutch nl dut/nld*
Dzongkha dz dzo
English en eng
Esperanto eo epo
Estonian et est
Faroese fo fao
Fijian fj fij
Finnish fi fin
French fr fre/fra*
Gaelic; Scottish Gaelic gd gla
Galician gl glg
Georgian ka geo/kat*
German de ger/deu*
Greek, Modern (1453-) el gre/ell*
Guarani gn grn
Gujarati gu guj
Haitian; Haitian Creole ht hat
Hausa ha hau
Hebrew he heb
Herero hz her
Hindi hi hin
Hiri Motu ho hmo
Hungarian hu hun
Icelandic is ice/isl*
Ido io ido
Indonesian id ind
Interlingua (International Auxiliary Language Association) ia ina
Interlingue ie ile
Inuktitut iu iku
Inupiaq ik ipk
Irish ga gle
Italian it ita
Japanese ja jpn
Javanese jv jav
Kalaallisut kl kal
Kannada kn kan
Kashmiri ks kas
Kazakh kk kaz
Khmer km khm
Kikuyu; Gikuyu ki kik
Kinyarwanda rw kin
Kirghiz ky kir
Komi kv kom
Korean ko kor
Kuanyama; Kwanyama kj kua
Kurdish ku kur
Lao lo lao
Latin la lat
Latvian lv lav
Limburgan; Limburger; Limburgish li lim
Lingala ln lin
Lithuanian lt lit
Luxembourgish; Letzeburgesch lb ltz
Macedonian mk mac/mkd*
Malagasy mg mlg
Malay ms may/msa*
Malayalam ml mal
Maltese mt mlt
Manx gv glv
Maori mi mao/mri*
Marathi mr mar
Marshallese mh mah
Moldavian mo mol
Mongolian mn mon
Nauru na nau
Navaho, Navajo nv nav
Ndebele, North nd nde
Ndebele, South nr nbl
Ndonga ng ndo
Nepali ne nep
Northern Sami se sme
Norwegian no nor
Norwegian Bokmal nb nob
Norwegian Nynorsk nn nno
Nyanja; Chichewa; Chewa ny nya
Occitan (post 1500); Provencal oc oci
Oriya or ori
Oromo om orm
Ossetian; Ossetic os oss
Pali pi pli
Panjabi pa pan
Persian fa per/fas*
Polish pl pol
Portuguese pt por
Pushto ps pus
Quechua qu que
Raeto-Romance rm roh
Romanian ro rum/ron*
Rundi rn run
Russian ru rus
Samoan sm smo
Sango sg sag
Sanskrit sa san
Sardinian sc srd
Serbian sr scc/srp*
Shona sn sna
Sichuan Yi ii iii
Sindhi sd snd
Sinhala; Sinhalese si sin
Slovak sk slo/slk*
Slovenian sl slv
Somali so som
Sotho, Southern st sot
Spanish; Castilian es spa
Sundanese su sun
Swahili sw swa
Swati ss ssw
Swedish sv swe
Tagalog tl tgl
Tahitian ty tah
Tajik tg tgk
Tamil ta tam
Tatar tt tat
Telugu te tel
Thai th tha
Tibetan bo tib/bod*
Tigrinya ti tir
Tonga (Tonga Islands) to ton
Tsonga ts tso
Tswana tn tsn
Turkish tr tur
Turkmen tk tuk
Twi tw twi
Uighur ug uig
Ukrainian uk ukr
Urdu ur urd
Uzbek uz uzb
Vietnamese vi vie
Volapuk vo vol
Walloon wa wln
Welsh cy wel/cym*
Western Frisian fy fry
Wolof wo wol
Xhosa xh xho
Yiddish yi yid
Yoruba yo yor
Zhuang; Chuang za zha
Zulu zu zul
 
Step 2
 
In this step, we add a ASP.NET Folder "App_GlobalResources" for saving language resource file.
 
Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > Add ASP.NET Folder > App_GlobalResources.
 
Step 3
 
In this step, we add Resources file for saving different languages under "App_GlobalResources" Folder.
 
Right Click on "App_GlobalResources" Folder form Solution Explorer > Add > New Item > Select "Resources File" under Visual C# > Enter Name (Like "Lang.or-IN.resx") > Add 
 
By using the same process we need to add 3 different resource files as mentioned below.
 
LanguageFile Name
English Lang.en-US.resx
French Lang.fr-FR.resx
Hindi Lang.hi-IN.resx
 
Then we need to configure resouce files as mentioned beow. For example: for Lang.or-IN.resx,
 
 
Here name means the string name passed in resource manager and value means the string name  against this resource manager, whatever value in any language that is translated into the  language as mentioned. Here in web pabe page Name is shown in English language but after selection of Oriya language the Name is translated into "ନାମ".
 
Then change Build Action to "Embedded Resource" of added resource files. To do so, Select Resource file > Right Click go to Property> Change Build Action to Embedded Resource.
 
Step 4
 
In this step, we need to add code in code behind file for translating languages based on selected languaged with ISO language code.
 
Code Ref
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Resources; //import following Namespace first  
  8. using System.Globalization; //import following Namespace first  
  9. using System.Threading; //import following Namespace first  
  10. using System.Reflection; //import following Namespace first  
  11.   
  12. namespace multilingualAsp  
  13. {  
  14.     public partial class UserSignUp : System.Web.UI.Page  
  15.     {  
  16.         ResourceManager rm;  
  17.         CultureInfo ci;  
  18.   
  19.         protected void Page_Load(object sender, EventArgs e)  
  20.         {  
  21.             if (Session["Lang"] == null)  
  22.             {  
  23.                 Session["Lang"] = Request.UserLanguages[0];  
  24.             }  
  25.   
  26.             if (!IsPostBack)  
  27.             {  
  28.                 LoadString(); //This function is used to translate languages based on selected languages  
  29.             }  
  30.         }  
  31.   
  32.         private void LoadString()  
  33.         {  
  34.             Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["Lang"].ToString());  
  35.             rm = new ResourceManager("multilingualAsp.App_GlobalResources.Lang", Assembly.GetExecutingAssembly()); //we configure resource manages for mapping with resource files in App_GlobalResources folder.  
  36.             ci = Thread.CurrentThread.CurrentCulture;  
  37.   
  38.             lblName.Text = rm.GetString("Name", ci); //Name is the resource manager string name that should mathch with Name solumn of resorce files and if that matches then the value against the name will be translated as mentioned for each languages.  
  39.             lblAddress.Text = rm.GetString("Address", ci); //same as//  
  40.             lblState.Text = rm.GetString("State", ci); //same as//  
  41.             lblCountry.Text = rm.GetString("Country", ci);//same as//  
  42.             btnSave.Text = rm.GetString("Save", ci);//same as//  
  43.             btnCancel.Text = rm.GetString("Cancel", ci);//same as//  
  44.             lblMyName.Text = rm.GetString("Application", ci);//same as//  
  45.   
  46.             lblh1.Text = rm.GetString("headerone", ci);//same as//  
  47.             lblh2.Text = rm.GetString("headertwo", ci);//same as//  
  48.             lblp1.Text = rm.GetString("paraone", ci);//same as//  
  49.             lblp2.Text = rm.GetString("paratwo", ci);//same as//  
  50.         }  
  51.   
  52.         protected void ddLang_SelectedIndexChanged(object sender, EventArgs e) //this event for showing selected language.  
  53.         {  
  54.                 Session["Lang"] = ddLang.SelectedValue;  
  55.                 LoadString();  
  56.             }  
  57.     }  
  58. }  
Code Description
 
Here I added code with description in green comment mark "//" at one place for better and faster understanding.
 
OUTPUT
 
The language selection is shown as below:
 
 
The content is translated into English.
 
 
The content is translated into French.
 
 
The content is translated into Hindi. 
 
 
The content is translated into Odia (The Mother language of the Odisha state, India).  
 
 
Link To Source Code,

Summary

 
In this article, we have learned,
  • Introduction to multilingual Asp.net Web Application
  • Configure resource files in Asp.net Global resource folder
  • Way to set-up ISO Language code for translating content
  • Setup resource manager in code behind(C#) based on resource files