Search code examples
javascriptnode.jsexpresshandlebars.jsghost-blog

Handlebars - Access Navigator


I am modifying parts of the Ghost blog to use different languages. For that, I am writing a Handlebars helper:

hbs.registerHelper("language", function () {
        var lang = (navigator.language) ? navigator.language : navigator.userLanguage;  
        return lang;
    });

However, I get an error where the message is navigator is undefined. Ghost is using express-hbs, so I am guessing it has something to do with the custom flavor, because the same helper was declared here.

What obvious thing am I missing here?


Solution

  • I figured this out after a late night of coding.

    In index.js, within the renderPost function, I can (in a very primitive way) query the client language:

    response.post.language = req.headers["accept-language"].substring(0,5).toLowerCase();

    This will create a new property within the post object. With the help of a custom conditional helper (placed in helpers.js):

    {{#ifCond language '==' 'en-us'}}
         {{content lang="1"}}
    {{else}}
         {{content lang="2"}}
    {{/ifCond}}
    

    In core\server\helpers\content.js I've already implemented a custom language parser that displays the right content depending on the index.