我正在开发一个多语言网站,nextjs
用于SSR,i18next
用于制作多语言机制。
由于使用了下一个SSR,我无法使用i18next-xhr-backend
,我应该使用i18next-node-fs-backend
但在这种情况下,我为fs
找出此错误:
Module not found: Can't resolve 'fs' in '/Users/macbook/Desktop/project/node_modules/i18next-node-fs-backend/lib'
有没有通用的方法可以获取i18next
语言 json 文件?
燃烧了一天后,我找到了如何做到这一点。我使用 i18next-fetch-后端(实际上在这个库中几乎没有编辑,我将发送一个 PR(,在我的 i18n.js
componenet 中,添加isomorphic-fetch
作为配置 obj 中的常规获取 API。
import i18n from 'i18next'
import Backend from 'i18next-fetch-backend'
import { initReactI18next } from 'react-i18next'
import fetch from 'isomorphic-fetch'
i18n
.use(Backend)
.use(initReactI18next)
.init({
lng: 'fa',
backend: {
/* translation file path */
loadPath: '/static/i18n/{{ns}}/{{lng}}.json',
fetch
},
fallbackLng: 'fa',
debug: true,
/* can have multiple namespace, in case you want to divide a huge translation into smaller pieces and load them on demand */
ns: ['translations'],
defaultNS: 'translations',
keySeparator: false,
interpolation: {
escapeValue: false,
formatSeparator: ','
},
react: {
wait: true
}
})
export default i18n
对于i18next-fetch-backend
库,我在文件的第 181 行上为默认配置添加了以下条件i18next-fetch-backend.cjs.js
:
fetch: typeof fetch === "function" ? fetch : (() => {}),