Giter VIP home page Giter VIP logo

language-translator's Introduction

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

language-translator's People

Contributors

hkaraoglu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

language-translator's Issues

Translate default language's files' texts by Yandex API

By using Yandex Translate API, the default language json files' texts can be translated to other languages.

Example:
Default language : en
langs : ["en", "tr", "fr", "es", "tr"]
When We start node application, library converts all default language's json files' texts to other all langs's language by getting translation from Yandex Translate API.

Not able to use in js file

It's working fine in the ejs file , thank you it suits my use case . But only thing is I am not able to use it in .js . It shows res is undefined.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.