简体   繁体   English

i18next detectLngFromPath无法正常工作

[英]i18next detectLngFromPath not working

i am trying to set up i18n on my project (using express.js), right now trying to make work i18next package, so that myproject.com/en/other/path/ shows to english and myproject.com/ee/other/path shows Latvian text. 我正在尝试在我的项目上设置i18n(使用express.js),现在正试图使i18next程序包正常工作,以便myproject.com/en/other/path/可以显示为英语,而myproject.com/ee/other/路径显示拉脱维亚文字。 But the module doesn't seem to detect language from path. 但是该模块似乎无法从路径中检测语言。 At first i thought it might find the langauge in the path but doesn't not automaticly set it, but debuging req, i18n returns 'en' on language, what am i doing wrong? 起初,我认为它可能会在路径中找到语言,但不会自动设置它,但是在调试req时,i18n在语言上返回“ en”,我在做什么错?

Here is the code of related to i18n: 以下是与i18n相关的代码:

setting-up i18next app.js: 设置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,
});

later in app.js reference the route file: 稍后在app.js中引用路由文件:

i18n.registerAppHelper(app);
app.use(i18n.handle);

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

/routes/index.js file: /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;

have a look at running sample (test): https://github.com/jamuhl/i18next-node/blob/master/test/i18next.detectLanguage.spec.js#L12 看看正在运行的示例(测试): https : //github.com/jamuhl/i18next-node/blob/master/test/i18next.detectLanguage.spec.js#L12

-> index starting from 0 - try setting detectLngFromPath=0 ->从0开始的索引-尝试设置detectLngFromPath=0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM