kingRauk
kingRauk

Reputation: 1259

Internationalization with MVC 5 and jQuery

Have a MVC 5 app that uses the Swedish and English languages. I'm using resource files to change language as needed on the server side, but I also need to change the error messages in JavaScript/jQuery.

Does jQuery support internationalization/localization? How does it work and can you provide an example?

Upvotes: 0

Views: 1052

Answers (1)

Sajad Deyargaroo
Sajad Deyargaroo

Reputation: 1149

On client side you can use jquery-localize, which allows to internatiionalization/localization based on JSON translation files.

It supports attribute based binding eg in below example the text in the header tag will be populated based on the current language.

<h1 data-localize="greeting"> Hello! </h1>

The data will be retreived from the greeting property of the object in JSON translation file.

Below example illustrates how data can be retrieved directly so that we can set is programatically eg in case of error messages.

Create a example-fr.json file for french, as shown below.

{
  "greeting": "Bonjour!"
}

Multiple files can be created for different languages eg example-es.json, etc. Then we can get the greeting text in any language eg below we are getting the text in french.

$("[data-localize]").localize("example", {
  language: "fr",
  callback: function(data, defaultCallback){
    greetingMsg = data.greeting;
    defaultCallback(data);
  }
})

You can find more details here.

Upvotes: 1

Related Questions