我想让我的i18n延迟加载翻译文件,从文档中,似乎很简单--添加.use(backend)
和import backend from "i18next-http-backend"
。
唯一的问题是,我在我的提供中使用的i18n实例已经在我的组织的内部回购中定义为一个UI库(我必须使用它),它只公开了一种为您初始化i18n实例的方法--这没有为.use(backend)
做任何准备,现在我不得不将它添加到代码中。
这是图书馆代码-
...
export const getDefaultI18nextInstance = (resources) => {
i18n
.use(AlphaLanguageDetector)
.use(initReactI18next)
.init({
fallbackLng: 'en',
// have a common namespace used around the full app
ns: [
'translations',
'common'
],
nsMode: 'fallback',
defaultNS: 'translations',
// debug: (process.env.NODE_ENV && process.env.NODE_ENV === 'production') ? false : true,
debug: true,
interpolation: {
escapeValue: false, // not needed for react!!
},
resources: resources || null,
react: {
wait: true
}
});
Object.keys(translations).forEach(lang => {
Object.keys(translations[lang]).forEach(namespace => {
i18n.addResourceBundle(lang, namespace, translations[lang][namespace], true, true)
})
});
return i18n;
}
export default {
getDefaultI18nextInstance,
translations,
t
};
...
我尝试在我的<I18nextProvider i18n={i18n.getDefaultI18nextInstance().use(backend)}>
文件中使用它,类似于这个index.js,但是我得到了错误
i18next::backendConnector:没有通过i18next.use添加后端。不会加载资源。
我在projectRoot/locales/{lang}/translation.json
有我的地点。
发布于 2021-01-12 11:08:27
您可以使用i18n.cloneInstance(options, callback)
并将后端配置传递为options
,它将合并您的ui库中的所有选项,并返回一个能够获取的i18next新实例。
import HttpApi from 'i18next-http-backend';
const original = getDefaultI18nextInstance({});
export const i18n = original
.cloneInstance({
backend: {
loadPath: '/locales/{{lng}}/{{ns}}.json',
},
})
.use(HttpApi);
https://stackoverflow.com/questions/65674006
复制相似问题