[英]Language getting detected but translation not working with i18n
我在我的 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))
這是翻譯測試文件:
// 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 :(
}
英文title的值是title ,馬來文是tajuk
根據express 中間件文檔,我將my作為接受語言 header 傳遞,並且console.log(req.i18n.language)
正在正確打印它。
但是, console.log(t('title'))
仍在打印title而不是tajuk
這看起來很瘋狂,但這解決了問題:
const i18n = req.i18n;
console.log(i18n.t('title'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.