fast-translation
A performant module for handling translations.
Installation
npm install fast-translation
Example
const Translator = ; const translator = ;translator;translator;await translator;translator; // 1 birdtranslator; // 2 birdstranslator; // 1 bird
Interpolation
Interpolation allows to insert values into the translations:
translator;await translator;translator // a blue bird
Keys
There are three possible types of keys:
- Keys representing the singular:
key
- Keys representing first plural:
key_plural
- Keys representing any plural (using the plurals specified with
addRule
):key_plural_3
API
Translator(options)
options
preloadLanguages (optional)
Array of languages to be loaded with sources.
preloadNamespaces (optional)
Array of namespaces to be loaded with sources.
reloadInterval (optional)
A number
in milliseconds that defines the interval to reload the translations.
logger (optional)
If set it will log errors during reload using the provided logger
. Needs at least an error()
function.
Translator#use(source)
Adds a source for translations. There are two kind of sources being provided:
const FileSource HttpSource = ;const httpSource = url: `http://localhost:/locales/__language__/__namespace__`;const fileSource = filePath: __dirname + `/locales/__language__-__namespace__.json`;translator;translator;
Translator#initialize()
Initializes the translator. Returns a Promise.
Translator#translate(key, options)
Translates the provided key
using the provided options
. All placeholders
will be replaced by the values provided with the options.
translator
The process of finding a translation works as follows (assuming the key is known):
- Determine the
count
: Eitheroptions.count
or1
. - Determine the
plural
: Call therule
function for thelanguage
with thecount
. - Determine the
namespace
: Eitheroptions.namespace
ordefault
. - Build an identifier from the
key
,language
,plural
andnamespace
. - Use the identifier to find a translation function, execute it with the
options
and return the result.
Note: If translate()
is being called with an unknown key the key will be
returned.
options#language
The language
that will be used to translate the key
will be translated to.
options#namespace (optional)
The namespace
that will be used to translate the key
. Defaults to default
.
options#count (optional)
A number
used to determine the plural for the key
. Defaults to 1
.
Translator#addRule(language, plurals, rule)
Adds a plural rule
function for a language
along with an array of plurals
that will be used to determine the correct plural.
translator
Translator#addTranslations(translations, language, [namespace])
If no namespace is being passed it defaults to default
. The translations may
contain placeholders like __placeholder__
.
For each key a function is being created and stored internally, that handles translation and interpolation for that key.
Translator#reload([handler])
Reloads the translations using the sources. Allows to pass a handler
function
that will be called if an error occurs.
Benchmark
> node benchmark/benchmark.js i18next x 225,728 ops/sec ±1.10% fast-translation x 5,130,229 ops/sec ±0.59% Fastest is fast-translation