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

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

I have a Javascript file (global-Scripts.js) that contains all my scripts. I use another file (messages.js) that has all the static error messages (for example: "are you sure you want to delete?") So that this can be localized.

Is it possible to localize this main script file without another file (messages.js) so that I can remove the script link?

+6
javascript localization
source share
2 answers

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

If your script is embedded in your assembly as a resource using the WebResourceAttribute attribute , then you can use the ScriptResourceAttribute to let the ScriptManager know that you have some localized lines stored in the .resx file somewhere you want it to be- Someday your script served. These lines are entered into the page as a JSON object, and then basically the script displays links to the JSON object, and not to literary lines.

For example, you would embed your script as follows:

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

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

In the 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 a script, reference it from the ScriptManager using a tag that indicates the assembly and name.

Alternatively, you can store fully localized copies of the script , such as "MyScript.js", "MyScript.es-MX.js", "MyScript.en-UK.js", "etc., where the localized logic and messages are hard-coded directly into the script.

If you do this localization method, access it from ScriptManager using a parameter specifying the path.

There is a really good review and links to detailed step-by-step instructions on this subject with code samples on MSDN.

Note that if you use ASP.NET MVC, the ScriptManager control does not actually work with it. In this case, you need to look at another solution, such as a jQuery plugin for globalization or a potentially custom ScriptManager replacement for use in MVC .

+5
source share

I have a solution that I used to solve this problem. It allows you to use your global resources in your asp.net web application in your js files. Essentially, it uses the GetLocalisedScript.aspx page to serve your js files.

The page is as follows:

 protected void Page_Load(object sender, EventArgs e) { string retval = ""; string file = Request["js"].ToString(); using(StreamReader sr = new StreamReader(Server.MapPath(string.Format("~\\scripts\\{0}.js",file)))) { retval = sr.ReadToEnd(); sr.Close(); } Regex rx = new Regex("##LOCALISE(.+?)##",RegexOptions.Singleline); MatchCollection mc = rx.Matches(retval,0); foreach (Match m in mc) { string strResxKey = m.Value.Replace("##LOCALISE(", "").Replace(")##", ""); string val = GetGlobalResourceObject("myResource", strResxKey).ToString(); retval = retval.Replace(m.Value, val); } //Just write out the XML data Response.ContentType = "text/xml"; //NOTE THAT THIS PAGE DOESN'T CONTAIN ANY HTML TAG, WHATSOEVER Response.Output.Write(retval); } 

Mark

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetLocalisedScript.aspx.cs" Inherits="TestMulti.GetLocalisedScript" %> 

On your page, replace the standard src as follows:

 <script src="GetLocalisedScript.aspx?js=myJS" type="text/jscript" ></script> 

Where myJS is the name of your js file.

Then modify your js file to include the ## LOCALIZE () ## tags.

 function alert2(val) { alert("##LOCALISE(Yes)##"); } 

The GetLocalisedScript.aspx page will replace all tags with a resource search.

Hope this helps someone.

+1
source share

All Articles