【问题标题】:i18next detectLngFromPath not workingi18next detectLngFromPath 不工作
【发布时间】:2014-04-23 12:20:02
【问题描述】:

我正在尝试在我的项目上设置 i18n(使用 express.js),现在正在尝试使 i18next 包工作,以便 myproject.com/en/other/path/ 显示为英语和 myproject.com /ee/other/path 显示拉脱维亚文本。 但是该模块似乎没有从路径中检测到语言。起初我以为它可能会在路径中找到语言但不会自动设置它,但是调试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;

【问题讨论】:

    标签: javascript i18next


    【解决方案1】:

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

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

    【讨论】:

    • 有点好笑,但发现问题是使用 'ee' 来识别 estonia,而不是 'et',现在一切似乎都可以正常工作,但通过测试它有助于找到问题。跨度>
    猜你喜欢
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2020-06-05
    • 1970-01-01
    • 2017-10-25
    相关资源
    最近更新 更多