Skip to content

🐡 Straightforward library for translations and dictionaries

License

Notifications You must be signed in to change notification settings

grvcoelho/babelfish

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

🐡 Straightforward library for translations and dictionaries

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published