Sunday, November 16, 2014

Multilingual Website in Asp.Net

Introduction:-This Article talks about creating Multilingual Website in Asp.net

Step 1:- Create a New ASP.NET Empty Web Application.

Step 2:- Add a new Webform and add below sample code for user interface inside form tag.

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
           
        <div>
            <center>
                 <table>
            <tr>
                <td colspan="2">
                    <h2>Multilingual Demo</h2>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="right">
                    <asp:DropDownList ID="ddlLang" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlLang_SelectedIndexChanged">
                        <asp:ListItem Text="English" Value="en-US"></asp:ListItem>
                        <asp:ListItem Text="français" Value="fr-FR"></asp:ListItem>
                        <asp:ListItem Text="español" Value="es-ES"></asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblName" runat="server" Text="Name" meta:resourcekey="lblNameResource1"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtName" runat="server" meta:resourcekey="txtNameResource1"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblEmail" runat="server" Text="Email" meta:resourcekey="lblEmailResource1"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtEmail" runat="server" meta:resourcekey="txtEmailResource1"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblPhone" runat="server" Text="Phone" meta:resourcekey="lblPhoneResource1"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtPhone" runat="server" meta:resourcekey="txtPhoneResource1"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <br />
                </td>
            </tr>
            <tr>
                <td>
                   
                </td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
                    <asp:Button ID="btnReset" runat="server" Text="Reset" />
                </td>
            </tr>
        </table>
            </center>
        </div>

        </ContentTemplate>
    </asp:UpdatePanel>

Step 3:- Now Add ASP.NET folder App_GlobalResources in order to save language resource file (.resx).
Right click  on project name in solution exploreràAddàAdd ASP.NET FolderàApp_GlobalResources














Step 4:- Add Resource file for different language inside App_GlobalResources folder.
Right Click App_GlobalResourcesàAddàNew ItemàResources File (.resx)
Note:-While naming file makes sure you follow below name.












List of different culture specification along with their name is available here

Step 5:- Now change Resource file Build Action to Embedded Resource.
Right Click Resource fileàpropertyàBuild Action=Embedded Resources.
















Step 6:- Write below code in the code-behind file of the created Webform.
Firstly import few required namespaces

using System;
using System.Resources;
using System.Globalization;
using System.Threading;
using System.Reflection;

ResourceManager resourceManager; //use to access cultural specific resources at runtime.
        CultureInfo cultureInfo; //provide information related to specific culture.


        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["Lang"]==null)
            {
                Session["Lang"] = Request.UserLanguages[0];//represent array of client language preferences.
            }
            if(!IsPostBack)
            {
                loadCulture();
            }
        }

        private void loadCulture()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["Lang"].ToString()); //fetch language preference.
            resourceManager = new ResourceManager("MultilingualDemo.App_GlobalResources.Lang", Assembly.GetExecutingAssembly());
            cultureInfo = Thread.CurrentThread.CurrentCulture; //fetch language details.

          
            lblName.Text = resourceManager.GetString("Name", cultureInfo);
            lblEmail.Text = resourceManager.GetString("Email", cultureInfo);
            lblPhone.Text = resourceManager.GetString("Phone", cultureInfo);
            btnSubmit.Text = resourceManager.GetString("Submit", cultureInfo);
            btnReset.Text = resourceManager.GetString("Reset", cultureInfo);
        }

        protected void ddlLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["Lang"] = ddlLang.SelectedValue;
            loadCulture();
        }

Step 7:- Preview of the Work


















Link to Github code

Hope you enjoyed it!!