1.1.0 • Published 6 years ago

@grvcoelho/babelfish v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

babelfish

Build Status Coverage Status

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 file.

Setup

First, you need to import it or require it:

import Babelfish from 'babelfish';

Then, you need to instantiate it and passing a dictionary object optionally:

let babelfish = new Babelfish({
	pt: {
		'APPLE': 'maçã'
	},
	en: {
		'APPLE': 'apple'
	}
});

Usage

Adding dictionaries:

babelfish.addDictionary('fr', {
	'APPLE': 'pomme'
});

Using dictionaries and translating:

babelfish.useDictionary('pt');
babelfish.translate('APPLE'); // "maçã"

You can also use functions as the translations:

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.

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 © 2016

1.1.0

6 years ago