Twitter Feed Popout byInfofru

Localization in asp.net (Quick View)

In the past few days, I see some posts in forum (http://www.asp.net) regarding Localization which is actually very easy but there is no quick view to Localization in available. So I plan to write one for my blog.Localization is something by which you can provide your end user a functionality to read your content in multiple languages. To cut it short … you have content in languages as such English and French and you are managing the whole stuff on one page. That is the main concept of Localization.Now, to use the feature of localization asp.net provides you with a file called Resource File (.RESX). Remember the time of Visual Studio 2003 it is added with the page by default. This file is nothing more than a normal XML file having Key / Value pairs stored inside.The Resource files will be created under some special folders which are as follows.App_GlobalResourcesApp_LocalResourcesThe difference between two is visible by their name which is off the scope. The content of Global one is available throughout the website where as the local is limited to that specific page.
Step 1:Create both folders in your project as we will use them in future. To create1. Right click your project2. Then Asp.net Folders3. And select both one by one.
First, we will look into Local Resources. To use the local resources file which is limited to the page level, let’s first decide what we want to do.Scenario: We want to create a login page for two languages English (US) and French.
Step 2:To do this, we need to design a page which looks like as follows (design view). The page name will be defult.aspxsnapshot_lc_1.jpg Source:<br /><asp:Label ID="lblTitle" runat="server" meta:resourcekey="lblTitle"></asp:Label><br /><br /><asp:Label ID="lblUserId" runat="server" meta:resourcekey="lblUserId"></asp:Label><asp:TextBox ID="txtUserId" runat="server" ></asp:TextBox><br /><asp:Label ID="lblPassword" runat="server" meta:resourcekey="lblPassword" ></asp:Label><asp:TextBox ID="txtPassword" runat="server"></asp:TextBox><br /><br /><asp:Button ID="btnSubmit" runat="server" meta:resourcekey="btnSubmit"/><asp:Button ID="btnCancel" runat="server" meta:resourcekey="btnCancel" />
 Forget, those bad BR just want to give you the idea how localization work. Notice that we have not set text for any of our control specifically, Button and Labels instead we have one new thing here and that is meta:resourcekey=”btnSubmit”.Forget what it does, just continue with step 3. 
Step 3:1. Right - Click on App_LocalResources2. Click Add New Item3. From the list of template select Resource File4. Name the resource file as default.aspx.resx (because default.aspx is our page name, if it were supercool.aspx the resource file will be supercool.aspx.resx. Remember it is the default resource file for the page.)5. Make it look like following
snapshot_lc_2.jpgNow, you can see the relationship between meta:resourcekey=”btnSubmit” and resource file. Let me make it more easy, Notice the second entry from top. It isbtnSubmit.Text : Submit.It means by writing meta:resourcekey=”btnSubmit” on any control, asp.net runtime will bind all the properties specified in resource file to the that control.Just run the page we have created means default.aspx and you will see the text of all controls extracted from the default resource file. Now let’s create a new resource file for the same page but for different culture.
Step 4:1. Right - Click on App_LocalResources2. Click Add New Item3. From the list of template select Resource File4. Name the resource file as default.aspx.fr-FR.resx (this time we are creating resource file for our page in French. So we specify, the runtime engine understandable culture code. Please bear in mind the fr-FR is only for French to know more like this please visit http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx)5. Now make it look like following
snapshot_lc_3.jpgTo change the culture we need to add the following dropdown at the top of our page.
Step 5:Culture :<asp:DropDownList ID="ddlCulture" runat="server" AutoPostBack="True"><asp:ListItem Text="English" Value="en-US"></asp:ListItem><asp:ListItem Text="French" Value="fr-FR"></asp:ListItem></asp:DropDownList>And following code against this drop down.Protected Sub ddlCulture_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCulture.SelectedIndexChangedSession("Culture") = ddlCulture.SelectedValue 'Storing culture selected in session        Response.Redirect(Request.RawUrl)End Sub
Now, we have created the drop down stuff let’s move on how to set the culture.Step 6: 

Protected Overrides Sub InitializeCulture()

        If Not Session("Culture") = "" Then            Dim selLang As String = Session("Culture")'getting the culture from session                       Page.UICulture = selLang 'setting page culture            Page.Culture = selLang               'Setting thread culture            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selLang)            Thread.CurrentThread.CurrentUICulture = New CultureInfo(selLang)        End If        MyBase.InitializeCulture()End Sub

Note : As you might notice there are two thing we are dealing with one is Culture and the other is CultureUI. Well, Culture is somthing like the Date Time / Currency of the page where is CultureUI is the UI settings for the control such as text and other proerties.
Till here, we have finished with the local resources files. Now let’s move forward and add a new resource file in App_GlobalResource, which is for the global access.
Step 7:1. Right - Click on App_GlobalResource2. Click Add New Item3. From the list of template select Resource File4. Name the resource file as MyGlobalResource.resx. (notice there is no special need for global resources)5. Now make it look like followingsnapshot_lc_4.jpgTo check Global Resources let us write following code on the click of submit button  Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.ClickResponse.Write(GetGlobalResourceObject("MyGlobalResource", "Success_Key")) '"MyGlobalResource" is the name of the classEnd Sub
That’s it, now if you press the submit button you will see the message we store in Global Resource file. Notice that, Global resource files are not culture specific and it is normally used to store things which are similar in all cultures as such images and all that.I have tried to make this post as much simple as I can, but I think it has gone quit long as the topic of Localization is not that easy to cover in 5 lines of post so I m thinking to create a video stuff for this .