polylang

1.0.1 • Public • Published

polylang

Build Status

Small library for handling multi language support using JavaScript

Installation

npm install --save polylang

npm

Usage

const Polylang = require('polylang'); // or if you are using ES6 imports, import Polylang from 'polylang';
 
const polylang = new Polylang(); // create a new instance
polylang.add('en', {
    welcome: 'welcome',
    title: 'Someone is always watching us',
    errors: {
        invalid_email: '<<email>> is not a valid email because <<reason>>',
    }
});
polylang.add('ro', {
    welcome: 'salutare',
    title: 'Cineva ne urmareste in permanenta',
    errors: {
        invalid_email: '<<email>> nu este un email valid deoarece <<motiv>>',
    }
});
 
polylang.t('welcome'); // welcome
polylang.t('errors.invalid_email', { email: 'test@test.com', reason: 'it is too short!' }); // test@test.com is not a valid email because it is too short!
 
polylang.lang = 'ro';
 
polylang.t('welcome'); // salutare
polylang.t('errors.invalid_email', { email: 'test@test.com', motiv: 'este prea scurt!' }); // test@test.com nu este un email valid deoarece este prea scurt!

API

new Polylang({defaultLang, accessDelimiter, startInterpolationDelimiter, endInterpolationDelimiter})

Param Type Description
{defaultLang string the default language that will be used to translate the text
accessDelimiter string delimiter used to represent levels in the translation object (ex: 'errors.password.too_short')
startInterpolationDelimiter string delimiter used to interpolate custom string into the translation at the start of the word
endInterpolationDelimiter} string delimiter used to interpolate custom string into the translation at the start of the word

Example

polylang = new Polylang({
    defaultLang: 'en'
    accessDelimiter: '.',
    startInterpolationDelimiter: '<<',
    endInterpolationDelimiter: '>>'
});

polylang.add(language, data)

Add a translation

Kind: instance method of Polylang

Param Type Description
language string language
data object translation object

Example

polylang.add('en',{
            errors: {
                invalid_email: 'The email "<<email>>" is invalid',
                password: {
                    too_short: 'The password is too short',
                    one_digit: 'The passowrd must contain at least one digit'
                },
            }
        });

polylang.remove(language)

Remove a translation

Kind: instance method of Polylang

Param Type Description
language string language name that wil be removeed

Example

polylang.remove('en');

polylang.t(selector, interpolationValues)

Translate

Kind: instance method of Polylang

Param Type Description
selector string translation path
interpolationValues string | object value/s to be replaced between interpolation delimiters

Example

polylang.t('errors.invalid_email', 'test@test.com');
polylang.t('errors.invalid_email', {email: 'test@test.com', reason: 'it is taken!'}); //if invalid_email string would be 'The email "<<email>>" is invalid because <<reason>>'

polylang.translate(selector, language, interpolationValues)

Translate in custom language

Kind: instance method of Polylang

Param Type Description
selector string translation path
language string translation language
interpolationValues string | object value/s to be replaced between interpolation delimiters

Example

polylang.t('errors.invalid_email', 'en', 'test@test.com');

Package Sidebar

Install

npm i polylang

Weekly Downloads

5

Version

1.0.1

License

MIT

Unpacked Size

13.7 kB

Total Files

7

Last publish

Collaborators

  • alexcambose