Click here to Skip to main content
Click here to Skip to main content

New language for a existing web application

, 5 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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:

Button1.Text = Resources.LWAGlobalResources.ButtonText;

...or

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):

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)

Share

About the Author

Goosetea
Web Developer
Romania Romania
.NET developer now working as a freelancer.

Comments and Discussions

 
GeneralBest Localization Plug-in for Visual Studio. PinmemberAlexander Nesterenko17-Dec-08 22:48 
Generalgood article Pinmembervegeta4ss9-Jul-08 8:19 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 5 Jul 2008
Article Copyright 2008 by Goosetea
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid