language-translator

Language localization and text translation module for NodeJS Express framework


Keywords
multilanguage, translator, language, multi, localization, text, string, express-js, expressjs, language-translator, localisation, nodejs, translator-text-api
License
MIT
Install
npm install language-translator@1.2.6

Documentation

language-translator

Text localization and translation module for NodeJS Express Framework.

Install

npm install language-translator --save

Load

var  languageTranslator = require('language-translator');
app.use(languageTranslator.init(
  {
    langs          : ["tr", "en", "fr", "de", "es"], // ... And other languages
    defaultLang    : "en",
    cookieName     : "lang",
    translate      : "true",
    translationApiKey: "{{your_yandex_translate_api_key}}"
}));

Configuration Parameters

langs

The module creates folders which takes an json array from configuration json. The array is required.

defaultLang

The module choose a language to choose a language folder as a default when cookie language is not be set. The defaultLang is required.

langDir

The module creates a root folder which takes its name from configuration json. The langDir is optional. Default value is language.

cookieName

The module creates a cookie to know user's language preference which requested before. Default cookie name is language.

equalizeKeys

The module offer an option to equalize keys of default language's json files with other languages. For example, you created a json file in default language folder but not in other language folders. When you restart node server, module creates all files and keys in other language folders for you. Default value is true. (Recommended)

translate

The module offer translate default language files to another language by using Yandex Translator API. Default value is true. (Recommended)

translationApiKey :

If you want translate all texts in default language folder's json files, you have to create API key in Yandex Developers Page. (https://translate.yandex.com/developers/keys) . It's free. This parameter is required if parameter translate is true.

Notice: If you use translation, you have to define items in langs array like below:

Supported Language List:

Language Code Language Code
Azerbaijan az Malayalam ml
Albanian sq Maltese mt
Amharic am Macedonian mk
English en Maori mi
Arabic ar Marathi mr
Armenian hy Mari mhr
Afrikaans af Mongolian mn
Basque eu German de
Bashkir ba Nepali ne
Belarusian be Norwegian no
Bengali bn Punjabi pa
Burmese my Papiamento pap
Bulgarian bg Persian fa
Bosnian bs Polish pl
Welsh cy Portuguese pt
Hungarian hu Romanian ro
Vietnamese vi Russian ru
Haitian (Creole) ht Cebuano ceb
Galician gl Serbian sr
Dutch nl Sinhala si
Hill Mari mrj Slovakian sk
Greek el Slovenian sl
Georgian ka Swahili sw
Gujarati gu Sundanese su
Danish da Tajik tg
Hebrew he Thai th
Yiddish yi Tagalog tl
Indonesian id Tamil ta
Irish ga Tatar tt
Italian it Telugu te
Icelandic is Turkish tr
Spanish es Udmurt udm
Kazakh kk Uzbek uz
Kannada kn Ukrainian uk
Catalan ca Urdu ur
Kyrgyz ky Finnish fi
Chinese zh French fr
Korean ko Hindi hi
Xhosa xh Croatian hr
Khmer km Czech cs
Laotian lo Swedish sv
Latin la Scottish gd
Latvian lv Estonian et
Lithuanian lt Esperanto eo
Luxembourgish lb Javanese jv
Malagasy mg Japanese ja
Malay ms

Usage

When your app started, firstly the module creates language folder in root directory. Then, it creates folders which names are in langs array. Then, it creates a json file which name is same as its folder name for common texts. And it creates a mapping.json file which is used to match route paths and language files.

For example:

langs : ["tr", "en"]

 your_app_folder
     -> language
	      -> tr
	        -> tr.json // It creates for common texts
		-> And your other language files...
	      -> en
		-> en.json // It creates for common texts
		-> And your other language files...

-- en.json file content:

{
	"example"           : "Example Text",
	"example_with_param": "Example %s"
}

On view file usage (Example for .ejs):

<%=  _lt.get('example')  %>

Formatted:

<%=  _lt.get(example_with_param, "Param value")  %>

--

On js file usage

 var _lt = res.locals._lt;
 _lt.load("home");
 console.log(_lt.get("example_with_param", "Param value"));

Example:

Your Application directory:

 your_app_folder
     -> language
	      -> tr
	        -> tr.json // It is used for common texts
		-> home.json
		-> about.json
		-> users.json
	      -> en
		-> en.json // It is used for common texts
		-> home.json
		-> about.json
		-> users.json   

Please STAR and WATCH the project! :)

References: “Medium.com - Nodejs text localization module” https://medium.com/@hkaraoglutr/nodejs-text-localization-module-687d3d285c3a