[英]react-i18next, add resources from props
我有组件回购和主要应用程序回购。 我在回购中使用组件实现了i18next,当我在此回购中有一个i18n配置文件时(或当我通过应用回购中的道具传递它时),它可以正常工作。 但是当我尝试仅从主应用程序发送“资源”部分并将其替换为组件中的配置文件时,我遇到了问题。 我试图克隆i18n实例并设置资源,但是它不起作用。 这是我的配置文件:i18n.js
import i18n from 'i18next';
import LngDetector from 'i18next-browser-languagedetector';
import { reactI18nextModule } from 'react-i18next';
i18n
.use(LngDetector)
.use(reactI18nextModule)
.init({
detection: {
order: ['cookie', 'localStorage'],
lookupLocalStorage: 'i18n_lang',
lookupCookie: 'i18n_lang',
caches: ['localStorage'],
},
load: 'current',
fallbackLng: 'en',
ns: ['components'],
defaultNS: 'components',
keySeparator: false,
interpolation: {
escapeValue: false,
formatSeparator: ',',
},
react: {
wait: true,
},
});
export default i18n;
resources.js文件(我一开始尝试使用资源键,但仍然无法使用):
import * as en from './en.json';
import * as de from './de.json';
export default {
en: {
components: en,
},
de: {
components: de,
},
};
现在我尝试了这样的事情:
import * as langs from './resources';
const newI18 = i18n.cloneInstance({ resources: langs });
const i18ProviderDecorator = (storyFn) => (
<I18nextProvider i18n={newI18}>
{ storyFn() }
</I18nextProvider>
当我通过带有资源的道具传递i18n.js时,它可以完美工作,但是我想从主应用程序中删除i18next并将其仅保留在组件中。 问候
一个i18next克隆实例使用与原始实例相同的存储->并且不再再次初始化->因此以这种方式传递资源不起作用: https : //github.com/i18next/i18next/blob/master/src/ i18next.js#L308
制作一个新实例i18n.createInstance或使用i18n.addResourceBundle传递资源以进行克隆: https ://www.i18next.com/api.html#addresourcebundle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.