views:

62

answers:

2

Hi, How to use localized messages in javascript file in asp.net.

i have a javascript file(global-Scripts.js) which contains all my scripts.i use another file (messages.js) which has all the static error messages( ex: are you sure you want to delete ?) so that this can be localized.Is it possible localize this main script file without having another file(messages.js),so that i can remove one script reference ...

A: 

Take a look at jQuery Globalization Plugin from Microsoft.

I suggest for ease of use that you have them separated but use a tool like SquishIt to bundle them into one so that only one file needs to be downloaded like you want.

XIII
This is not related to my question at all
In what sense? Your question's about globalization of javascript. Microsoft recently released such a library to make it easier so I pointed out to that.
XIII
+1  A: 

The ASP.NET ScriptManager control has the ability to provide localization for your scripts in a couple of ways.

If your script is embedded in your assembly as a resource using the WebResourceAttribute then you can use the ScriptResourceAttribute to let the ScriptManager know that you have some localized strings stored in a .resx file somewhere that you want served up any time your script is served. These strings get injected into the page as a JSON object and then in your main script you output references to the JSON object rather than literal strings.

For example, you would embed your script like this:

[assembly: System.Web.UI.WebResource("ProjectNamespace.MyScript.js", "application/x-javascript")]
[assembly: System.Web.UI.ScriptResource("ProjectNamespace.MyScript.js", "ProjectNamespace.MyScriptResources", "Messages")]

The "ProjectNamespace.MyScript.js" is the full path to the embedded resource that is your script. In the ScriptResourceAttribute, the second parameter is the full path to the embedded .resx file (minus the .resx extension) that contains all of the localized messages. You treat that just like any other .resx, so you'd have MyScriptResources.resx for the default culture, then MyScriptResources.es-MX.resx for Mexican Spanish overrides, etc. That last parameter in the ScriptResourceAttribute is the name of the JSON object that will be generated.

In your script, you reference the JSON object:

function DoSomething()
{
  alert(Messages.ErrorMessage);
}

In the above snippet, "ErrorMessage" is the name of one of the string resources in the .resx file.

If you embed the script, reference it from the ScriptManager using a tag that specifies an Assembly and a Name.

Alternatively, you can keep fully localized copies of the script, like "MyScript.js," "MyScript.es-MX.js," "MyScript.en-UK.js," etc. where the localized logic and messages are hardcoded right into the script.

If you do this localization method, reference it from the ScriptManager using a that specifies a Path.

There is a really nice overview and links to detailed walkthroughs on this with code examples on MSDN.

Note that if you are using ASP.NET MVC, the ScriptManager control doesn't really work with it. In that case, you'll want to look at a different solution like the jQuery globalization plugin or potentially a custom ScriptManager replacement for use in MVC.

Travis Illig