9951 explained code solutions for 126 technologies


backbone.jsHow can I implement internationalization in a Backbone.js application?


Internationalization (i18n) in a Backbone.js application can be implemented using the i18next library. This library provides a simple API to access and manage internationalization data.

Here is an example of how to use i18next with Backbone.js:

// Initialize i18next
i18n.init({
  lng: 'en',
  resources: {
    en: {
      translation: {
        hello: 'Hello world'
      }
    }
  }
});

// Create Backbone model
var MyModel = Backbone.Model.extend({
  // Use i18next to get internationalized string
  getMessage: function() {
    return i18n.t('hello');
  }
});

// Create model instance
var myModel = new MyModel();

// Output internationalized string
console.log(myModel.getMessage());

Output example

Hello world

The code above initializes i18next with a single language (English) and a single translation string (hello). Then it creates a Backbone model with a getMessage method which uses i18next to get the internationalized string. Finally, it creates a model instance and outputs the internationalized string.

Helpful links

Edit this code on GitHub