Skip to content

Commit 7cdeff9

Browse files
authored
Merge pull request #70 from markdegrootnl/load-callback
Added onLoad Option which is called after language is loaded.
2 parents 6d7f0db + c024baf commit 7cdeff9

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ let sharedInstance: I18n = null
2121
const DEFAULT_OPTIONS: OptionsInterface = {
2222
lang: !isServer && document.documentElement.lang ? document.documentElement.lang.replace('-', '_') : null,
2323
fallbackLang: 'en',
24-
resolve: (lang: string) => new Promise((resolve) => resolve({ default: {} }))
24+
resolve: (lang: string) => new Promise((resolve) => resolve({ default: {} })),
25+
onLoad: (lang: string) => {}
2526
}
2627

2728
/**
@@ -305,6 +306,7 @@ export class I18n {
305306
}
306307
}
307308

309+
this.options.onLoad(lang)
308310
return lang
309311
}
310312

src/interfaces/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export interface OptionsInterface {
77
lang?: string
88
fallbackLang?: string
99
resolve?(lang: string): Promise<LanguageJsonFileInterface>
10+
onLoad?: (lang: string) => void
1011
}

test/class.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,19 @@ it('allows resetting all data', async () => {
6868
expect(i18n.getActiveLanguage()).toBe('en')
6969
expect(i18n.trans('Welcome!')).toBe('Welcome!')
7070
})
71+
72+
it('calls onLoad when loaded', async () => {
73+
const onLoadFunction = jest.fn()
74+
75+
const i18n = new I18n({
76+
lang: 'pt',
77+
resolve: lang => import(`./fixtures/lang/${lang}.json`),
78+
onLoad: onLoadFunction
79+
})
80+
81+
await i18n.loadLanguageAsync('en')
82+
83+
expect(onLoadFunction).toHaveBeenCalledTimes(2)
84+
expect(onLoadFunction).toHaveBeenCalledWith('en')
85+
expect(onLoadFunction).toHaveBeenCalledWith('pt')
86+
})

0 commit comments

Comments
 (0)