Changing UICulture of Master and Content Pages on Button Click in ASP.NET Using C#, VB.NET

Introduction

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

When the user selects a language, the UICulture of the page is set to that specific language and the page contents are displayed from the respective resource file. Here we will provide an option to change UICulture to one of 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 the UICulture of a Content Page

Step 1

Create a new ASP.NET Web application.

ASP.NET1.jpg

Step 2

Add some UI controls on the Default.aspx page with a "meta:resourceKey" attribute. This attribute gets values from the resource pages
  1. <table class="tbl">  
  2.       <tr>  
  3.           <td colspan="2">  
  4.               <asp:Button ID="btnEnglish" runat="server" meta:resourceKey="btnEnglish"  
  5.                   onclick="btn_Click" />  
  6.               <asp:Button ID="btnHindi" runat="server" meta:resourceKey="btnHindi"  
  7.                   onclick="btn_Click" />  
  8.               <asp:Button ID="btnFrench" runat="server" meta:resourceKey="btnFrench"  
  9.                   onclick="btn_Click" />  
  10.           </td>  
  11.       </tr>  
  12.       <tr>  
  13.           <td>  
  14.               <asp:Label ID="lblFirstName" runat="server"   
  15.               AssociatedControlID="txtFirstName" meta:resourceKey="lblFirstName"></asp:Label>  
  16.           </td>  
  17.           <td>  
  18.               <asp:TextBox ID="txtFirstName" runat="server" CssClass="textbox"  
  19.               meta:resourceKey="txtFirstName"></asp:TextBox>  
  20.           </td>  
  21.       </tr>  
  22.       <tr>  
  23.           <td>  
  24.               <asp:Label ID="lblLastName" runat="server"  
  25.               AssociatedControlID="txtLastName" meta:resourceKey="lblLastName"></asp:Label>  
  26.           </td>  
  27.           <td>  
  28.               <asp:TextBox ID="txtLastName" runat="server" CssClass="textbox"  
  29.               meta:resourceKey="txtLastName"></asp:TextBox>  
  30.           </td>  
  31.       </tr>  
  32.       <tr>  
  33.           <td>  
  34.               <asp:Label ID="lblMobile" runat="server"  
  35.               AssociatedControlID="txtMobile" meta:resourceKey="lblMobile"></asp:Label>  
  36.           </td>  
  37.           <td>  
  38.               <asp:TextBox ID="txtMobile" runat="server" CssClass="textbox"  
  39.               meta:resourceKey="txtMobile"></asp:TextBox>  
  40.           </td>  
  41.       </tr>  
  42.       <tr>  
  43.           <td>  
  44.               <asp:Label ID="lblOrganisation" runat="server"  
  45.               AssociatedControlID="txtOrganisation" meta:resourceKey="lblOrganisation"></asp:Label>  
  46.           </td>  
  47.           <td>  
  48.               <asp:TextBox ID="txtOrganisation" runat="server" CssClass="textbox"  
  49.               meta:resourceKey="txtOrganisation"></asp:TextBox>  
  50.           </td>  
  51.       </tr>  
  52.       <tr>  
  53.           <td valign="top">  
  54.               <asp:Label ID="lblAddress" runat="server"  
  55.               AssociatedControlID="txtAddress" meta:resourceKey="lblAddress"></asp:Label>  
  56.           </td>  
  57.           <td>  
  58.               <asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine"  
  59.               meta:resourceKey="txtAddress"></asp:TextBox>  
  60.           </td>  
  61.       </tr>  
  62.   </table> 

Step 3

As you can see, we are using buttons for the three languages, English, Hindi and French. Add a resource file for each language and one for the default. So we will add the four resource files:

  • Default.aspx.en-US.resx

  • Default.aspx.fr-FR.resx

  • Default.aspx.hi-IN.resx

  • Default.aspx.resx

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

Step 4

Add values to the resource files for all the controls with the "meta:resourceKey" attribute. I have given a value for only the "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 to other languages, like French, Hindi, and Portuguese etcetera.

ASP.NET2.jpg

Step 5

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

C#

  1. using System.Globalization;  
  2. using System.Threading;
VB 

  1. Imports System.Globalization  
  2. Imports System.Threading 

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

C#

  1. protected override void InitializeCulture()  
  2. {  
  3.     base.InitializeCulture();  
  4.     if (Session["culture"] != null)  
  5.     {  
  6.         CultureInfo ci = new CultureInfo(Session["culture"].ToString());  
  7.         Thread.CurrentThread.CurrentCulture = ci;  
  8.         Thread.CurrentThread.CurrentUICulture = ci;  
  9.     }  
  10. }  
  11. protected void btn_Click(object sender, EventArgs e)  
  12. {  
  13.     Button btn = (Button)sender;  
  14.     switch (btn.ID)  
  15.     {  
  16.         case ("btnEnglish"):  
  17.             Session["culture"] = "en-US";  
  18.             Server.Transfer(Request.Url.PathAndQuery, false);  
  19.             break;  
  20.         case ("btnHindi"):  
  21.             Session["culture"] = "hi-IN";  
  22.             Server.Transfer(Request.Url.PathAndQuery, false);  
  23.             break;  
  24.         case ("btnFrench"):  
  25.             Session["culture"] = "fr-FR";  
  26.             Server.Transfer(Request.Url.PathAndQuery, false);  
  27.             break;  
  28.     }  
  29. } 

VB

  1. Protected Overrides Sub InitializeCulture()  
  2.     MyBase.InitializeCulture()  
  3.     If (Session("culture") IsNot NothingThen  
  4.         Dim ci As New CultureInfo(Session("culture").ToString())  
  5.         Thread.CurrentThread.CurrentCulture = ci  
  6.         Thread.CurrentThread.CurrentUICulture = ci  
  7.     End If  
  8. End Sub  
  9. Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btnFrench.Click  
  10.     Dim btn As Button = sender  
  11.     Select Case btn.ID  
  12.         Case ("btnEnglish")  
  13.             Session("culture") = "en-US"  
  14.             Server.Transfer(Request.Url.PathAndQuery, False)  
  15.         Case ("btnHindi")  
  16.             Session("culture") = "hi-IN"  
  17.             Server.Transfer(Request.Url.PathAndQuery, False)  
  18.         Case ("btnFrench")  
  19.             Session("culture") = "fr-FR"  
  20.             Server.Transfer(Request.Url.PathAndQuery, False)  
  21.     End Select  
  22. End Sub 

Changing UICulture of a Master Page

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

C#

  1. protected override void OnInit(EventArgs e)  
  2. {  
  3.     base.OnInit(e);  
  4.     if (Session["culture"] != null)  
  5.     {  
  6.         CultureInfo ci = new CultureInfo(Session["culture"].ToString());  
  7.         Thread.CurrentThread.CurrentCulture = ci;  
  8.         Thread.CurrentThread.CurrentUICulture = ci;  
  9.     }  
  10. } 

VB

  1. Protected Overrides Sub OnInit(e As System.EventArgs)  
  2.     MyBase.OnInit(e)  
  3.     If (Session("culture") IsNot NothingThen  
  4.         Dim ci As New CultureInfo(Session("culture").ToString())  
  5.         Thread.CurrentThread.CurrentCulture = ci  
  6.         Thread.CurrentThread.CurrentUICulture = ci  
  7.     End If  
  8. 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 the ability to view content of the site in multiple languages. When the user clicks on a language button, all contents of the site are displayed in that language.


Similar Articles