locked
how to store selected language on multilingual site RRS feed

  • Question

  • User-47461065 posted

    I have a page that has dropdownlist and with save button which saves language. Now I need to store a language in browser that was saved lastly. For example if the language is Hindi, then next time if I open the site, it should open in Hindi language.

    how to store selected language in browser?

    Please help me!! I am new to this.

    My code for base page and master page are as follows:

    class page which the child pages inherits:
    Public Const LanguageDropDownName As String = "ctl00$Contentplaceholder1$ddlLanguage"     Public Const PostBackEventTarget As String = "__EVENTTARGET"
    Protected Overrides Sub InitializeCulture()
         
       If (Request(PostBackEventTarget)) IsNot Nothing Then
                Dim controlID As String = Request(PostBackEventTarget)
    
                If controlID.Equals(LanguageDropDownName) Then
    
                    Dim selectedValue As String = Request.Form(Request(PostBackEventTarget)).ToString()
    
                    Select Case selectedValue
                        Case "0"
                            SetCulture("en-US", "en-US")
                            Exit Select
                        Case "1"
                            SetCulture("te-IN", "te-IN")
                            Exit Select
                        Case Else
                            Exit Select
                    End Select
    
                End If
    
            End If
    
            If Session("MyUICulture") IsNot Nothing AndAlso Session("MyCulture") IsNot Nothing Then
                Thread.CurrentThread.CurrentUICulture = DirectCast(Session("MyUICulture"), CultureInfo)
                Thread.CurrentThread.CurrentCulture = DirectCast(Session("MyCulture"), CultureInfo)
            End If
    MyBase.InitializeCulture()
        End Sub
    
    Protected Sub SetCulture(ByVal name As String, ByVal locale As String)
            Thread.CurrentThread.CurrentUICulture = New CultureInfo(name)
            Thread.CurrentThread.CurrentCulture = New CultureInfo(locale)
            Session("MyUICulture") = Thread.CurrentThread.CurrentUICulture
            Session("MyCulture") = Thread.CurrentThread.CurrentCulture
        End Sub
    
    

    This is the master page code which I am using now:

    if Not IsPostBack() Then
            If Session("Language") IsNot Nothing Then
                ddlLanguage.SelectedIndex = ddlLanguage.Items.IndexOf(ddlLanguage.Items.FindByValue(Convert.ToString(Session("Language"))))
                End If
           End If
    End Sub
    
    
    Protected Sub ddlLanguage_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlLanguage.SelectedIndexChanged
            Session("Language") = ddlLanguage.SelectedValue
    End Sub
    

    But i have to keep the dropdownlist in separate page .

    how can i store the language?


    Wednesday, March 20, 2013 1:52 AM

Answers

  • User-47461065 posted

    Thanks for helping me.

    Here is the solution I got:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
    Dim cultureCookie As HttpCookie = Request.Cookies("Culture")
    Dim cultureCode As String = If((cultureCookie IsNot Nothing), cultureCookie.Value, Nothing)
    If Not (String.IsNullOrEmpty(cultureCookie)) Then
    ddlLanguage.SelectedValue =  cultureCode
    End If
    End If
    End Sub
    
    
    Protected Sub ddlLanguage_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlLanguage.SelectedIndexChanged
    Request.Cookies.Add(New HttpCookie("Culture", ddlLanguage.SelectedValue))
    Response.Redirect(Request.Url.AbsolutePath)
    End Sub
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 22, 2013 5:47 AM

All replies

  • User1630798415 posted

    Use statemanagement techniques. Like cookies, Sessions, viewstate.

    Wednesday, March 20, 2013 2:00 AM
  • User-47461065 posted

    Hi thanks for immediate response.

    Can you provide some sample ?

    Wednesday, March 20, 2013 2:04 AM
  • User1630798415 posted

    here is one example to create cookie

    http://asp-net-example.blogspot.in/2009/01/aspnet-cookie-example-how-to-create.html

    When you visit same page next time to get saved language, you have to check cookie is exist or not. here is help 

    http://www.bytechaser.com/en/functions/xpfuz66p54/cookieexists-test-for-cookie-presence.aspx

    Wednesday, March 20, 2013 2:30 AM
  • User-47461065 posted

    Thanks for helping me.

    Here is the solution I got:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
    Dim cultureCookie As HttpCookie = Request.Cookies("Culture")
    Dim cultureCode As String = If((cultureCookie IsNot Nothing), cultureCookie.Value, Nothing)
    If Not (String.IsNullOrEmpty(cultureCookie)) Then
    ddlLanguage.SelectedValue =  cultureCode
    End If
    End If
    End Sub
    
    
    Protected Sub ddlLanguage_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlLanguage.SelectedIndexChanged
    Request.Cookies.Add(New HttpCookie("Culture", ddlLanguage.SelectedValue))
    Response.Redirect(Request.Url.AbsolutePath)
    End Sub
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 22, 2013 5:47 AM