Coder Social home page Coder Social logo

easy-locale's Introduction

easy-locale

A simple and easy to use lib for localizing your application. Size 1.1 kb. Writen on pure JavaScript.

Demo

Usage

Including file:

 <script src="easy-locale.min.js"></script>

Install via npm:

npm install easy-locale

Initialization

(function(){
  var translations = {
    en:{
      string: 'test string',
      section:{
        substring: 'test substring'
      }
      string_with_values: 'string with value1: %{value1} and value2: %{value2}'
      string_with_count: {
        elements: {
          zero: '%{count} elements',
          one: '%{count} element',
          other: '%{count} elements'
        }
      },
    },
    ...
  }
  locale.init('en', translations);
  window.t = locale.t;
})();

Basic usage

console.log(t('string'));
// => test string
console.log(t('section.substring'));
// => test substring

Interpolate

console.log(t('string_with_values', {value1: 'first value', value2: 'second value'}));
// => string with value1: first value and value2: second value

Pluralization

console.log(t('string_with_count.elements', {count: 0}));
// => 0 elements
console.log(t('string_with_count.elements', {count: 1}));
// => 1 element

Options

singleLocale

Sometimes you may need only one locale.

For example if you build on server side dynamicly your translation based on his current locale zone and then send it to client.

var translations = {
     first: 'First string',
     second: 'Second string',
    };
var options = {
 singleLocale: true
}
// We still need set first parameter for determining pluralization logic
locale.init('en', translations, options);    


console.log(locale.t('first'));
// => First string

Helpers

getCurrent

Shows object with current translation

var translations = {
     en: {
      first: 'First string',
      second: 'Second string',
     },
     ru: {
      first: 'Первая строка',
      second: 'Вторая строка',
     }
    };
    
locale.init('en', translations);    

console.log(locale.getCurrent());
// => 
// {
//  first: 'First string',
//  second: 'Second string'
// }

easy-locale's People

Contributors

sylpheeed avatar

Watchers

 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.