0.1.1 • Published 5 years ago

lorgar v0.1.1

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

Lorgar

Very simple module for translate your texts with custom params, cases and plurals.

Features

  • Simple, fast and small
  • Custom params for translate
  • Custom params for plural

Menu

Installing

Using npm:

npm install lorgar

Usage

Register new Localizator class

javascript:

import Localizator from 'lorgar';


const localizator = new Localizator({
  locales?: {
    [key: string]: {
      [key: string]: string;
    }
  },
  language?: string,
  fallbackLanguage?: string,
});

Change locale

You can set new locale or update existing locale. Just use the changeLocale method.

javascript:

/**
 * first argument - name of locale,
 * second argument - locale object
 */
localizator.changeLocale(string, {
  [key: string]: string
});

Delete locale

Also you can delete the locale by calling the deleteLocale method

/**
 * first argument - name of locale
 */
localizator.deleteLocale(string);

Change language

Call the changeLanguage methods if you want to change the language

/**
 * first argument - new language
 */
localizator.changeLanguage(string);

Get translate

locale:

{
  "some.key": "some translate"
}

javascript:

localizator.t('some.key'); // some translate

Get translate with wrong key

javascript:

localizator.t('wrong.key'); // wrong.key

Get translate with wrong key and fallback

javascript:

localizator.t('wrong.key', null, 'some fallback'); // some fallback

Get translate with params

Params in array:

locale:

{
  "some.key": "some $0"
}

javascript:

localizator.t('some.key', ['param']); // some param

Params in object

locale:

{
  "some.key": "some {test}"
}

javascript:

localizator.t('some.key', 'some fallback', { test: 'param' }); // some param

Get translate with plural form

locale:

{
  "plural": "$0 [subs,subscriber,subscribers,subscribers]"
}
[varName, firstForm, secondForm, thirdForm];

// varName - name of variable

// firstForm - singular form (one apple - en, одно яблоко - ru)

// secondForm - plural form, like two items (two apples - en, два яблока - ru)

// thirdForm - plural form, like five items (five apples - en, пять яблок - ru)

One item

javascript:

localizator.t('plural', 'some fallback', [1], { subs: 1 }); // 1 subscriber

Two items

localizator.t('plural', 'some fallback', [2], { subs: 2 }); // 2 subscribers

Five items

localizator.t('plural', 'some fallback', [5], { subs: 5 }); // 5 subscribers
0.1.1

5 years ago

0.1.0

5 years ago