Skip to content

Commit c0c92e3

Browse files
authored
try to address #2270 by cloning the store data (#2271)
* try to address #2270 by cloning the store data * cosmetics
1 parent 182abcc commit c0c92e3

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/i18next.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,15 @@ class I18n extends EventEmitter {
604604
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
605605
};
606606
if (forkResourceStore) {
607-
clone.store = new ResourceStore(this.store.data, mergedOptions);
607+
// faster than const clonedData = JSON.parse(JSON.stringify(this.store.data))
608+
const clonedData = Object.keys(this.store.data).reduce((prev, l) => {
609+
prev[l] = { ...this.store.data[l] };
610+
return Object.keys(prev[l]).reduce((acc, n) => {
611+
acc[n] = { ...prev[l][n] };
612+
return acc;
613+
}, {});
614+
}, {});
615+
clone.store = new ResourceStore(clonedData, mergedOptions);
608616
clone.services.resourceStore = clone.store;
609617
}
610618
clone.translator = new Translator(clone.services, mergedOptions);

test/runtime/i18next.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,16 @@ describe('i18next', () => {
9999
},
100100
});
101101
newInstance = orgInstance.cloneInstance({ forkResourceStore: true, keySeparator: '__' });
102+
newInstance.addResourceBundle('en', 'translation', {
103+
deeper: { key: 'value here cloned' },
104+
});
102105
});
103106

104107
it('it not has shared instance of resource store', () => {
105108
expect(newInstance.store).not.to.equal(orgInstance.store);
106109
expect(orgInstance.t('deeper.key')).to.equal('value here');
107110
expect(orgInstance.t('deeper.key')).not.to.equal(newInstance.t('deeper.key'));
108-
expect(newInstance.t('deeper__key')).to.equal('value here');
109-
expect(orgInstance.t('deeper.key')).to.equal(newInstance.t('deeper__key'));
111+
expect(newInstance.t('deeper__key')).to.equal('value here cloned');
110112
});
111113
});
112114
});

0 commit comments

Comments
 (0)