Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET
Article

New language for a existing web application

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
5 Jul 2008CPOL4 min read 27.2K   18   2
How to create and deploy resource files to an already installed web application

Introduction

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:

new-project.png

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:

default.png

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:

properties.png

Save, close and double-click on button in default.aspx. Add this line in code behind:

C#
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:

global.png

Now, in order to set the text property of Button1 you can write, in Page_Load:

C#
Button1.Text = Resources.LWAGlobalResources.ButtonText;

...or

C#
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:

ASP.NET
<%@ 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):

web-setup.png

...and then add the project output:

setup-add.png

Click on OK, build then install on local IIS. When prompted choose LWA as virtual directory name:

setup-web-app.png

Go ahead and check it on http://localhost/lwa :

test-ie.png

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[0] will return the first language in browser:

internet-options-de.png

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:

language-pack-add-file.png

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:

language-pack-setup.png

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 .

History

That's the first version of this article, published on 6th July 2008.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Romania Romania
.NET developer now working as a freelancer.

Comments and Discussions

 
Generalgood article Pin
vegeta4ss9-Jul-08 7:19
vegeta4ss9-Jul-08 7:19 
this is a good solution to a common problem I have started seeing more frequently.

However, with all of this data in the resources files..can we keep it synchronized?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.