node.js - Express 和 i18n 默认区域设置不起作用

标签 node.js express i18n-node

我正在尝试设置 i18n-node 和 Expressjs。我的配置是这样的:

//i18n 配置 =============================================== =============

var i18nconf = {
   locales        : ["en", "es"],
   cookie         : "locale",
   defaultLocale : "en",
   directory      : path.join(__dirname, "lang"),
   indent : "  "
};

i18n.configure(i18nconf);
app.use(i18n.init);

lang/文件夹 en.json 和 es.json 上有 2 个语言环境,我可以在它们之间切换,没有问题,但 Express 总是默认加载 es.json,不知何故忽略了conf中的 defaultLocale: 'en' 。

有什么想法吗?

提前致谢!

最佳答案

这个库的运行对我来说有点有趣。我将此中间件添加到我的 Express 服务器配置中,i18n 开始按其应有的方式运行。

app.use(function (req, res, next) {
    var locale = 'en'
    req.setLocale(locale)
    res.locals.language = locale
    next()
})

关于node.js - Express 和 i18n 默认区域设置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28479990/

相关文章:

javascript - Meteor JS - 动态扫描文件夹并添加到集合中

javascript - 编写 Google Cloud Functions 时是否应该在 'finally' block 中使用 response.send() ?

node.js - 在node.js中添加值

javascript - Spotify 白名单 URI 仍然返回 { "error": "invalid_grant", "error_description": "Invalid redirect URI"}

node.js i18n - 无法读取未定义的属性 'toLowerCase'

html - node.js:有没有办法将 HTML 放入 i18n-node JSON 翻译文件中?

node.js - SQL 'UNION ALL' 类似 MongoDB 中的实现

javascript - Ajax 在发送时从服务器读取响应

express - 如何使用 phantom-html2pdf 在 PDF 页面中添加页眉和页脚

node.js - 使用 Mongo Schema 和 i18n 的多语言验证消息