i18下一个detectLngFromPath不工作

i18next detectLngFromPath not working

本文关键字:工作 detectLngFromPath 下一个 i18      更新时间:2023-09-26

我正试图在我的项目上设置i18n(使用express.js),现在正试图使工作成为i18next包,以便myproject.com/en/other/path/显示为英语,myproject.com/ee/other/path显示拉脱维亚文本。但该模块似乎并没有从路径中检测到语言。起初,我认为它可能会在路径中找到langauge,但不会自动设置它,但在调试req时,i18n在语言上返回'en',我做错了什么?

这是与i18n相关的代码:

设置i18next app.js:

var i18n = require('i18next');
i18n.init({
  ignoreRoutes: ['images/', 'public/', 'css/', 'js/'],
  supportedLngs: ['en', 'ee'],
  fallbackLng: 'en',
  //detectLngQS: 'lang', // ?lang=ee
  detectLngFromPath: 1,
  forceDetectLngFromPath: true,
  detectLngFromHeaders: false,
  useCookie: false,
  //cookieName: 'interspace-lang-cookie', // default 'i18next'
  debug: true,
});

稍后在app.js中引用路由文件:

i18n.registerAppHelper(app);
app.use(i18n.handle);
var routes = require('./routes/index');
app.use('/', routes);

/routes/index.js文件:

var express = require('express');
var router = express.Router();
router.get('/:lang', function(req, res) {
  res.render('index', { title: 'Hello' });
});
module.exports = router;

查看运行示例(测试):https://github.com/jamuhl/i18next-node/blob/master/test/i18next.detectLanguage.spec.js#L12

->索引从0开始-尝试设置detectLngFromPath=0