views:

1558

answers:

4

How would one get resx resource strings into javascript code stored in a .js file?

If your javascript is in a script block in the markup, you can use this syntax:

<%$Resources:Resource, FieldName %>

and it will parse the resource value in as it renders the page... Unfortunately, that will only be parsed if the javascript appears in the body of the page. In an external .js file referenced in a <script> tag, those server tags obviously never get parsed.

I don't want to have to write a ScriptService to return those resources or anything like that, since they don't change after the page is rendered so it's a waste to have something that active.

One possibility could be to write an ashx handler and point the <script> tags to that, but I'm still not sure how I would read in the .js files and parse any server tags like that before streaming the text to the client. Is there a line of code I can run that will do that task similarly to the ASP.NET parser?

Or does anyone have any other suggestions?

A: 

I usually pass the resource string as a parameter to whatever javascript function I'm calling, that way I can continue to use the expression syntax in the HTML.

Chris Shaffer
That would normally be my inclination as well, but in this case, it's a client side item clicked handler for a RadMenu, so it leads to a switch statement with 7 cases, all displaying different "are you sure?" strings :(
Grank
+1  A: 

In a nutshell, make ASP.NET serve javascript rather than HTML for a specific page. Cleanest if done as a custom IHttpHandler, but in a pinch a page will do, just remember to:

1) Clear out all the ASP.NET stuff and make it look like a JS file.

2) Set the content-type to "text/javascript" in the codebehind.

Once you have a script like this setup, you can then create a client-side copy of your resources that other client-side scripts can reference from your app.

Wyatt Barnett
+3  A: 

There's no native support for this.

I built a JavaScriptResourceHandler a while ago that can serve Serverside resources into the client page via objects where each property on the object represents a localization resource id and its value. You can check this out and download it from this blog post:

http://www.west-wind.com/Weblog/posts/698097.aspx

I've been using this extensively in a number of apps and it works well. The main win on this is that you can localize your resources in one place (Resx or in my case a custom ResourceProvider using a database) rather than having to have multiple localization schemes.

Rick Strahl
Hmm, an interesting approach. Might work for me. The drawbacks are that I have to have a separate resources file for only what I want to push out or else send extra stuff of no use... and that I'm inside sharepoint so reading the resx in as a file and parsing it may be a problem, we'll see. Thanks for the post!
Grank
A: 

I the brown field application I'm working on we have an xslt that transforms the resx file into a javascript file as part of the build process. This works well since this is a web application. I'm not sure if the original question is a web application.

Rob Murdoch