[英]Language getting detected but translation not working with i18n
I have set up the i18n middleware in my Express Node js server like this:我在我的 Express Node js 服务器中设置了i18n中间件,如下所示:
// server.js
import i18nMiddleware from 'i18next-express-middleware';
import i18n from 'i18next';
import Backend from 'i18next-node-fs-backend';
import { LanguageDetector } from 'i18next-express-middleware';
i18n
.use(Backend)
.use(LanguageDetector)
.init({
whitelist: ['en', 'my'],
fallbackLng: 'en',
// have a common namespace used around the full app
ns: ['common'],
defaultNS: 'common',
debug: false,
backend: {
loadPath: './locales/{{lng}}/{{ns}}.json',
// jsonIndent: 2
}
});
app.use(i18nMiddleware.handle(i18n))
Here is the translation test file:这是翻译测试文件:
// test.js
import i18next from "i18next";
const test = (req, res) =>{
const t = req.i18n.t.bind(i18next);
console.log(req.i18n.language) // language set correctly :)
console.log(t('title')) // translation not working :(
}
The value of title in English is title and for Malaysian, it's tajuk英文title的值是title ,马来文是tajuk
As per the express middleware documentation , I'm passing my as the accept-language header, and console.log(req.i18n.language)
is correctly printing it.根据express 中间件文档,我将my作为接受语言 header 传递,并且
console.log(req.i18n.language)
正在正确打印它。
However, console.log(t('title'))
is still printing title instead of tajuk但是,
console.log(t('title'))
仍在打印title而不是tajuk
This looks crazy but this solved the problem:这看起来很疯狂,但这解决了问题:
const i18n = req.i18n;
console.log(i18n.t('title'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.