locked
Globalization/Localization help needed RRS feed

  • Question

  • User765438783 posted

    Hi Friends,

    I need your help!

    My requirement is this:

    My web site need to display content in 2 languages (French - by default & English)

    I have a hyperlink or link button on top of my home page, it works like a swith,

    By default the languge is French, so the link will read like "en" and if the user changes it to English by clicking on the link "en",

    the content will show in English and the link would change to "fr" means french.

    I tried using the InitializeCulture() method, but I could not pass the value to this method.

    Please advise.

    Thank you!

    Tuesday, August 28, 2012 5:13 PM

Answers

  • User1410261963 posted

    have a look at this

    using System.Globalization; 
    using System.Threading; 
     
    public partial class _Default : System.Web.UI.Page 
    { 
        protected override void InitializeCulture() 
        { 
            base.InitializeCulture(); 
            if (Request.QueryString["Language"] != null) 
            { 
                Thread.CurrentThread.CurrentCulture = new CultureInfo(Request.QueryString["Language"]); 
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.QueryString["Language"]); 
            } 
        } 
        protected void Page_Load(object sender, EventArgs e) 
        { 
     
        } 
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            if (DropDownList1.SelectedIndex !=0) 
            { 
                //here SelectedValue as a pattern such as ar-EG or en-US 
                Response.Redirect(Request.Path+"?Language="+DropDownList1.SelectedValue); 
            } 
        } 
        protected void LinkButton1_Click(object sender, EventArgs e) 
        { 
            Response.Redirect("~/Login.aspx?Language=" + Request.QueryString["Language"]); 
        } 
    }

    Visit these links  if you wanna more info  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 30, 2012 10:05 AM

All replies

  • User371383779 posted

    I would suggest you to keep this settings in the user session. So if a user wants to see it in french then store a string fr-FR in the session on clicking of fr link. In the string "fr-FR" first two chars represents settings the globalization and next two represents the localization. For english you can store en-US.

    You need to put code that will set the currently selected locale for your page -

    protected void Page_Load(object sender, EventArgs e)
    {
    string culture = Session["SelectedCulture"];
    Thread.CurrentThread.CurrentCulture = culture;
    Thread.CurrentThread.CurrentUICulture = culture;
    }

    Hope this helps.

    Wednesday, August 29, 2012 4:32 AM
  • User765438783 posted

    Hi Pankaj,

    Thanks for your reply.

    I am trying to apply the language settings for the whole web site, so I would like to do it in the InitializeCulture method.

    When I click on the link, I need to pass what language that the user selected to the InitializeCulture method, but this method fires early in the page life cycle I could not pass that value to this method.

    Please advise if there is some thing we can do about it.

    Thank you!

    Thursday, August 30, 2012 10:03 AM
  • User1410261963 posted

    have a look at this

    using System.Globalization; 
    using System.Threading; 
     
    public partial class _Default : System.Web.UI.Page 
    { 
        protected override void InitializeCulture() 
        { 
            base.InitializeCulture(); 
            if (Request.QueryString["Language"] != null) 
            { 
                Thread.CurrentThread.CurrentCulture = new CultureInfo(Request.QueryString["Language"]); 
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.QueryString["Language"]); 
            } 
        } 
        protected void Page_Load(object sender, EventArgs e) 
        { 
     
        } 
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            if (DropDownList1.SelectedIndex !=0) 
            { 
                //here SelectedValue as a pattern such as ar-EG or en-US 
                Response.Redirect(Request.Path+"?Language="+DropDownList1.SelectedValue); 
            } 
        } 
        protected void LinkButton1_Click(object sender, EventArgs e) 
        { 
            Response.Redirect("~/Login.aspx?Language=" + Request.QueryString["Language"]); 
        } 
    }

    Visit these links  if you wanna more info  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 30, 2012 10:05 AM
  • User765438783 posted

    Thanks for your response.

    I liked your solution, I have a quick question though.

    Just curious that why should we redirect everytime?

    I was using hidden variables but unable to pass the value to the InitializeCulture method.

    Thanks for your suggestion, I would like it right away :)

    Thursday, August 30, 2012 11:58 AM