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

1
  • You can make the JavaScript an ashx and do the replacements in the file. Commented Mar 5, 2013 at 14:34

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

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.