Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Bind i18n and resource store events to clear the memoized cache #18

Merged
merged 1 commit into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});

});

});