0

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?

Den
  • 16,686
  • 4
  • 47
  • 87
  • I think your code is running at server.navigator is global in browser. – gu mingfeng May 26 '16 at 04:19
  • @gujefers so what would be the proper way to tweak the code above to reference the right instance? – Den May 26 '16 at 04:22
  • Render it at browser,or get the language from Session Object(maybe Request) – gu mingfeng May 26 '16 at 04:34
  • @gujefers To clarify, the purpose of this is to pass it into a view, where I can get the language before it renders the content in the browser. So ultimately I would want to get it in the helper. Any thoughts on doing that? – Den May 26 '16 at 04:39
  • 1
    Be careful with this approach - I definitely wouldn't recommend it as a general approach to handling multiple languages. You remove the ability to have a caching layer in front of your blog (the cache will store whatever the language the first visitor to a page requests) and unless you are redirecting to a language-specific page you are also opening yourself to google penalties. Google has some details on how to properly handle multi-language sites - https://support.google.com/webmasters/answer/182192?hl=en – Kevin Ansfield May 30 '16 at 09:05

2 Answers2

1
function getLanguage(req){
    ....
}
app.get('/', function (req, res, next) {
    res.render('home', {
        showTitle: true,

        helpers: {
            language: function () { return getLanguage(req); }
        }
    });
});
gu mingfeng
  • 1,010
  • 8
  • 10
0

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.

Community
  • 1
  • 1
Den
  • 16,686
  • 4
  • 47
  • 87