Multilingual Web Forms Application: Problem with Controls texts RRS feed

  • Question

  • User223078540 posted

    I want the users of my web forms application to select the Site language from a DropDownList control.

    I am offering two languages: English and Spanish.

    I used VS 2010 Web Forms Application template, so I have a Master Page (Site.Master) and two pages (Default.aspx and About.aspx).

    In the Session section of the global asax file I created a variable strLanguage.

    In SiteMaster I added the DropDownList with 3 Items: "", "English" and "Español".

    In the selected index changed event I added the following code:

     If ddlLanguage.SelectedValue <> "" Then
                Session("strLanguage") = ddlLanguage.SelectedValue
            End If

    In my "default" and "about" pages I added code to:

    Imports System.Threading
    Imports System.Globalization

    And, in the load event, added the code

    Select Case Session("strLanguage")
                Case "English"
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en")
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en")
                Case "Español"
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("es")
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("es")
            End Select

    I created two resource files:
     Strings.resx (English strings)

    If I add text in the form

     <%= Resources.Strings.txtLogin%>

    It works ok. The string appears in the DropDownList selected language

    But, If I try to use a label control and write the following code

    <asp:Label ID="Label1" runat="server" Text="<%$ Resources:Strings, txtCL %>"> </asp:Label>

    The string is shown always in English.

    I checked the string key and it is correct: txtCL.

    I tried with the string txtLogin and it works as free text, but the label text is always in English.

    Any help to solve this?

    Is this the best way to develop the multilingual web forms application?

    Thank you

    Monday, June 14, 2010 1:35 AM

All replies

  • User-925286913 posted

    You need to set culture in InitializeCulture() event.



    Monday, June 14, 2010 4:44 AM
  • User223078540 posted

    Thank you chintanpshah,

    I read the post you pointed out and followed their instructions.

    I had two problems with this post:

    1. I am using a Master Page and the drop down list is in it. Since master Pages are not pages, but controls, they don't take the Page directive.

    2. I wanted to store the selected language in a session variable and session is not available at the time InitializeCulture() is executed.

    I found this post: http://forums.asp.net/t/969928.aspx?PageIndex=1 from rmprimo that provides a solution with master pages and global.asax.

    I wrote a VB version of rmprimo solution (slightly different):

    In Global.asax.vb file:

    Imports System.Threading
    Imports System.Globalization

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
            ' Fires at the beginning of each request
            Dim lang As String = String.Empty 'default to the invariant culture
            Dim cookie As HttpCookie = Request.Cookies("SelectedLanguage")
            If Not IsNothing(cookie) Then
                lang = cookie.Value
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang)
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
            End If
    End Sub 

    In the master page:

    <asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="True">
              <asp:ListItem Value="en">English</asp:ListItem>
              <asp:ListItem Value="es">Español</asp:ListItem>

    The selected index changed handler:

    Protected Sub ddlLanguage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlLanguage.SelectedIndexChanged
            If ddlLanguage.SelectedValue <> "" Then
                Dim cookie As New HttpCookie("SelectedLanguage")
                cookie.Value = ddlLanguage.SelectedValue
                cookie.Expires = Now.AddDays(1)
            End If
    End Sub


    The only differences are:

    1. I store in the cookie the drop down list selected value, instead of its UniqueID.

    2. I set the cookie expiration day to 24 hours from the moment it is created / Updated.


    danludwig, one of the commenters for rmprimo post, is showing a solution using a session variable instead of cookies (http://forums.asp.net/t/969928.aspx?PageIndex=2). Altough I haven't tested it, it looks fine to me.

    Thak you again,

    Tuesday, June 15, 2010 1:35 AM
  • User-925286913 posted

    I personally use Coockies to store user selected language so that we can restore it on futures user sessions.

    I also have settings section in my Admin Panel, where admin can select default language of website. If coockie is not found, I am using language set by administrator.

    Tuesday, June 15, 2010 3:26 AM
  • User223078540 posted

    I was told it is bad taste to use cookies in a public application, so I tried danludwig's cookieless solution (http://forums.asp.net/t/969928.aspx?PageIndex=2) and it worked.

    With this code, the application opens in IE preferred language (Internet Options-General-Languages) and if the user selects a language from the dropdown control in Site.Master it changes to the selected language.

    Note that here we only change uiculture (the user interface) and not culture (date, money, and numbers formats).

    The Visual Basic code I am using is: 

    'In Site.Master
    <asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlLanguage_SelectedIndexChanged" OnPreRender="ddlLanguage_PreRender">
         <asp:ListItem Value="en">English</asp:ListItem>
         <asp:ListItem Value="es" selected="True">Español</asp:ListItem>
    'In Site.Master.vb
    Protected Sub ddlLanguage_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlLanguage.PreRender
            Dim ddl As DropDownList = sender
            ddl.SelectedValue = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName
    End Sub
    Protected Sub ddlLanguage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlLanguage.SelectedIndexChanged
        Dim ddl As DropDownList = sender
        Session("SelectedLanguage") = ddl.SelectedValue
        Response.Redirect(Page.AppRelativeVirtualPath, True) 'No query string
    End Sub
    'In Global.asax
    Private Sub Global_asax_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRequestHandlerExecute
        If Not IsNothing(Context.Session) AndAlso Not IsNothing(Context.Session("SelectedLanguage")) Then
            Dim SelectedLanguage As String = Context.Session("SelectedLanguage").ToString().ToLower()
            Dim CurrentLanguage As String = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.ToLower()
            If Not CurrentLanguage.Equals(SelectedLanguage) Then Thread.CurrentThread.CurrentUICulture = New CultureInfo(Session("selectedLanguage").ToString)
        End If
    End Sub
    'In Web.Config, inside <system.web>
    <globalization culture="auto" uiCulture="auto" />
    Monday, July 12, 2010 11:47 PM
  • User-925286913 posted

    Will above code retain user selected language for future?

    Tuesday, July 13, 2010 1:53 AM
  • User223078540 posted

    It won't. Every time you open the application the language will revert to the browser preferred language. 

    Tuesday, July 13, 2010 7:50 AM