I18next 最好在 shell 级别使用 shell 命名空间进行初始化,每个内部 spa 都会将其命名空间添加到共享实例中。
这样您就不会有重复的实例和代码。
您可以使用 [i18next.addResourceBundle][1] 来添加与当前内部应用相关的翻译资源。
i18next.addResourceBundle('en', 'app1/namespace-1', {
// ----------------------------------^ nested namespace allow you to group namespace by inner apps, and avoid namespace collisions
key: 'hello from namespace 1'
});
将 i18next 实例作为 props 传递给内部应用。
// root.application.js
import {i18n} from './i18n';
// ------^ shells i18next configured instance
singleSpa.registerApplication({
name: 'app1',
activeWhen,
app,
customProps: { i18n, lang: 'en' }
});
// app1.js
export function mount(props) {
const {i18n, lang} = props;
i18n.addResourceBundle(lang, 'app1/namespace-1', {
key: 'hello from namespace 1',
});
return reactLifecycles.mount(props);
}
希望这个想法很清楚:]
[1]:https://www.i18next.com/how-to/add-or-load-translations#add-after-init