In order to localize Web applications effectively server only localization is not enough. You also need to be able to localize your JavaScript code to take advantage of localized resources. ASP.NET provides some localization features through the ScriptManager and EnableScriptLocalization option, but this mechanism is rather unwieldy and works with JavaScript file localization.

This toolset includes the ability to serve localized ASP.NET resources to JavaScript pages by using a simple JavaScriptResourceHandler.axd request which provides one or more localized JavaScript objects to the client that act as resource dictionaries. The handler's URL contains query string parameters that specify the type of resource to retrieve (local or global), the locale, the resourceset name and a few other settings that will generate a JavaScript object that is loaded as a separate script file.

Registering the HTTP Handler

To use the handler you can use the JavaScriptResourceHandler.RegisterLocalJavaScriptResources or RegistierGlobalJavaScriptResources methods. The first step is to register the handler in web.config.

To register in IIS 7:

<system.webServer> <handlers> <add name="JavaScriptResourceHandler" verb="GET" path="JavascriptResourceHandler.axd" type="Westwind.Globalization.JavaScriptResourceHandler,Westwind.Globalization"/> </handlers> </system.webServer>

To register in previous versions:

<system.web> <httpHandlers> <add name="JavaScriptResourceHandler" verb="GET" path="JavascriptResourceHandler.axd" type="Westwind.Globalization.JavaScriptResourceHandler,Westwind.Globalization"/> </httpHandlers> </system.web>

Using the Handler through Registration with Static Methods

Now to use the handler to generate both some global resources and local resources for the current page you can use code like the following:

JavaScriptResourceHandler.RegisterJavaScriptGlobalResources(this, "globalRes","resources"); JavaScriptResourceHandler.RegisterJavaScriptLocalResources(this, "localRes");

You can also access the JavaScriptResourceHandler from MVC applications:

<script src="<%= Westwind.Globalization.JavaScriptResourceHandler.GetJavaScriptResourcesUrl("globalRes","CodePasteMvc.Resources.Resources") %>" type="text/javascript"></script>

which results in two separate include scripts that get injected into the page:

<script src="JavaScriptResourceHandler.axd?ResourceSet=resources &LocaleId=en-US &VarName=globalRes&ResourceType=resdb &IsGlobal=1" type="text/javascript"></script> <script src="JavaScriptResourceHandler.axd?ResourceSet=localizationadmin/localizationadmin.aspx &LocaleId=en-US&VarName=localRes &ResourceType=resdb" type="text/javascript"></script>

Each of those scripts then contains a JavaScript representation of an object - globalRes and localRes - respectively that contains all the specified resources (local or global). The result looks something like this:

var globalRes = { CouldNotCreateNewCustomer: "Neuer Kunde konnte nicht erstellt werden", CouldNotLoadCustomer: "Kunde konnte nicht erstellt werden", CustomerSaved: "Kunde wurde gespeichert", Today: "Heute", Yesterday: "Gestern" };

and:

var localRes = { AreYouSureYouWantToRemoveValue: "Sind Sie sicher dass Sie diesen Wert l\u00F6schen wollen?", BackupComplete: "Der Backup f\u00FChrte erfolgreich durch", BackupFailed: "Der Backup konnte nicht durchgef\u00FChrt werden", BackupNotification: "Diese Operation macht einen Backup von der Lokalisationtabelle. \nM\u00F6chten Sie fortfahren?", Close: "Schliessen", FeatureDisabled: "Diese Funktion ist nicht vorhanden im on-line Demo " };

You can then use these resources in JavaScript code anywhere simply by referencing this object:

alert(globalRes.CustomerSaved); alert(localRes.Close);

Default Settings for the simpler Inclusion Methods

The RegisterXXX methods shown above are the simplified versions. The full versions provide many more options so please check out the overloads.

A couple of things are worth noting. The simplified local resource retrieval methods do not return any control properties (ie. txtName.Text - anything that contains . in the Id). Typically JavaScript code only needs informational messages rather than control property settings and so those values are not included by default. However, the more specific overloads allow to include those values as well.

Any included resources that have a . separator are turned into _ separarators since . is not valid for a JavaScript variable name. So txtName.Text it would be converted to txtName_Text.

The most specific version allows selection between the standard Resx driver and the DbResourceProvider. The default for the less specific overloadeds is to auto-detect the resource provider which checks for the existance of flags in the DbResourceProvider or DbSimpleResourceProvider.