Skip to content

hkaraoglu/language-translator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

LanguageCodeLanguageCode
AzerbaijanazMalayalamml
AlbaniansqMaltesemt
AmharicamMacedonianmk
EnglishenMaorimi
ArabicarMarathimr
ArmenianhyMarimhr
AfrikaansafMongolianmn
BasqueeuGermande
BashkirbaNepaline
BelarusianbeNorwegianno
BengalibnPunjabipa
BurmesemyPapiamentopap
BulgarianbgPersianfa
BosnianbsPolishpl
WelshcyPortuguesept
HungarianhuRomanianro
VietnameseviRussianru
Haitian (Creole)htCebuanoceb
GalicianglSerbiansr
DutchnlSinhalasi
Hill MarimrjSlovakiansk
GreekelSloveniansl
GeorgiankaSwahilisw
GujaratiguSundanesesu
DanishdaTajiktg
HebrewheThaith
YiddishyiTagalogtl
IndonesianidTamilta
IrishgaTatartt
ItalianitTelugute
IcelandicisTurkishtr
SpanishesUdmurtudm
KazakhkkUzbekuz
KannadaknUkrainianuk
CatalancaUrduur
KyrgyzkyFinnishfi
ChinesezhFrenchfr
KoreankoHindihi
XhosaxhCroatianhr
KhmerkmCzechcs
LaotianloSwedishsv
LatinlaScottishgd
LatvianlvEstonianet
LithuanianltEsperantoeo
LuxembourgishlbJavanesejv
MalagasymgJapaneseja
Malayms

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