user2471435
user2471435

Reputation: 1674

Globalization of strings in MVC - JavaScript translation

I have an MVC app, in which to translate I am storing all the text fields in a .resx file and then retrieving them with:

Resources.Resource.FirstName}

I have a JavaScript file which I need to do the same for two strings. I have checked Google and saw the JQuery.Globalization library (which doesn't seem to be available anymore) but that is way overkill for just two strings. I need to translate the two "Please wait..." strings in the following. How doo I do this easily?

$("#the_button").lockSubmit({
        submitText: "Please wait..."
    });

    $(".the_button").lockSubmit({
        submitText: "Please wait..."
    });

Upvotes: 0

Views: 498

Answers (1)

py3r3str
py3r3str

Reputation: 1879

The easiest way is to define global js variabe in head section of html in layaout:

...
<script>
    var translation = {
        submitText: "@Resources.Resource.SubmitText"
    };
</script>
...

and then use this in js script

...
$("#the_button, .the_button").lockSubmit({        // you can use multiple selector here
    submitText: translation.submitText
});
...

You can also use html data-* attribute:

...
<button id="the_button" data-submit-text="@Resources.Resource.SubmitText" >Button</button>

and js file:

...
$("#the_button").lockSubmit({
    submitText: $("#the_button").attr("data-submit-text");
});
...

Upvotes: 1

Related Questions