Giter Club home page Giter Club logo

backbone-i18n's Introduction

Backbone-Mustache-i18n

Content internalization and localization (i18n) with Backbone and Mustache

Features

  • Translation specific elements in Mustache templates;
  • Global access to the translations through Javascript variables;
  • Request the translations from an external API;

Requirements

Installation

  • Script Tag: <script type="text/javascript" src="http://cdn.rawgit.com/Cloudoki/backbone-i18n/master/index.js"></script>
  • Bower: bower install git://github.com/Cloudoki/backbone-i18n.git
  • npm: npm install github:Cloudoki/backbone-i18n

Usage

Initialize the plugin:

  // Define your translations
  var translations = {
    'Hello World!': 'Olá Mundo!'
  };

  var i18n = new I18n();                  // Initialize the translations plugin
  i18n.set(translations);                 // Apply the translations

Translations available through Mustache:

After initializing the plugin, the translations will be automatically hooked up to Mustache:

  var template = "{{#_i18n_}}{{title}}{{/_i18n_}}";
  this.$el.html(Mustache.render( template, {title: 'Hello World!'}));     // Render

Access Translations on Javascript:

You can also access the translations through Javascript variables:

  console.log(i18n.translate('Hello World!')); // Olá Mundo!
  console.log(i18n.translate('lost in translation')); // lost in translation

Fetch translations from resource API

// You will need to extend the i18n Backbone Model
// to provide the translations resource url
var xi18n = I18n.extend({
  url: function(){
    return '/translations/' + this.locale ? this.locale : 'en-GB';
  }
});

var i18n = new xi18n();

i18n.setLocale('pt-PT');
i18n.fetch({
  success: function(){
    console.log('translations loaded');
  },
});

Check the examples

You will need a static server

npm install -g http-server

Serve the entire project directory

http-server ./

Open your browser at http://127.0.0.1:8080/examples/

backbone-i18n's People

Contributors

efmr avatar a31859 avatar

Stargazers

 avatar trskldn avatar Toni Petrovic avatar

Watchers

James Cloos avatar Koen Betsens avatar Ricardo Malta avatar Rui Molefas avatar Cátia Araújo avatar Bram Van Oost avatar  avatar Maria Ribeiro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.