locked
Modifying a culture for a whole application RRS feed

  • Question

  • User2026927769 posted

    Hello

    I am in charge of set up the internationalisation within an application that consists of many .aspx pages (with their related .aspx.cs code..)

    In the main page I want to select the language (fr-FR or en-US)  and propagate it in all the .aspx pages.

    Must I rewrite absolutly the method initializeCulture() of every .aspx.cs page or

    does it exist a simple way to set the new language once for all the .aspx pages??

     

    Thanks

    PE

    Tuesday, May 12, 2009 10:09 AM

Answers

  • User1282917655 posted

    you can create a base class that inherits from System.Web.UI.Page , then write your localization code in this base class, and then let every page in the website inhertis from that base class.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 12, 2009 11:03 AM
  • User641418711 posted

     Halo Abdulla.AbdelHaq,

     

    How to store the language selection in base class? 

    Are you using a session?

    I try this but got error (String reference not set to an instance of a String. Parameter name: name):

    Can you help to give me better understanding to use the base class?

    TIA

     

    Option Explicit On
    
    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
    Imports System.Globalization
    Imports System.Threading
    
    Public Delegate Sub CultureChanged(ByVal Sender As Object, ByVal CurrentCulture As String)
    
    Public Class BasePage
        Inherits Page
    
        Public Event OnCultureChanged As CultureChanged
    
        Public Property LastCultureName() As String
            Get
                Dim strlastCultureName As String = DirectCast(Session("_lastCulture"), String)
    
                If strlastCultureName Is Nothing Then
                    Session("_lastCulture") = Thread.CurrentThread.CurrentCulture.Name
                End If
    
                Return strlastCultureName
            End Get
    
            Set(ByVal value As String)
                Session("_lastCulture") = value
            End Set
    
        End Property
    
        Protected Overloads Overrides Sub InitializeCulture()
            'TODO: make this prettier
            Dim lang As String = "en-US"
            lang = Session("_lastCulture")
    
            If Session("_lastCulture") Is Nothing Then
                lang = LastCultureName
            Else
                lang = Session("_lastCulture")
            End If
    
            Thread.CurrentThread.CurrentCulture = New CultureInfo(lang)
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang)
        End Sub
    
        Protected Overloads Overrides Sub OnLoadComplete(ByVal e As EventArgs)
            MyBase.OnLoadComplete(e)
    
            If LastCultureName <> Thread.CurrentThread.CurrentCulture.Name Then
                LastCultureName = Thread.CurrentThread.CurrentCulture.Name
    
                RaiseEvent OnCultureChanged(Me, LastCultureName)
            End If
        End Sub
    End Class
      

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2009 11:20 PM

All replies

  • User1282917655 posted

    you can create a base class that inherits from System.Web.UI.Page , then write your localization code in this base class, and then let every page in the website inhertis from that base class.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 12, 2009 11:03 AM
  • User641418711 posted

     Halo Abdulla.AbdelHaq,

     

    How to store the language selection in base class? 

    Are you using a session?

    I try this but got error (String reference not set to an instance of a String. Parameter name: name):

    Can you help to give me better understanding to use the base class?

    TIA

     

    Option Explicit On
    
    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
    Imports System.Globalization
    Imports System.Threading
    
    Public Delegate Sub CultureChanged(ByVal Sender As Object, ByVal CurrentCulture As String)
    
    Public Class BasePage
        Inherits Page
    
        Public Event OnCultureChanged As CultureChanged
    
        Public Property LastCultureName() As String
            Get
                Dim strlastCultureName As String = DirectCast(Session("_lastCulture"), String)
    
                If strlastCultureName Is Nothing Then
                    Session("_lastCulture") = Thread.CurrentThread.CurrentCulture.Name
                End If
    
                Return strlastCultureName
            End Get
    
            Set(ByVal value As String)
                Session("_lastCulture") = value
            End Set
    
        End Property
    
        Protected Overloads Overrides Sub InitializeCulture()
            'TODO: make this prettier
            Dim lang As String = "en-US"
            lang = Session("_lastCulture")
    
            If Session("_lastCulture") Is Nothing Then
                lang = LastCultureName
            Else
                lang = Session("_lastCulture")
            End If
    
            Thread.CurrentThread.CurrentCulture = New CultureInfo(lang)
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang)
        End Sub
    
        Protected Overloads Overrides Sub OnLoadComplete(ByVal e As EventArgs)
            MyBase.OnLoadComplete(e)
    
            If LastCultureName <> Thread.CurrentThread.CurrentCulture.Name Then
                LastCultureName = Thread.CurrentThread.CurrentCulture.Name
    
                RaiseEvent OnCultureChanged(Me, LastCultureName)
            End If
        End Sub
    End Class
      

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2009 11:20 PM
  • User2026927769 posted

    Hello

     

    May be you have to cast the Session variable...

    ex : String lang;

    lang = (String) Session["yourvariable"]

     

     

    Tuesday, June 9, 2009 8:42 AM
  • User-1778709380 posted
    Hello,

    thanks for linking to that thread. Meanwhile I think I found a solution.

    at every page (or maybe in a master page) there some buttons for the wanted languages, each with a click-event. I save the languages in the Session. For loading the controls I override the RenderControl-method. 

    protected void btn_en_Click(object sender, EventArgs e)
        {
            Session["uiculture"] = "en";
            Session["culture"] = "en-US";
            Page.Culture = Session["culture"].ToString();
            Page.UICulture = Session["uiCulture"].ToString();
        }

    public override void RenderControl(HtmlTextWriter writer)
        {
            Page.Culture = Session["culture"].ToString();
            Page.UICulture = Session["uiCulture"].ToString();
            base.RenderControl(writer);
        }

     In that thread I read about using Thread.CurrentThread.CurrentCulture. ... At first I also tried that way. But changing the culture via using Threads... seems only to change the culture in the method where i called Threads... 

    what do you think about my way? is overriding 'rendercontrol' a good way?

    • in using resources... is it better to use global or local resources for localization?
    • in which moments it is useful to use the ResourceManager? (when using satellite assemblies?)
    • i use the strong-typed access to global resources (Resources.Resource.String1 ..); i also could use the expression property of a control - how should I use this? at the moment controls with expression aren't translated... -> should I override another method?

    very thanks,

    cortadillo

    Tuesday, November 17, 2009 6:07 AM