Search code examples
javascriptasp.net-mvc-4razorresx

How to access the .resx strings for javascript?


I'm developing a app in .net mvc and I am using the concept of Localisation by having a separate .resx file for flexibility of being able to organize the strings quickly and efficiently. And in the process, I'm having e few javascript code, that has some alerts and confirm boxes being displayed on certain actions. I do not have a separate .js file but just had embedded the code into the .cshtml file. My question is, Cant i take these alert and confirm messages from the .resx file as well?.. I have alreday browsed a lot and no fruitful result of how to use the .resx string within the javascript code? i tried something like,

              <script type="javascript">
              var confirm_text= @ResourceLocalisation.ConfirmText
              var confirmText=confirm(confirm_text);
              if(confirmText==true)
              .....

But this dint work.. What am I missing?.. Can Someone help me achieve localistion for javascript code?.. My question may be a dupe of, How to get texts from Resx to be used in Javascript?? but I'm posting this only because I dint get my question answered in this post..

Is there some configuration that i should be doing in web.config file aswell ?..

Thanks in Advance..;)


Solution

  • We do that all the time, and it works properly for us. Can you show a copy of the HTML generated? Also, the text should be quoted.

    @model MyModel
    @using My.App.ResourcesProject  // if needed
    
    <script type="text/javascript">
        var str = "@LocalizedStrings.AlertText";
        alert(str);
    </script>