Building multi-language ASP.NET site with resource localization
Last year I added additional Thai and Japanese languages to my spa website. Now that I recently got a chance to do the same for a Java and PHP web sites, I’m amazed at how easy it is to do this in ASP.NET.

To do this, I first start from designing the web page. I create a language bar user control that contain 3 link buttons for each language. I put this user control at the bottom of the page.
Now in my web project in Visual Studio, I add resource files to the project. If you are using VS2005 or 2008, the tool will automatically add App_GlobalResources folder for you. Here I create 3 resource files for 3 languages. Note that I name them all the same (I put the localized strings for entire site in one file). You can have multiple resource files as well. I also append “.<culture code>” to the end of each resource file to indicate the language that it should serve.
I go ahead and design the web page. For each text label, instead of define the string content for Text property, I set it so that the value would be automatically injected from the resource file. The value format is
<%$ Resources:<resource file name>, <resource entry name> %>
Alternatively, I can programmatically set the localized string value from C# code as well. Here’s a code-behind example.
After I have all the required labels laid out, I can now go to the resource file and enter the localized string values. Note that HTML tag is also allowed for localized string value.
Lastly, the most important piece. How you can go about telling ASP.NET of the language you choose. The key here is to set the right UICulture for the current thread. The way I do this is to keep the current culture code in a session value. Once the user selects preferred language from the user control, I assigned the value to the current thread.

Using the selected language’s culture info code to create CultureInfo object and assign it to the current thread.
You also have another option by setting your web page to automatically localize display string by the browser’s current culture. Simply set aspx page Culture attribute to “auto” and the ASP.NET runtime will use browser’s current culture setting by default.
And that’s it. I didn’t realize how easy it was until I had to do this same thing on other platform.
Teera on April 6th 2008 in ASP.NET, Software Development, Web Dev, .NET





javier alvarado responded on 19 Jan 2009 at 12:36 pm #
please send to me the source code… if you can…