An open API service indexing awesome lists of open source software.

https://github.com/alexcambose/polylang

Library for handling multi language support using JavaScript
https://github.com/alexcambose/polylang

city communication country dialect earth english human javascript javascript-library language locale localization oral pronunciation speech spoken-language timezone vocabulary word words

Last synced: 11 months ago
JSON representation

Library for handling multi language support using JavaScript

Awesome Lists containing this project

README

          

# polylang

[![Build Status](https://travis-ci.org/alexcambose/polylang.svg?branch=master)](https://travis-ci.org/alexcambose/polylang)

Library for handling multi language support using JavaScript

## Installation
Install using [npm](https://www.npmjs.com/package/polylang)
```
npm install --save polylang
```

## Usage
```javascript
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: '<> is not a valid email because <>',
}
});
polylang.add('ro', {
welcome: 'salutare',
title: 'Cineva ne urmareste in permanenta',
errors: {
invalid_email: '<> nu este un email valid deoarece <>',
}
});

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

* [Polylang](#Polylang)
* [new Polylang({defaultLang, accessDelimiter, startInterpolationDelimiter, endInterpolationDelimiter})](#new_Polylang_new)
* [.add(language, data)](#Polylang+add)
* [.remove(language)](#Polylang+remove)
* [.t(selector, interpolationValues)](#Polylang+t)
* [.translate(selector, language, interpolationValues)](#Polylang+translate)

### 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**
```js
polylang = new Polylang({
defaultLang: 'en'
accessDelimiter: '.',
startInterpolationDelimiter: '<<',
endInterpolationDelimiter: '>>'
});
```

### polylang.add(language, data)
Add a translation

**Kind**: instance method of [Polylang](#Polylang)

| Param | Type | Description |
| --- | --- | --- |
| language | string | Language |
| data | object | Translation object |

**Example**
```js
polylang.add('en',{
errors: {
invalid_email: 'The 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](#Polylang)

| Param | Type | Description |
| --- | --- | --- |
| language | string | Language name that will be removeed |

**Example**
```js
polylang.remove('en');
```

### polylang.t(selector, interpolationValues)
Translate

**Kind**: instance method of [Polylang](#Polylang)

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

**Example**
```js
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 "<>" is invalid because <>'
```

### polylang.translate(selector, language, interpolationValues)
Translate in a custom language

**Kind**: instance method of [Polylang](#Polylang)

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

**Example**
```js
polylang.t('errors.invalid_email', 'en', 'test@test.com');
```
## Test
```
npm test
```
## License
[MIT](https://github.com/alexcambose/polylang/blob/master/LICENSE)