Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use resources in javascript file?

Hy,

I've got a resource file, which entries I want to use in my separate javascript file with following code:

@TestMvc.Resources.Views.Home.Strings.MyString

But in my javascript file the code line would not be interpreted by the MVC 5 framework.

How can I get access to my resource strings?

Thanks for help :)

like image 569
Robert Jaskowski Avatar asked Dec 12 '13 08:12

Robert Jaskowski


People also ask

What is a resource in JavaScript?

In JSData, a Resource (Model) is the data and meta data associated with a particular RESTful entity.

How do you reference a file in JavaScript?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

What is a resources RESX file?

A . resx file contains a standard set of header information that describes the format of the resource entries, and specifies the versioning information for the XML code that parses the data. These files contain all the strings, labels, captions, and titles for all text in the three IBM Cognos Office components.


2 Answers

It's normal that MVC isn't handling the resource reference in the javascript file, since the javascript is handled client-side, and MVC handling everything server-side... That being said, there are three methods that I can think of that could solve your problem if you would like to...

1) Wrap your javascript code in a separate .cshtml file.

If this is an option for you, you could create a partial view, that contains nothing but a reference to your resources, and a script tag with your javascript inside.. After that, you just render your partial view in your actual view (just like you would render your javascript file), and let MVC render your javascript (with the desired resources) before handing it to your client.

Example of your .cshtml file:

@using TestMvc.Resources.Views.Home
<script type="text/javascript">
    $(document).ready(function(){
        alert('@Strings.MyString'); //Here you can use your resource string
    });
</script>

If you inspect the rendered script client-side, you will see that MVC has inserted the string in your javascript.

2) render your strings in variables in a script block in your .cshtml file

and in your .js file treat them like local variables (ignoring the intellisense that's telling your it cannot access the undeclared variable)

<script type="text/javascript>
    var myResourceString= "@Strings.MyString";
</script>

And in your javascript

alert(myResourceString);

3) Render your strings in a hidden field, and get the value inside your javascript.

If wrapping all your javascript in a .cshtml file isn't an option, you can always put your resource strings in hidden fields like so:

<input type="hidden" id="myResourceString" value="@Strings.MyString">

And in your javascript

alert(document.getElementById('myResourceString').value);

While I'm typing this, I see that the post is a bit old, but it might be useful for someone in the future...

like image 50
Thomas Mulder Avatar answered Oct 16 '22 02:10

Thomas Mulder


I don't think you can do that in a JavaScript file.

If you really want to do that, you should write this code inside inline scripts in Razor pages.

like image 33
albusshin Avatar answered Oct 16 '22 03:10

albusshin