manual_localization v1.3.3
manual_localization
Simple Implementation Used to Localize and load different resource files for the web page , it works only with ejs templates
Installing
npm install manual_localization --save
Usage
First create a Resources Dierctory in Your project root file and rename it to LangaugesResources
then create your resource holder Json file that contains the translation material its , naming convention should be res-PageName.json (i.e res-Home.Json ) for translation the Home page in your web Application .
the body of this file (Json) should be like that :
{
"Image": {
"en": "flag-icon flag-icon-um",
"ar": "flag-icon flag-icon-eg"
},
"lang": {
"en": "English",
"ar": "العربية"
}
}
- require manual_localization in your app.js file
var mlocalization = require('manual_localization');
- In Your Exprees Request body instantiate the manual_localization and create a promise event handler to wait for the loading of the resource file.
//....
app.get("/ResorceTest",(req,res) =>{
//can be parsed from the querystring or url paramter
var Requestd_Langauge = "ar";
var RequestedPage = "Home" ;
var LoadedResources = mlocalization.LoadResources(Requestd_Langauge , RequestedPage);
LoadedResources.then((result) => {
// "/Pages/ResorceTest" -- the path to your EJS views .
res.render(__dirname + "/Pages/ResorceTest",result);
});
})
The result object in the pervious code will be in this format , so make sure that you map your objects name correctly to the right proberty.
{
"Image" : "flag-icon flag-icon-eg" ,
"lang" : "العربية"
}
- in your ejs tempalte file head the HTML page with this function :
<%
Translate = function(value) {
if(typeof value != 'undefined'){%><%=value%><%}else{%> return "value";<%}
}
%>
You can instead use <%- include functions/translate.ejs %> for more organized code
- finally in the HTML Markup just call the function :
<li class="active">
<a href="#"><%= Translate(Home) %></a>
</li>
Errors
- Error 102 : The resource file.json may contains words as empty string .
- Error 101 : Resource file couldn't be reloaded this may happened beacuase an error in the .json file , or you miscreating the LangaugeResource Dierctory .
Dependancies
jsonfile - Dependancy
Authors
- Alameer Ashraf - Solution Developer - Alameer Ashraf
License
This project is licensed under the ISC License - see the LICENSE.md file for details