Skip to content

Commit

Permalink
Merge pull request #18 from offirgolan/clear-cache
Browse files Browse the repository at this point in the history
feat: Bind i18n and resource store events to clear the memoized cache
  • Loading branch information
jamuhl authored Mar 20, 2020
2 parents e330144 + 8dbd59b commit 7f711b3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ i18next
memoizeFallback: false,

// array or single object of loaded 'i18next-icu/locale-data
localeData: null
localeData: null,

// which events should clear the cache, can be set to false or string of events separated by " "
bindI18n: '',

// which events on resourceSource should clear the cache, can be set to false or string of events separated by " "
bindI18nStore: ''
}
```

Expand Down
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ declare module "i18next-icu" {
memoizeFallback?: boolean;
localeData?: LocaleData | LocaleData[];
formats?: IcuFormats;
bindI18n?: string;
bindI18nStore?: string;
}

export interface IcuInstance extends ThirdPartyModule {
init(i18next: i18n, options?: IcuConfig): void;
addLocaleData(localeData: LocaleData | LocaleData[]): void;
addUserDefinedFormats(formats: IcuFormats): void;
clearCache(): void;
}

interface IcuConstructor {
Expand Down
20 changes: 19 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import IntlMessageFormat from 'intl-messageformat';
function getDefaults() {
return {
memoize: true,
memoizeFallback: false
memoizeFallback: false,
bindI18n: '',
bindI18nStore: '',
};
}

Expand All @@ -22,8 +24,20 @@ class ICU {
this.formats = this.options.formats;

if (i18next) {
const { bindI18n, bindI18nStore, memoize } = this.options;

i18next.IntlMessageFormat = IntlMessageFormat;
i18next.ICU = this;

if (memoize) {
if (bindI18n) {
i18next.on(bindI18n, () => this.clearCache())
}

if (bindI18nStore) {
i18next.store.on(bindI18nStore, () => this.clearCache())
}
}
}

if (this.options.localeData) {
Expand Down Expand Up @@ -70,6 +84,10 @@ class ICU {
// so there is no need to add keys to that finalKeys array
return finalKeys;
}

clearCache() {
this.mem = {};
}
}

ICU.type = 'i18nFormat';
Expand Down
20 changes: 20 additions & 0 deletions test/icu.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ describe('icu format', () => {
expect(i18next.t('key', { numPhotos: 2000 })).toEqual('You have 2,000 photos.');
});

it('should clear the cache on bound events', () => {
i18next.use(ICU).init({
lng: 'en',
resources: {},
i18nFormat: {
memoize: true,
bindI18n: 'languageChanged',
bindI18nStore: 'added'
}
});

const spy = jest.spyOn(i18next.services.i18nFormat, 'clearCache');

expect(spy).not.toHaveBeenCalled();
i18next.changeLanguage('ar');
expect(spy).toHaveBeenCalledTimes(1);
i18next.addResourceBundle('en', 'translation', { key: 'value' });
expect(spy).toHaveBeenCalledTimes(2);
});

});

});

0 comments on commit 7f711b3

Please sign in to comment.