Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using resource files (.resx) in javascript

I'm trying to use localization in my project but I can't find a way to access my resx files from javascript. I have been looking around a bit and I don't think the 'AJAX call' method would be ideal for my project since I have quiet a lot of string that need to be fetched and it would just have to spam the server hard!

if I just put it in my HTML then it works with this code:

@using Resources
<p>@Html.Raw(ISt_Localization.January)</p>

I guess one of the things I could do is put all the strings in a hidden div and then get the content from the divs in my javascript but this wouldn't be very effective..

like image 997
Utarehpson Avatar asked Mar 14 '13 09:03

Utarehpson


People also ask

What is a .resx file and how do you use it?

The . resx resource file format consists of XML entries that specify objects and strings inside XML tags. One advantage of a . resx file is that when opened with a text editor (such as Notepad) it can be written to, parsed, and manipulated.


2 Answers

I had a similar situation and in my case, I created a separate partial view which only contained a javascript block where I put all the resource strings required for use in client side logic. Every resource string was defined as a javascript variable. You could also create an associative array.

In your partial view:

var Resources = {
        January : "@Html.Raw(ISt_Localization.January)",
        February : "@Html.Raw(ISt_Localization.February)",
        ...
};
like image 186
Jaanus Varus Avatar answered Oct 14 '22 13:10

Jaanus Varus


You can also try the below thing directly

@using Resources

<script>
var value = '@Resource.January';
/* work with value 
.......
.....

*/
</script>
like image 32
Harshada Avatar answered Oct 14 '22 13:10

Harshada