0

What is the best approach to have Resource Multi-Language values in an external JS file?

I found different approaches but I can't put them in external JS file, and I would like to get feedback:

1º var in the View

<script> var buttonLabel = @Resources.MapCenterButtonTitle</script>

2º Hidden Input in the View

@Html.Hidden("buttonLabel", Resources.MapCenterButtonTitle)

3º I tried this one https://stackoverflow.com/a/6217109/1480877 but it does not allow Resource values in const var

4º Any other that could be better then those.

Thanks

Community
  • 1
  • 1
Patrick
  • 2,995
  • 14
  • 64
  • 125

1 Answers1

0

I do it this way:

script file:

function InitSomething(resources){

}

view:

@{
    var serializer = new JavaScriptSerializer();
    var resources = serializer.Serialize(new { buttonLabel = Resources.MapCenterButtonTitle });
}

<script type="text/javascript" src="@Url.Content("src")" onload="InitSomething(@resources)"></script>
karaxuna
  • 26,752
  • 13
  • 82
  • 117