1

I have a multilingual company website (9 languages -- Visual Studio 2010 / VB / ASP.NET 4.0). I followed a tutorial on how to make it so the client may click a flag and the page displays in that culture's language. It works great in that regard. EVERY page in the website is set to inherit BasePage.vb. I have 9 resource (.resx) files in my App_GlobalResources folder.

The Problem:

If an end-user is on, for instance, domain/default.aspx, and they click the German flag, the text will switch to German and it will last the entire session. That's good. But the URL stays the same -- domain/default.aspx. It does NOT suggest a language in the URL even though the text on the webpage has changed.

It's been recommended to me that it'd be best to have it so when a client chooses a language, the URL reads domain/default.aspx?lang=de or domain/DE/default.aspx (preferred).

My question:

How can I accomplish this? Is it easiest to use an IIS7 Rewrite rule? If so, how? Do I have to edit something on my code behind, or in my basepage.vb? Any guidance in this regard would be sincerely appreciated!

(P.S. -- I know this is lengthy, but for anybody kind enough to delve into this, I'm listing my codings below for clarity if they're necessary)

---Homepage.master-just 2 examples. "de" calls the LocalizedText.de.resx file)-----

  <asp:LinkButton ID="LinkButton8" runat="server"
  CommandArgument="en" OnClick="RequestLanguageChange_Click"
  class="flagbutton">      
  <asp:Image ID="Image8" runat="server" ImageUrl="~/images/flagen.png"
  tooltip="View this website in English" title="View this website in English"/>
  <img class="map" src="images/flaghoveren.png" alt=""/>
  </asp:LinkButton>


  <asp:LinkButton ID="LinkButton6" runat="server"
  CommandArgument="de" OnClick="RequestLanguageChange_Click"
  class="flagbutton">      
  <asp:Image ID="Image7" runat="server" ImageUrl="~/images/flagde.png"
  tooltip="View this website in Deutsch" title="View this website in Deutsch"/>
  <img class="map" src="images/flaghoverde.png" alt=""/>
  </asp:LinkButton>

---------------Homepage.master.vb code behind--------------------------

        Partial Public Class Homepage
      Inherits System.Web.UI.MasterPage
      Protected Sub Page_Load(sender As Object, e As EventArgs)
      End Sub

      Protected Sub RequestLanguageChange_Click(sender As Object, e As EventArgs)
          Dim senderLink As LinkButton = TryCast(sender, LinkButton)

          'store requested language as new culture in the session
       Session(Udev.MasterPageWithLocalization.Classes.Global.SESSION_KEY_CULTURE) = 
          senderLink.CommandArgument()

          'reload last requested page with new culture
          Server.Transfer(Request.Path)
      End Sub
  End Class

-----------------BasePage.vb (in App_Code folder)-------------------

  Imports Microsoft.VisualBasic
  Imports System
  Imports System.Data
  Imports System.Configuration
  Imports System.Globalization
  Imports System.Threading
  Imports System.Web
  Imports System.Web.Security
  Imports System.Web.UI
  Imports System.Web.UI.WebControls
  Imports System.Web.UI.WebControls.WebParts
  Imports System.Web.UI.HtmlControls

  Namespace Udev.MasterPageWithLocalization.Classes
      ''' <summary>
      ''' Custom base page used for all web forms.
      ''' </summary>
      Public Class BasePage
          Inherits Page

          Protected Overrides Sub InitializeCulture()
              'retrieve culture information from session
              Dim culture__1 As String =       
        Convert.ToString(Session([Global].SESSION_KEY_CULTURE))

              'check whether a culture is stored in the session
              If culture__1.Length > 0 Then
                  Culture = culture__1
              End If

              'set culture to current thread
              Thread.CurrentThread.CurrentCulture =   
      CultureInfo.CreateSpecificCulture(culture__1)
             Thread.CurrentThread.CurrentUICulture = New CultureInfo(culture__1)
              'call base class
              MyBase.InitializeCulture()
          End Sub
      End Class
  End Namespace

-------------Culture.vb (in App_Code folder)-------------------

  Imports Microsoft.VisualBasic
  Imports System
  Imports System.Data
  Imports System.Configuration
  Imports System.Web
  Imports System.Web.Security
  Imports System.Web.UI
  Imports System.Web.UI.WebControls
  Imports System.Web.UI.WebControls.WebParts
  Imports System.Web.UI.HtmlControls

  Namespace Udev.MasterPageWithLocalization.Classes
      ''' <summary>
      ''' This class provides ISO definitions for all cultures that are supported by
         this application.
      ''' </summary>
      Public Structure Culture
          'German - Switzerland definition
          Public Const DE As String = "de"
          'English - Great Britain definition
          Public Const EN As String = "en"
      End Structure
  End Namespace

--------------Global.vb (in App_Code folder)--------------------------

  Imports System
  Imports System.Data
  Imports System.Configuration
  Imports System.Web
  Imports System.Web.Security
  Imports System.Web.UI
  Imports System.Web.UI.WebControls
  Imports System.Web.UI.WebControls.WebParts
  Imports System.Web.UI.HtmlControls

  Namespace Udev.MasterPageWithLocalization.Classes
      ''' <summary>
      ''' Summary description for Global
      ''' </summary>
      Public Structure [Global]
          Public Const SESSION_KEY_CULTURE As String = "culture"
      End Structure
  End Namespace
0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.