【发布时间】:2017-08-09 02:07:29
【问题描述】:
每个人, 我在获取本地参数然后在路由中传递它时遇到问题。 我在 routes/middleware.js 中有这段代码
exports.detectLang = function(req, res, next) {
var match = req.url.match(/^\/(de|en)([\/\?].*)?$/i);
if (match) {
req.setLocale(match[1]);
// Make locale available in template
// (necessary until i18n 0.6.x)
res.locals.locale = req.getLocale();
// reset the URL for routing
req.url = match[2] || '/';
console.log(response);
} else {
// Here you can redirect to default locale if you want
console.log("does not match")
}
next();
};
这会检测 url 中的 de 或 en 部分,并使用 de 或 en 设置局部变量“Locale”
现在在 routes/view/index.js 中
我想获取本地参数“Locale”并将其放入路由中 像这样的
// Setup Route Bindings
exports = module.exports = function (app) {
// Views
app.get('/'+locale, routes.views.index);
app.all('/'+locale+'employees', routes.views.employees);
};
但我无法从中间件获取该语言环境参数到 index.js
【问题讨论】:
-
为什么不使用 cookie?
标签: node.js keystonejs