Skip to content

Latest commit

 

History

History
65 lines (51 loc) · 1.41 KB

README.md

File metadata and controls

65 lines (51 loc) · 1.41 KB

malal

a lightweight i18n and l10n library for node.js and the browser Coverage Status

  • Version 2 is now use ES6 modules, for CJS modules please use version 1 *

Installation

$ yarn add malal

If you prefer using npm:

$ npm i -S malal

Usage

const m = require('malal')
// decalre your translation mapping or parse a json file
const en = { greeting : { _hello : 'hello {{name}}' }}
const de = { greeting : { _hello : 'hallo {{name}}' }}
m.set('en', en)
m.set('de', de)

by default the first language is used

.__ is an alias of .tanslate

m.__('greeting._hello') // >> 'hello {{name}}'
m.translate('greeting._hello') // >> 'hello {{name}}'

change the language

m.use('de') // >> 'hello {{name}}'
m.__('greeting._hello') // >> 'hallo {{name}}'

set default context

m.context = { name: 'Adam'}
m.__('greeting._hello') // >> 'hallo Adam'

set in place context

m.context = { name: 'Adam'}
m.__('greeting._hello', { name: 'Sami'}) // >> 'hallo Sami'

composable use

 m.set('en', en)
  .set('de', de)
  .use('de')
  .setContext({ name: 'Adam'})
  .__('greeting._hello', { name: 'Sami'}) // >> 'hallo Sami'