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"));
}
1
  • 3
    Put some code of what you have done so far.. How can we identify your problem Commented May 5, 2014 at 12:50

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
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.
I added the last paragraph to answer your questions.
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!

5 Comments

It is no longer relevant, plus I just wanted to put a :), but there is a 15 character limit :P
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 Well, you can merge all resources into a single JSON file and load it for the entire page.
@Matías Fidemraizer do you have an example how I can do this?
@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!
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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.