2

I am updating my ASP.NET project to use some more JavaScript and AJAX. I’ve refactored parts of my HTML so that they are created with JavaScript, but now I’m not able to use the values of my local resource files anymore.

The structure of my project is that each page/usercontrol has it’s own set of local resources and it’s own set of .js files.

Is there a simple/elegant solution to use the local resources in JavaScript? Preferably I would like to see something that takes the myfile.js file, parses it by looking at the current language, and outputs something myfile.en.js

UPDATE

How I’d like to work:

<script src="../message.js"></script>
function showMessage(){
alert(GetLocalResource("AlertMessage"));
}
jfamvg
  • 237
  • 3
  • 10

3 Answers3

2

Yes. Create an Controller, or ApiController that reads the resources from your folder or resx file.

To read from a resx file, use ResourceManager

There are plenty examples online for the general structure. Like this one: Display image from database in asp mvc. Of course, you could use this not only for images, but any type of file.

You can map an incoming parameter, like filename the way you want, and you can post-process it to replace things if you need to. You can also set some HTTP headers to handle caching.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • It's a good approach. Main problem with this would be that you may end performing a lot of requests (until it gets fully cached...). Am I wrong? – Matías Fidemraizer May 05 '14 at 13:00
  • Is this solution usable for multiple resources? The way I read your answer my JavaScript needs to make a GET call for every local recourse string, I don't think that's a elegant solution. – jfamvg May 05 '14 at 13:00
  • There's nothing stopping the controller from making sophisticated decisions about what scripts to concatenate for a single payload; or even blindly spitting out every resource in the manager. – Rex M May 05 '14 at 13:02
  • I added the last paragraph to answer your questions. – Patrick Hofman May 05 '14 at 13:02
2

In my case, I found that converting RESX files into JSON files and then loading them in the client-side JavaScript works good.

Since RESX is XML, using LINQ-to-XML you'll convert each node into a key and each value into the key value:

{ "myLabel": "hello world!" }

Now the problem will be loading the right resources for user's culture. If you use a convention like RESX and files are called like MyResources.en-US.json, if you write a cookie with current user's culture, it's just about concatenating it as follows: "MyResources." + cultureCookieValue + ".json"

Finally, you might use jQuery to load the whole resources:

$.getJSON("/JsonResources/MyResources." + cultureCookieValue + .json").done(function(resources) {

  var myLabel = resources.myLabel;
});

This is just the idea. Now, if you develop it, you may create some abstractions so your client-side code can access such resources using this approach.

Suggestion: You can convert RESX on build using MSBuild!

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • It is no longer relevant, plus I just wanted to put a :), but there is a 15 character limit :P – Lourens May 05 '14 at 13:06
  • I don't want to make an extra call for my localization because I already have a lot of ajax calls and I want to keep the number down. I can put it in a variable on rendering the basic HTML. I am going to try that! – jfamvg May 05 '14 at 13:08
  • @jfamvg Well, you can merge all resources into a single JSON file and load it for the entire page. – Matías Fidemraizer May 05 '14 at 13:23
  • @Matías Fidemraizer do you have an example how I can do this? – jfamvg May 05 '14 at 13:24
  • @jfamvg I do in my own projects and also implement it in my job, but I don't have it to the public. But if you can create the whole separate JSON files, it's about using file I/O and concatenating all in a single file, then `File.WriteAllText` and go! – Matías Fidemraizer May 05 '14 at 14:26
0

Here is an example on how to write your own Http Handler to create minified JavaScript on the fly. Perhaps you can extend this with translation options and output not only minified, but also translated files.

http://weboptimizer.codeplex.com/

Tys
  • 3,592
  • 9
  • 49
  • 71