locked
Localization not working RRS feed

  • Question

  • User-97721543 posted

    Hi,


    So i have this website that I want to do in spanish and english. I put a button on the page:

    			<asp:DropDownList ID="LangDropDown" runat="server" Font-Size="XX-Small" AutoPostBack="True">
    <asp:ListItem  Value="en-US">English</asp:ListItem>
    <asp:ListItem Value="es-PR">Español</asp:ListItem>
    </asp:DropDownList>

    And in code behind:

        Protected Overrides Sub InitializeCulture()
            Dim aLanguage As String = Request("LangDropDown")
            Dim bLanguage As String

            If aLanguage IsNot Nothing Or aLanguage <> "" Then

                If aLanguage = "en-US" Then
                    bLanguage = "en-US"
                Else
                    bLanguage = "es-PR"
                End If

                Profile.vLanguage = bLanguage
                Thread.CurrentThread.CurrentUICulture = New CultureInfo(bLanguage)
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(bLanguage)
            End If

        End Sub

    Notice that I put the language code in the profile information. This is because I later retrieve it, on the different pages of the website, and pass it as a parameter for all my database queries.

    Now, for all the text that doesn't come out of a database, use a label:

    <asp:Label ID="whateverLabel" runat="server" Text="<%$ Resources:resource, WhatEverText %>"></asp:Label>

    And in the App_GlobalResources folder I have two files, resource.resx and resource.en.resx, with identical variables with spanish and english values.

    So, I'm on the page, and change language and voila. Everything on the page is now Spanish. Works great. So I click on a menu item to navigate to another page within the website and everything that is from the database is in Spanish (remember I'm using the profile.vLanguage to retrieve the right  information out of the database) BUT all the labels are back in english. So the website doesn't remember I had selected spanish.

    So I think, ok, lets put something on the page load event:

        Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load

            Dim bLanguage As String = Profile.vLanguage 

                Thread.CurrentThread.CurrentUICulture = New CultureInfo(bLanguage)
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(bLanguage)

    Basically, get the language from the Profile, which I know is working since everything that is database is right, and set the language to that.

    Not Working!


    What am I doing wrong?!?

     





    Tuesday, September 21, 2010 2:23 AM

Answers

  • User-1828494216 posted

    Hi,

    By putting

    bLanguage = profile.Languge
            Dim ci As New System.Globalization.CultureInfo(bLanguage)  
            System.Threading.Thread.CurrentThread.CurrentCulture = ci  
            System.Threading.Thread.CurrentThread.CurrentUICulture = ci  
            MyBase.InitializeCulture()  

    bLanguage = profile.Languge
    Dim ci As New System.Globalization.CultureInfo(bLanguage)
    System.Threading.Thread.CurrentThread.CurrentCulture = ci
    System.Threading.Thread.CurrentThread.CurrentUICulture = ci
    MyBase.InitializeCulture()  

    On every page, right?

    Still, not working :( 

    The better way is to store culture to Session and create a common Page-derived base class and then have all your .aspx page files inherit from that.

    sample of base class

    Imports System.Globalization
    Imports System.Threading
    
        Public Class LitwarePage : Inherits Page
    
            Protected Overrides Sub InitializeCulture()
                '*** make sure to call base class implementation
                MyBase.InitializeCulture()
    
                '*** pull language preference from Session
                Dim LanguagePreference As String = Session("language")
    
                '*** set the cultures
                If LanguagePreference IsNot Nothing Then
                    Me.UICulture = LanguagePreference
                    Me.Culture = LanguagePreference
                End If
            End Sub
        End Class


    then update your aspx page definitions to derive from it instead of from the standard Page class


    Partial Class _Default : Inherits LanguagePage
        '*** page class definition goes here
    End Class
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 21, 2010 12:29 PM
  • User-97721543 posted

    On Page:

    <div class="form">
    <asp:Label ID="SetLanguageLabel" runat="server" Text="<%$ Resources:resource, SetLanguageLabel %>" CssClass="SetLang"></asp:Label>
    <asp:DropDownList ID="LangDropDown" runat="server" Font-Size="XX-Small" AutoPostBack="False" CssClass="dropdown">
    <asp:ListItem Value="en-US">English</asp:ListItem>
    <asp:ListItem Value="es-PR">Español</asp:ListItem>
    </asp:DropDownList>
    <asp:Button CssClass="LangButtom" ID="BTlang" runat="server" Text="►" Font-Size="XX-Small" Height="21px" />
    </div>

    On Code Behind:
        Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load

            If Profile.myLang = "es-PR" Then
                LangDropDown.SelectedValue = "en-US"
            Else
                LangDropDown.SelectedValue = "es-PR"
            End If
        End Sub

        Protected Sub BTlang_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles BTlang.Click

            Dim myLang As String = LangDropDown.Text
            Dim tLang As String

            If myLang IsNot Nothing Or myLang <> "" Then

                If myLang = "en-US" Then
                    tLang = "en-US"
                Else
                    tLang = "es-PR"
                End If

                Profile.myLang = tLang
                Session("language") = tLang

                Dim myURL As String = Request.RawUrl
                Response.Redirect(myURL)

            End If

        End Sub

    On App_Code/myLang.vb

    Public Class myLang : Inherits Page

        Protected Overrides Sub InitializeCulture()
            MyBase.InitializeCulture()

            If Session("language"Is Nothing Or Session("language") = "" Then
                Session("language") = HttpContext.Current.Profile.GetPropertyValue("myLang")
            End If

            Dim LanguagePreference As String = Session("language")
            If LanguagePreference IsNot Nothing Then
                Me.UICulture = LanguagePreference
                Me.Culture = LanguagePreference
            End If

        End Sub

    End Class


    Using Session AND Profile is a bit redundant, but can't read Profile.MyLanguage in myLang.vb and use Profile.MyLanguage as paramaters input in database queries and didn't want to change the code. Other than that, this works pretty good.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 19, 2010 11:15 PM

All replies

  • User-1828494216 posted

    are you missing this in your web.config?


    <globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true"/>


    allso modify your code to

        Protected Overrides Sub InitializeCulture()
            Dim aLanguage As String = Request("LangDropDown")
            Dim bLanguage As String
    
            If aLanguage IsNot Nothing Or aLanguage <> "" Then
    
                If aLanguage = "en-US" Then
                    bLanguage = "en-US"
                Else
                    bLanguage = "es-PR"
                End If
    
                Dim ci As New System.Globalization.CultureInfo(bLanguage)
                System.Threading.Thread.CurrentThread.CurrentCulture = ci
                System.Threading.Thread.CurrentThread.CurrentUICulture = ci
    
    MyBase.InitializeCulture()        
    End If
        End Sub






    Tuesday, September 21, 2010 2:44 AM
  • User-97721543 posted

    Hi,

    Thanks for the replay. Yes, I was missing that. However, I did add it and still doesn't work. (It goes inside system.web, right?)

    I also tried adding

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="default.aspx.vb" Culture="auto:es-PR" UICulture="auto:es-PR" Inherits="home" Debug="true"%>

    To the top of the page, in combination with the page load event and the globalization line on the web.config.

    Nothing. :S 

    Tuesday, September 21, 2010 2:52 AM
  • User-1828494216 posted

    Hi,

    It's going inside web.config file.


    <configuration>
       <system.web>
          <globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true"/>
       </system.web>
    </configuration>
    

    Allso try this:

        Protected Overrides Sub InitializeCulture()
            Dim aLanguage As String = Request("LangDropDown")
            Dim bLanguage As String
    
            If aLanguage IsNot Nothing Or aLanguage <> "" Then
    
                If aLanguage = "en-US" Then
                    bLanguage = "en-US"
                Else
                    bLanguage = "es-PR"
                End If
    
                Dim ci As New System.Globalization.CultureInfo(bLanguage)
                System.Threading.Thread.CurrentThread.CurrentCulture = ci
                System.Threading.Thread.CurrentThread.CurrentUICulture = ci
                MyBase.InitializeCulture()        
            End If
        End Sub


    Are you using MasterPages?

    Tuesday, September 21, 2010 2:56 AM
  • User-97721543 posted

    Thanks.

    Still not working.

    I also tried putting part of you code on the page load event: 

    bLanguage = profile.Languge

    1.         Dim ci As New System.Globalization.CultureInfo(bLanguage)  
    2.         System.Threading.Thread.CurrentThread.CurrentCulture = ci  
    3.         System.Threading.Thread.CurrentThread.CurrentUICulture = ci  
    4.         MyBase.InitializeCulture()  


    It didn't work.

    Tuesday, September 21, 2010 3:19 AM
  • User-1828494216 posted

    You are aware that you must override the culture on every page, right?


    Tuesday, September 21, 2010 3:58 AM
  • User-97721543 posted

    Hi,


    By putting

    bLanguage = profile.Languge
            Dim ci As New System.Globalization.CultureInfo(bLanguage)  
            System.Threading.Thread.CurrentThread.CurrentCulture = ci  
            System.Threading.Thread.CurrentThread.CurrentUICulture = ci  
            MyBase.InitializeCulture()  

    bLanguage = profile.Languge
    Dim ci As New System.Globalization.CultureInfo(bLanguage)
    System.Threading.Thread.CurrentThread.CurrentCulture = ci
    System.Threading.Thread.CurrentThread.CurrentUICulture = ci
    MyBase.InitializeCulture()  

    On every page, right?

    Still, not working :( 

    Tuesday, September 21, 2010 10:50 AM
  • User-1828494216 posted

    Hi,

    By putting

    bLanguage = profile.Languge
            Dim ci As New System.Globalization.CultureInfo(bLanguage)  
            System.Threading.Thread.CurrentThread.CurrentCulture = ci  
            System.Threading.Thread.CurrentThread.CurrentUICulture = ci  
            MyBase.InitializeCulture()  

    bLanguage = profile.Languge
    Dim ci As New System.Globalization.CultureInfo(bLanguage)
    System.Threading.Thread.CurrentThread.CurrentCulture = ci
    System.Threading.Thread.CurrentThread.CurrentUICulture = ci
    MyBase.InitializeCulture()  

    On every page, right?

    Still, not working :( 

    The better way is to store culture to Session and create a common Page-derived base class and then have all your .aspx page files inherit from that.

    sample of base class

    Imports System.Globalization
    Imports System.Threading
    
        Public Class LitwarePage : Inherits Page
    
            Protected Overrides Sub InitializeCulture()
                '*** make sure to call base class implementation
                MyBase.InitializeCulture()
    
                '*** pull language preference from Session
                Dim LanguagePreference As String = Session("language")
    
                '*** set the cultures
                If LanguagePreference IsNot Nothing Then
                    Me.UICulture = LanguagePreference
                    Me.Culture = LanguagePreference
                End If
            End Sub
        End Class


    then update your aspx page definitions to derive from it instead of from the standard Page class


    Partial Class _Default : Inherits LanguagePage
        '*** page class definition goes here
    End Class
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 21, 2010 12:29 PM
  • User-97721543 posted

    Hi,


    I tried all code and still nothing. So, I created to labels and in code behind:

            LabelA.Text = System.Threading.Thread.CurrentThread.CurrentCulture.ToString
            LabelB.Text = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString

    The language is correct! However, its not reading the correct resource. I mean, when I change the language it works, but if I change page, or even reenter the same page, it read the english resource (resource.en.resx) instead of the Spanish one even when both labels show es-PR as the Culture and UICulture. 

    I tried making an extra resource, so that I had:

    resource.en.resx
    resource.es.resx
    resource.resx

    the default one is spanish, stills, it reads the "en" one. So the problem is not on the code in page behind.

     Do you have to allow globalization on the server or somewhere else?

    Thanks.

     

    Tuesday, September 21, 2010 9:28 PM
  • User-97721543 posted

    On Page:

    <div class="form">
    <asp:Label ID="SetLanguageLabel" runat="server" Text="<%$ Resources:resource, SetLanguageLabel %>" CssClass="SetLang"></asp:Label>
    <asp:DropDownList ID="LangDropDown" runat="server" Font-Size="XX-Small" AutoPostBack="False" CssClass="dropdown">
    <asp:ListItem Value="en-US">English</asp:ListItem>
    <asp:ListItem Value="es-PR">Español</asp:ListItem>
    </asp:DropDownList>
    <asp:Button CssClass="LangButtom" ID="BTlang" runat="server" Text="►" Font-Size="XX-Small" Height="21px" />
    </div>

    On Code Behind:
        Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load

            If Profile.myLang = "es-PR" Then
                LangDropDown.SelectedValue = "en-US"
            Else
                LangDropDown.SelectedValue = "es-PR"
            End If
        End Sub

        Protected Sub BTlang_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles BTlang.Click

            Dim myLang As String = LangDropDown.Text
            Dim tLang As String

            If myLang IsNot Nothing Or myLang <> "" Then

                If myLang = "en-US" Then
                    tLang = "en-US"
                Else
                    tLang = "es-PR"
                End If

                Profile.myLang = tLang
                Session("language") = tLang

                Dim myURL As String = Request.RawUrl
                Response.Redirect(myURL)

            End If

        End Sub

    On App_Code/myLang.vb

    Public Class myLang : Inherits Page

        Protected Overrides Sub InitializeCulture()
            MyBase.InitializeCulture()

            If Session("language"Is Nothing Or Session("language") = "" Then
                Session("language") = HttpContext.Current.Profile.GetPropertyValue("myLang")
            End If

            Dim LanguagePreference As String = Session("language")
            If LanguagePreference IsNot Nothing Then
                Me.UICulture = LanguagePreference
                Me.Culture = LanguagePreference
            End If

        End Sub

    End Class


    Using Session AND Profile is a bit redundant, but can't read Profile.MyLanguage in myLang.vb and use Profile.MyLanguage as paramaters input in database queries and didn't want to change the code. Other than that, this works pretty good.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 19, 2010 11:15 PM