Git Product home page Git Product logo

translit's Introduction

Translit

A simple really small i18n library with intuitive API.

size version

Features

  • Dependency free
  • Really small
  • Compatible with Web Components and LitElement

Install

$ npm i @syu93/translit

Usage

import Translit from '@syu93/translit';

const i18n = new Translit({
  translation: {
    en: {
      hello: {
        world: 'Hello world'
      },
      itemInList: (count) => `This list contains ${count} item${count > 1 ? 's' : ''}.`
    }
  } 
});

console.log(i18n.t('hello.world'))
// => Hello world

Locales

By default Translit define the locale as English en .

You can change the locale in the constructor

import Translit from '@syu93/translit';

const i18n = new Translit({
  translation: {
      // ...
  },
  locale: 'en'
});

Or you can dynamically change the locale using the setLocale method

i18n.setLocale('fr');

Or you can even set the locale as you use it

i18n.t('hello.wold', null, 'en');

You can as well load another locale with the addLocale method

i18n.addLocale({
  fr: {
    hello : {
      world: 'Bonjour à tous'
    }
  }
});

Simple translation

Define a JSON of translation and use the path as a string to access the translation.

const i18n = new Translit({
  translation: {
    en: {
      hello: {
        world: 'Hello world'
      }
    }
  } 
});

console.log(i18n.t('hello.world'));
// => Hello world

Pluralisation

If you need a more complex translation, with pluralisation for example, you can simple define a method that takes a param and return a string.

const i18n = new Translit({
  translation: {
    en: {
      itemInList: (count) => `This list contains ${count} item${count > 1 ? 's' : ''}.`
    }
  } 
});

console.log(i18n.t('itemInList', 1));
// => This list contains 1 item

console.log(i18n.t('itemInList', 2));
// => This list contains 2 items

LitElement

To use Translit inside a LitElement, just call the this.t method inside your template

render() {
	return html`
        <section>
            <p>${this.t('text.hi')}</p>
        </section>
	`;
}

API

Translit( config : Object )

  • Translation : An object containing the translation.
  • Locale : The current locale used for translation.

setLocale( locale : String )

Locale : The language string.

Dynamically change the locale translation.

addLocale( translation : Object )

Translation : A translation object.

Dynamically add a new translation.

t( translation : String, data : Any, locale : String )

Translation : A string representing the path for the translation. Data : The data to be passed the translate function. Locale : The locale of the translation (override the default locale).

Translate a given string from the translation object.

translit's People

Contributors

syu93 avatar

Watchers

 avatar  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.