lang-map-manager
v1.0.4
Published
Translates multiple languages using longest prefix match algorithm
Downloads
2
Readme
Language
Translates multiple languages using longest prefix match algorithm
Installation
npm install lang-map-manager@latest
Usage
Package import
Language setting is not required for single language pair
// With settings
var Language = require('lang-map-manager')('en', 'id');
or
// Without settings
var Language = require('lang-map-manager')();
Setting the language
If you want to use multiple translation language change the language setting using the code below
var Language = require('lang-map-manager')();
// Set the language for the base
Language.setLanguage('en');
Language.setTargetLanguage('id');
Set and translate
Set the translate using Language.setTranslation(phrase, translation)
Translate using Language.translate(text)
Language.setTranslation('hi from there', 'halo dari sana');
Language.setTranslation('hi', 'halo');
// returns 'halo from here'
console.log(Language.translate('hi from here'));
or Manage Translating Language Pairs directly:
Language.setTranslation('hi from there', 'halo dari sana', 'en', 'id');
Language.setTranslation('hi', 'halo', 'en', 'id');
// returns 'halo from here'
console.log(Language.translate('hi from here', 'en', 'id'));
Check the empty translation
check the empty translations after translating contents (returns array of unset translations)
console.log(Language.getEmptyTranslations());
// outputs ['from', 'here', 'hi from here']
Complete Example Code:
var Language = require('lang-map-manager')();
// this is not required for single languages
Language.setLanguage('id');
Language.setTargetLanguage('en');
Language.setTranslation('hi from there', 'halo dari sana');
Language.setTranslation('hi', 'halo');
let translation = Language.translate('hi from here');
console.log(translation);
// outputs 'halo from here' (the hi word has already defined a meaning for it self)
console.log(Language.getEmptyTranslations());
// outputs ['from', 'here', 'hi from here']