Giter Site home page Giter Site logo

alexcambose / polylang Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 1.0 36 KB

Library for handling multi language support using JavaScript

License: MIT License

JavaScript 100.00%
javascript javascript-library language locale earth localization timezone communication human vocabulary

polylang's Introduction

polylang

Build Status

Library for handling multi language support using JavaScript

Installation

Install using npm

npm install --save polylang

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: '[email protected]', 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: '[email protected]', 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 will 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', '[email protected]');
polylang.t('errors.invalid_email', {email: '[email protected]', reason: 'it is taken!'}); //if invalid_email string would be 'The email "<<email>>" is invalid because <<reason>>'

polylang.translate(selector, language, interpolationValues)

Translate in a 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', '[email protected]');

Test

npm test

License

MIT

polylang's People

Contributors

alexcambose avatar anthonysea avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

anthonysea

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.