ASP.Net Localization

To use resources first create Resource files for a page, to generate resources to go Tools - > Generate Local Resources.
A resource file for that page will be generated in App_LocalResources Folder, for example if i have generated a file against Default.aspx then a resource file will be generated in App_LocalResources with the name Default.aspx.resx, this will be the default resource file having mapping of all the controls texts in the resource. Now Create other resource file with the value in other language against same keys. eg i want to use arabic and i created a file with the name Default.aspx.ar-EG.resx. now to use this resource file i just have to override InitalizeCulture method and have to set the culture value the code is given below.

To set the culture and UI culture for an ASP.NET Web page programmatically

  1. Override the InitializeCulture method for the page.
  2. In the overridden method, determine which language and culture to set the page to.
  3. Set the UI culture and culture in one of the following ways:

  • Set the Culture and UICulture properties of the page to the language and culture string (for example, en-US). These properties are internal to the page, and can only be used in a page.
  • Set the CurrentUICulture and CurrentCulture properties of the current thread to the UI culture and culture, respectively. The CurrentUICulture property takes a language and culture information string. To set the CurrentCulture property, you create an instance of the CultureInfo class and call its CreateSpecificCulture method.
Following is the code to override InitializeCulture.

 protected override void InitializeCulture()
 {


     if (Session["UICulture"] != null)
     {
         if (Session["UICulture"].ToString() == "ar-EG")
         {
          
             Thread.CurrentThread.CurrentUICulture = new CultureInfo(Session["UICulture"].ToString());
             Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Session["UICulture"].ToString());

         }
     }
 }


Add Following code to your Event ( Language select from dropdownlist or button )


    protected void Button2_Click(object sender, EventArgs e)
    {
        Session["UICulture"] = "ar-EG";
        SelectedLanguage.Value = "ar-EG";
        //Force re-initialization of the page to fire InitializeCulture()
        Response.Redirect(Request.Url.LocalPath);
    }

Comments

Popular posts from this blog

Use configSource attribute to manage Web.Config sections in ASP.Net 2.0

DevOps - Key Concepts - Roles & Responsibilities - Tools & Technologies