Look at the following image:

this is your home page

change language

We can create the Multilanguage application in ASP.NET like the following.

Step 1

Add the Master Page Site1.Master and design the menu and language selection dropdown list like this:

  1. <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="MultilanguageSample.Site1" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <style type="text/css">
  7. html {
  8. font-family: Tahoma;
  9. font-size: 14px;
  10. font-style: normal;
  11. background-color: Silver;
  12. }
  13. .Content {
  14. margin: auto;
  15. width: 700px;
  16. background-color: white;
  17. border: Solid 1px black;
  18. }
  19. .auto-style1 {
  20. width: 100%;
  21. }
  22. </style>
  23. <asp:ContentPlaceHolder ID="head" runat="server">
  24. </asp:ContentPlaceHolder>
  25. </head>
  26. <body>
  27. <form id="form1" runat="server">
  28. <div class="Content">
  29. <br />
  30. <table class="auto-style1">
  31. <tr>
  32. <td style="background-color: #339966"> <a href="Default.aspx">
  33. <asp:Label ID="Label1" meta:resourcekey="menuItemDefault" runat="server" Text="Home"></asp:Label>
  34. </a></td>
  35. <td style="background-color: #339966; " ><a href="Contact.aspx">
  36. <asp:Label ID="Label2" meta:resourcekey="menuItemContact" runat="server" Text="ContactUs"></asp:Label>
  37. </a></td>
  38. <td style="background-color: #339966;"><asp:DropDownList ID="DropDownList_Language" runat="server" Height="20px" Width="170px"
  39. OnSelectedIndexChanged="DropDownList_Language_SelectedIndexChanged" AutoPostBack="true">
  40. <asp:ListItem Value="en-US">English</asp:ListItem>
  41. <asp:ListItem Value="sv-SE">Swedish</asp:ListItem>
  42. </asp:DropDownList></td>
  43. </tr>
  44. </table>
  45. <br />
  46. <br />
  47. <div>
  48. <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
  49. </asp:ContentPlaceHolder>
  50. </div>
  51. </div>
  52. </form>
  53. </body>
  54. </html>
Step 2

Write the code in the code behind file like this:
  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. namespace MultilanguageSample
  8. {
  9. public partial class Site1 : System.Web.UI.MasterPage
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (Session["language"] != null && !IsPostBack)
  14. {
  15. DropDownList_Language.ClearSelection();
  16. DropDownList_Language.Items.FindByValue(Session["language"].ToString()).Selected = true;
  17. }
  18. }
  19. protected void DropDownList_Language_SelectedIndexChanged(object sender, EventArgs e)
  20. {
  21. switch (DropDownList_Language.SelectedValue)
  22. {
  23. case "en-US": this.SetMyNewCulture("en-US");
  24. break;
  25. case "sv-SE": this.SetMyNewCulture("sv-SE");
  26. break;
  27. default:
  28. break;
  29. }
  30. Response.Redirect(Request.Path);
  31. }
  32. private void SetMyNewCulture(string culture)
  33. {
  34. Session["language"] = culture;
  35. }
  36. }
  37. }
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.

master page

Step 4

Add this method in the global.asax file:
  1. void Application_AcquireRequestState(object sender, EventArgs e)
  2. {
  3. HttpContext context = HttpContext.Current;
  4. if (context.Session["language"] != null)
  5. {
  6. Thread.CurrentThread.CurrentUICulture = new CultureInfo(context.Session["language"].ToString().Trim());
  7. Thread.CurrentThread.CurrentCulture = new CultureInfo(context.Session["language"].ToString().Trim());
  8. }
  9. }
This is used for getting the current state of the session.

Step 5

Call the resource key on the label like this as page wise.
  1. <asp:Label ID="Label1" meta:resourcekey="menuItemDefault" runat="server" Text="Home"></asp:Label>
You call also call the resource file in C# like this:
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. lblMsg.Text = GetLocalResourceObject("lblMsgHome.Text").ToString();
  4. }
Note: Please let me know if you have any good approach to do this.