Thursday, September 1, 2011

C# Globalisation

Global .asa
 protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["CultureInfo"];
            if (cookie != null && cookie.Value != null)
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = new  System.Globalization.CultureInfo(cookie.Value);
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value);
            }
            else
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("ro-RO");
                System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("ro-RO");
            }
        }


Master page
 - dropdownist with list of languages
   < asp:DropDownList runat="server" ID="dropLanguage" AutoPostBack="true"  runat="server"
            onselectedindexchanged="dropLanguage_SelectedIndexChanged" >
            < asp:ListItem Value="fr-FR" Text="French" />
            < asp:ListItem Value="en-GB" Text="English" />
        < /asp:DropDownList>
 - Change of the language
 protected void dropLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Sets the cookie that is to be used by Global.asax
            HttpCookie cookie = new HttpCookie("CultureInfo");
            cookie.Value = dropLanguage.SelectedValue;
            Response.Cookies.Add(cookie);
            //Set the culture and reload the page for immediate effect.
            //Future effects are handled by Global.asax
            Thread.CurrentThread.CurrentCulture =
                          new CultureInfo(dropLanguage.SelectedValue);
            Thread.CurrentThread.CurrentUICulture =
                          new CultureInfo(dropLanguage.SelectedValue);
            Server.Transfer(Request.Path);
        }

 - Create Resources Files:
create App_GlobalResources\Default.resx
           App_GlobalResources\Default.en-GB.resx
           App_GlobalResources\Default.fr-FR.resx

Use of resources:

< asp:Literal runat="server" ID="litInstruction"  Text="<%$ Resources:Default, Instruction %>"  />



















No comments:

Post a Comment