<%@ page language="C#" %> <script runat="server"> </script> <html> <head runat="server"> <title>My Localized Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Heading" Runat="server"> </asp:Label> </div> <div> </div> </form> </body> </html>
<%@ page language="C#" meta:resourceKey="PageTitleResource1" %> <script runat="server"> </script> <html> <head runat="server"> <title>My Localized Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Heading" Runat="server" meta:resourcekey="LabelResource1"> </asp:Label> </div> <div> </div> </form> </body> </html>
The PageTitleResource1 key has a value of "My Localized Page" pulled directly from the <title /> portion of the .aspx.
If the page is executed as is, the page will display with no content, as expected. Adding a value to the LabelResource1.Text entry in the .resx file "Walk" for example, and then reloading the page will cause that value ("Walk") to be displayed as the Text of the Label control.
Localizing the web page from this point is very simple, firstly a copy of the .resx file needs to be made and placed in the LocalResources folder associated with the .aspx file. Rename the file for the appropriate culture, such as French-French (fr-FR), to be called Default.aspx.fr-fr.resx.
Within the Default.aspx.fr-fr.resx file are the Resources that were in the Default.aspx.resx file, editing the value "Walk" for LabelResource1.Text to "Paid" will complete the French version of the Web Page. Loading the page at this point will still load the default non-culture specific .resx file.
One way to show the French Resources is to change the UICulture property of the page to the appropriate culture (fr-FR). If the Property UICulture="fr-fr" is added to the Page directive and the page is reloaded you will see "Paid" load on the page.
Whilst this is handy, ASP.NET 2.0 has some even cooler features baked right in to the Framework, one of which is automatically sensing a clients culture and loading the appropriate resources based on this. Modern browsers send a value with each HTTP Request indicating the preferred culture. The Browser I am currently using (IE 6.0) sends a value of en-GB (British English) to the web server with each request.
It is quite simple to turn on the automatic Resource loading based on the browser client's preffered culture. Change the UICulture property's value to "auto" so it reads UICulture="auto" then save and reload the page. You will see that (unless you're using a French browser!) that the value displayed on the page has returned to "Walk". This is because the Web Server has decided to revert to the default .resx file as it cannot find a .resx file for your culture.
Change the default culture on your browser, on IE 6 this can be done by going to: -
Reload the Page, you will see that the Web Server has picked up your client settings and automatically served you the French Resources.
Don't forget to set your Browser back to the correct Language before continuing (or things might get a little confusing).
So there's how you can get simple Localization in ASP.NET 2.0, in the next article I'll talk about the other ways you can access Resources using code, some of that is even cooler than the above.