JSPM

  • Created
  • Published
  • Downloads 44
  • Score
    100M100P100Q73867F
  • License MIT

Package Exports

  • inno-trans

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (inno-trans) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

inno-trans

📜 simple localization library (inspired by laravel translation)

npm npm

Install

yarn add inno-trans
//es6
import trans from "inno-trans";

//commonjs
var trans = require('inno-trans');

Example

Basic

const lang = trans({
  locale: 'en',
  message: {
    en: { 'hello': '{name}, hello!' }
  }
})

> lang.trans('hello', {name: "byungi"})

output

byungi, hello!

Pluralization

...
messages: { en: { apples: 'one apple|many apples' } }
...

> lang.transChoice('apples', 1)
> lang.transChoice('apples', 2)

output

one apple
many apples

Complex pluralization

...
messages: { en: { apples : '{0}none|[1,19]some|[20,*]many' } }
...

> lang.transChoice('apples', 0)
> lang.transChoice('apples', 19)
> lang.transChoice('apples', 20)

output

none
some
many

Support fallback

const lang = trans({
  locale: 'en',
  fallback:['ko'], // Specifies a fallback language.
  message: {
    en: { 'hello': 'hello!' },
    ko: { 'ok': '👌' },
  }
})

> lang.trans('hello')
> lang.trans('ok')

output

hello!
👌

Support Filter

const customFilter = (message, values, locale, tag) =>{
  return '~~' + message + '~~'
}

const lang = trans({
  locale: 'en',
  filter: [customFilter],
  message: {
    en: { 'hello': 'hello!' },
  }
})

> lang.trans('hello')

output

~~hello!~~

possible to load lazily

lang
  .message('en', addMessages)
  .filter([filter1, filter2])
  .fallback(['ko', 'jp'])

License

MIT