We can create the Multilanguage application in asp.net like this
Step 1: Add the Master Page Site1.Master and Design the Menu and language Selection dropdown list like this
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="MultilanguageSample.Site1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> html { font-family: Tahoma; font-size: 14px; font-style: normal; background-color: Silver; } .Content { margin: auto; width: 700px; background-color: white; border: Solid 1px black; } .auto-style1 { width: 100%; } </style> <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder> </head> <body> <form id="form1" runat="server"> <div class="Content"> <br /> <table class="auto-style1"> <tr> <td style="background-color: #339966"> <a href="Default.aspx"> <asp:Label ID="Label1" meta:resourcekey="menuItemDefault" runat="server" Text="Home"></asp:Label> </a></td> <td style="background-color: #339966; " ><a href="Contact.aspx"> <asp:Label ID="Label2" meta:resourcekey="menuItemContact" runat="server" Text="ContactUs"></asp:Label> </a></td> <td style="background-color: #339966;"><asp:DropDownList ID="DropDownList_Language" runat="server" Height="20px" Width="170px" OnSelectedIndexChanged="DropDownList_Language_SelectedIndexChanged" AutoPostBack="true"> <asp:ListItem Value="en-US">English</asp:ListItem> <asp:ListItem Value="sv-SE">Swedish</asp:ListItem> </asp:DropDownList></td> </tr> </table> <br /> <br /> <div> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </div> </form> </body> </html>
Step 2: Write the code in code behind file like this
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace MultilanguageSample { public partial class Site1 : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { if (Session["language"] != null && !IsPostBack) { DropDownList_Language.ClearSelection(); DropDownList_Language.Items.FindByValue(Session["language"].ToString()).Selected = true; } } protected void DropDownList_Language_SelectedIndexChanged(object sender, EventArgs e) { switch (DropDownList_Language.SelectedValue) { case "en-US": this.SetMyNewCulture("en-US"); break; case "sv-SE": this.SetMyNewCulture("sv-SE"); break; default: break; } Response.Redirect(Request.Path); } private void SetMyNewCulture(string culture) { Session["language"] = culture; } } }
Note: Here you can also store the user language selection in cookies or Local Storage of Html 5 using JavaScript. This can be used to maintain the user preference language at page Load.
Step 3 : Create a “App_LocalResources” folder and add the resources as page wise Like Site.master .resx for English and Site1.Master.sv. resx for Swedish
Note: Keep the separate resource file for each page for better maintainability.
Step 4: Add this method in global.asax file
void Application_AcquireRequestState(object sender, EventArgs e) { HttpContext context = HttpContext.Current; if (context.Session["language"] != null) { Thread.CurrentThread.CurrentUICulture = new CultureInfo(context.Session["language"].ToString().Trim()); Thread.CurrentThread.CurrentCulture = new CultureInfo(context.Session["language"].ToString().Trim()); } }
This is used for getting the current state of session.
Step 5: Call the resource key on label like this as a page wise.
You call also call the resource file in C# like this
protected void Page_Load(object sender, EventArgs e) { lblMsg.Text = GetLocalResourceObject("lblMsgHome.Text").ToString(); }