把手 - Access Navigator

Handlebars - Access Navigator

我正在修改 Ghost 博客的部分内容以使用不同的语言。为此,我正在编写一个 Handlebars 助手:

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

但是,我收到一个错误消息 navigator is undefined。 Ghost 正在使用 express-hbs,所以我猜它与自定义风格有关,因为同一个助手 was declared here.

我在这里遗漏了什么明显的东西?

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

        helpers: {
            language: function () { return getLanguage(req); }
        }
    });
});

经过一个深夜的编码,我弄明白了这一点。

index.js中,在renderPost函数中,我可以(以一种非常原始的方式)查询客户端语言:

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

这将在 post 对象中创建一个新的 属性。借助一个custom conditional helper(放在helpers.js):

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

core\server\helpers\content.js 中,我已经实现了一个自定义语言解析器,它根据索引显示正确的内容。