Changing UICulture of master and content pages on button click in ASP.NET using C#, VB.NET

Download Source Code

Introduction
In this article I will explain how to change UICulture of master and content pages on button click in ASP.NET. This will enable users to select a language to view contents of a page/site.

When user selects a language, UICulture of the page is set to that specific language and page contents are displayed from the respective resource file. Here we will give option to change UICulture in 3 different cultures, “en-US”, “hi-IN” and “fr-FR”.

You can also refer to my previous article “How to localize ASP.NET controls based on browser’s language and culture settings” to get started with localization

Changing UICulture of a Content Page

Step 1:

Create a new ASP.NET Web application

New Project_2013-05-18_13-01-56
Step 2:

Add some UI controls on Default.aspx page with a “meta:resourceKey” attribute. This attribute is used to get values from the resource pages

<table>
     <tr>
          <td colspan="2">
               <asp:Button ID="btnEnglish" runat="server" meta:resourceKey="btnEnglish"
 onclick="btn_Click" />
               <asp:Button ID="btnHindi" runat="server" meta:resourceKey="btnHindi"
 onclick="btn_Click" />
               <asp:Button ID="btnFrench" runat="server" meta:resourceKey="btnFrench"
 onclick="btn_Click" />
          </td>
     </tr>

     <tr>
          <td>
               <asp:Label ID="lblFirstName" runat="server"
 AssociatedControlID="txtFirstName" meta:resourceKey="lblFirstName"></asp:Label>
         </td>
         <td>
               <asp:TextBox ID="txtFirstName" runat="server" CssClass="textbox"
 meta:resourceKey="txtFirstName"></asp:TextBox>
         </td>
     </tr>

     <tr>
          <td>
               <asp:Label ID="lblLastName" runat="server"
 AssociatedControlID="txtLastName" meta:resourceKey="lblLastName"></asp:Label>
          </td>
          <td>
               <asp:TextBox ID="txtLastName" runat="server" CssClass="textbox"
 meta:resourceKey="txtLastName"></asp:TextBox>
          </td>
     </tr>
     <tr>
          <td>
               <asp:Label ID="lblMobile" runat="server"
 AssociatedControlID="txtMobile" meta:resourceKey="lblMobile"></asp:Label>
          </td>
          <td>
               <asp:TextBox ID="txtMobile" runat="server" CssClass="textbox"
 meta:resourceKey="txtMobile"></asp:TextBox>
          </td>
     </tr>

     <tr>
          <td>
               <asp:Label ID="lblOrganisation" runat="server"
 AssociatedControlID="txtOrganisation" meta:resourceKey="lblOrganisation"></asp:Label>
          </td>
          <td>
               <asp:TextBox ID="txtOrganisation" runat="server" CssClass="textbox"
 meta:resourceKey="txtOrganisation"></asp:TextBox>
          </td>
     </tr>

     <tr>
          <td valign="top">
               <asp:Label ID="lblAddress" runat="server"
 AssociatedControlID="txtAddress" meta:resourceKey="lblAddress"></asp:Label>
          </td>
          <td>
               <asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine"
 meta:resourceKey="txtAddress"></asp:TextBox>
          </td>
     </tr>
</table>

Step 3:

As you can see we have given buttons for three languages, English, Hindi and French. Add a resource file for each languages and one for default. So we will add four resource files
Default.aspx.en-US.resx
Default.aspx.fr-FR.resx
Default.aspx.hi-IN.resx
Default.aspx.resx

Please refer my previous article “How to localize ASP.NET controls based on browser’s language and culture settings” to get an idea on how to add a resource file and how to name it.

Step 4:

Add values in resource files for all the controls with “meta:resourceKey” attribute. I have given value for only “Text” property of the controls. You can also provide values for other properties like ImageUrl of an Image. You can use Google translation as an alternative to translate text in other languages like French, Hindi, and Portuguese etc

ChangeUICulture_CS - Microsoft Visual Web Developer 2010 Express_2013-05-18_13-48-08
Step 5:

Import following classes in the code behind file of Default.aspx

C#:

using System.Globalization;
using System.Threading;

VB:

Imports System.Globalization
Imports System.Threading

Then write following to change UICulture of the site on button click. Here we have override InitializeCulture function of the page to set selected UICulture

C#:

protected override void InitializeCulture()
{
     base.InitializeCulture();
     if (Session["culture"] != null)
     {
          CultureInfo ci = new CultureInfo(Session["culture"].ToString());
          Thread.CurrentThread.CurrentCulture = ci;
          Thread.CurrentThread.CurrentUICulture = ci;
     }
}

protected void btn_Click(object sender, EventArgs e)
{
     Button btn = (Button)sender;

     switch (btn.ID)
     {
          case ("btnEnglish"):
               Session["culture"] = "en-US";
               Server.Transfer(Request.Url.PathAndQuery, false);
               break;
          case ("btnHindi"):
               Session["culture"] = "hi-IN";
               Server.Transfer(Request.Url.PathAndQuery, false);
               break;
          case ("btnFrench"):
               Session["culture"] = "fr-FR";
               Server.Transfer(Request.Url.PathAndQuery, false);
               break;
     }
}

VB:

Protected Overrides Sub InitializeCulture()
     MyBase.InitializeCulture()
     If (Session("culture") IsNot Nothing) Then
          Dim ci As New CultureInfo(Session("culture").ToString())
          Thread.CurrentThread.CurrentCulture = ci
          Thread.CurrentThread.CurrentUICulture = ci
     End If
 End Sub

Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btnFrench.Click
     Dim btn As Button = sender

     Select Case btn.ID
         Case ("btnEnglish")
              Session("culture") = "en-US"
              Server.Transfer(Request.Url.PathAndQuery, False)
         Case ("btnHindi")
              Session("culture") = "hi-IN"
              Server.Transfer(Request.Url.PathAndQuery, False)
         Case ("btnFrench")
              Session("culture") = "fr-FR"
              Server.Transfer(Request.Url.PathAndQuery, False)
     End Select
End Sub

Changing UICulture of a Master Page

To change UICulture of a Master page, follow the steps 1 to 4. In step 5, replace InitilizeCulture method with OnInit method. Button click event will remain same

C#:

protected override void OnInit(EventArgs e)
{
     base.OnInit(e);
     if (Session["culture"] != null)
     {
          CultureInfo ci = new CultureInfo(Session["culture"].ToString());
          Thread.CurrentThread.CurrentCulture = ci;
          Thread.CurrentThread.CurrentUICulture = ci;
     }
}

VB:


Protected Overrides Sub OnInit(e As System.EventArgs)
     MyBase.OnInit(e)
     If (Session("culture") IsNot Nothing) Then
          Dim ci As New CultureInfo(Session("culture").ToString())
          Thread.CurrentThread.CurrentCulture = ci
          Thread.CurrentThread.CurrentUICulture = ci
     End If
 End Sub

Conclusion:

In this article we have seen how to change UICulture of a site using a button click event. Here we have given users flexibility to view content of the site in multiple languages. When user clicks on a language button, all contents of the site are displayed in that language. Output of the above codes will look like following

Home Page - Mozilla Firefox_2013-05-18_15-47-40

One thought on “Changing UICulture of master and content pages on button click in ASP.NET using C#, VB.NET

Leave a Reply

Your email address will not be published. Required fields are marked *