Why another localization article? Because I found no answer on such a simple problem: let’s suppose you have an ASP.NET web that you successfully deploy. You have a lot of clients in USA, in Europe and in Asia. Everything goes fine until one of your clients asks you to translate the application in its own language. This is a very simple task and you don’t have to recompile the application and send it back to your client. The only concern it’s finding a good translator that will spend some time to understand the business rules behind your application and not just literally translate word-by-word -- but that’s another story.
Using the code
Everything begins with a new web project:
Add some controls on default.aspx page: a label, a button and another label. Set the text for Label1: “Default text A” and for Label2: “Default text B”.
The page will look like this:
Now, let’s suppose that you want the text for Label2 to change in “Default text B changed” after clicking the button. For this, you will right-click on project (in solution explorer) and choose Properties. Then choose Resources tab. A new link will be visible: “This project does not contain a default resources file. Click here to create one”. Do it now. Create a new key – “ModifiedText” and type “Default text B changed” in value. You should have something like this:
Save, close and double-click on button in default.aspx. Add this line in code behind:
Label2.Text = Properties.Resources.ModifiedText;
Now let’s add a global resources file. For this, right click the project in solution Explorer – Add – Add ASP.NET Folder – choose App_GlobalResources. Right click this folder and add a new resources file; name it LWAGlobalResources.resx. Create a new key: ButtonText and assign it a value: “Push me”. You should have something like:
Now, in order to set the text property of Button1 you can write, in Page_Load:
Button1.Text = Resources.LWAGlobalResources.ButtonText;
Button1.Text = GetGlobalResourceObject( "LWAGlobalResources", "ButtonText" ).ToString();
Personally I prefer the former because it’s easier to write.
Now let’s create the local resource file. For this, go to Default.aspx and choose Tools – Generate Local Resource. Also add UICulture="auto" in header page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="LocalizationWebApp._Default" UICulture="auto"%>
Until now we just created the application. Let’s add a setup project (LWASetup):
...and then add the project output:
Click on OK, build then install on local IIS. When prompted choose LWA as virtual directory name:
Go ahead and check it on http://localhost/lwa :
So far you created the web application and installed it on client.
Now the German customer comes and says: “Hey, I have a lot of German users and would like you translate this great application for me!”. Well, many users in Germany have this configuration in Internet Explorer. If they don’t, then you can provide a “change your language” DropDownList, but for the sake of simplicity, let’s assume they have, so our Request.UserLanguages will return the first language in browser:
Back in Visual Studio and copy Resources.resx file under Properties (select it, Ctrl-C, Ctrl-V). Rename it to Resources.de.resx. Open it and translate the value of ButtonText key: “Standard-Text B verändert” (I hope this is the correct translation, at least Google says so). Do the same for LWAGlobalResources.resx and Default.aspx.resx. Translate each value in these files. Remember not to translate the keys, only the values!
Now, if you have German as first language in your Internet Explorer (as a German user has) and run the application you will see the texts in German. Of course, you can build the setup project again and send the German customer the new LWASetup.exe file. But that’s not ok; you just compiled the application. The idea is to send him only the German translations.
For this you will add a new web setup project: let’s call it LWALanguagePack. Right click on it – Add – Project output – Localized resources.
Next, right click on Web Application Folder (it should be in the left side, in File System panel) and add two new web folders: App_GlobalResources and App_LocalResources:
Browse to LWAGlobalResources.de.resx file; you find it in LocalizationWebApp\App_GlobalResources under your solution folder. Add it.
Do the same for Default.aspx.de.resx under LocalizationWebApp\App_LocalResources.
Finally, build LWALanguagePack and install it. When prompted, provide the same name of the virtual directory you entered when installed the application:
And that’s it; you just added a new language to an already installed web application. From now on, all the users that have German as first language in Internet Explorer will see messages in German; the others will see the messages in English and that’s because the fallback resource are written in English.
Points of Interest
For more information you should check http://msdn.microsoft.com/en-us/library/1021kkz0.aspx .
That's the first version of this article, published on 6th July 2008.