https://github.com/grvcoelho/babelfish
:blowfish: Straightforward library for translations and dictionaries
https://github.com/grvcoelho/babelfish
i18n javascript lib
Last synced: 2 months ago
JSON representation
:blowfish: Straightforward library for translations and dictionaries
- Host: GitHub
- URL: https://github.com/grvcoelho/babelfish
- Owner: grvcoelho
- License: mit
- Created: 2016-01-22T01:00:11.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-09-17T13:34:58.000Z (over 8 years ago)
- Last Synced: 2025-03-29T03:32:06.397Z (3 months ago)
- Topics: i18n, javascript, lib
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 47
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# babelfish
[](https://travis-ci.org/grvcoelho/babelfish)
[](https://coveralls.io/github/grvcoelho/babelfish)Straightforward library for translations and i18n
## Install
You can get it on npm.
```
npm install @grvcoelho/babelfish --save
```If you're not into package management, just [download a ZIP](https://github.com/grvcoelho/babelfish/archive/master.zip) file.
## Setup
First, you need to import it or require it:
```javascript
import Babelfish from 'babelfish';
```Then, you need to instantiate it and passing a dictionary object optionally:
```javascript
let babelfish = new Babelfish({
pt: {
'APPLE': 'maçã'
},
en: {
'APPLE': 'apple'
}
});
```## Usage
Adding dictionaries:
```javascript
babelfish.addDictionary('fr', {
'APPLE': 'pomme'
});
```Using dictionaries and translating:
```javascript
babelfish.useDictionary('pt');
babelfish.translate('APPLE'); // "maçã"
```You can also use functions as the translations:
```javascript
babelfish.addDictionary('fr', {
'APPLE': function() {
return 'pomme' + '!';
}
});babelfish.useDictionary('fr');
babelfish.translate('APPLE'); // "pomme!"
```You can handle pluralisation by passing an array as the translations, and passing a second argument to the `.translate()` method.
```javascript
babelfish.addDictionary('pt', {
'APPLE': [
'Sem maçãs',
'1 maçã',
'%s maçãs'
]
});babelfish.useDictionary('pt');
babelfish.translate('APPLE'); // if no count is passed, 1 is the default. "1 maçã"
babelfish.translate('APPLE', 0); // "Sem maçãs"
babelfish.translate('APPLE', 1); // "1 maçã"
babelfish.translate('APPLE', 2); // "2 maçãs"
babelfish.translate('APPLE', 17); // "17 maçãs"
```## License
[MIT](https://github.com/grvcoelho/babelfish/blob/master/LICENSE) © 2016