Search code examples
node.jsexpresshandlebars.jskeystonejs

How to Use the values of one local variable in replacement of other in HandleBarjs


I am facing and issue with the handle bars.

I have created two local variable with the name of the language and which corresponds the path of two Json files.

app.locals.en=require('../locales/en.json');
app.locals.de=require('../locales/de.json');

I am detecting the de or en from the URL and storing into another local variable call "Lang"

app.locals.lang = "Some code that detects DE or EN from URL and Stores into 
lang" 

Now in the Handlebrsjs I am using something like

 <h1>{{lang.Home.TITLE}}</h1>

The Lang above should give De or EN which it is giving but the it does not goes to the Json file but if we simply use

<h1>{{en.Home.TITLE}}</h1>

It works fine. I want to use lang.home.title instead of en.home.title becs the lang also have the same value that is en. But it does not work with lang.home.title

There must be some way to solve the problem. Please tell me. Thank you.


Solution

  • Try this:

    app.locals.data = {};
    
    app.locals.data.en=require('../locales/en.json');
    app.locals.data.de=require('../locales/de.json');
    
    app.locals.data.lang = "Some code that detects DE or EN from URL and Stores into 
    

    lang"

    You should then be able to do the following in your template:

     <h1>{{data[lang].Home.TITLE}}</h1>
    

    I hope this helps.