Skip to content

Commit 27ccd59

Browse files
authored
feat: componentInstanceCreatedListener option (#58)
* feat: componentInstanceCreatedListener option * fix: lint errors
1 parent 8609bd0 commit 27ccd59

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ yarn add vue-i18n@next
212212
- [x] preserveDirectiveContent
213213
- [x] warnHtmlInMessage
214214
- [x] postTranslation
215+
- [x] componentInstanceCreatedListener
215216
- [x] t
216217
- [x] tc
217218
- [x] te

src/legacy.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export interface Formatter {
5858
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5959
interpolate(message: string, values: any, path: string): Array<any> | null
6060
}
61+
export type ComponentInstanceCreatedListener = (
62+
target: VueI18n,
63+
global: VueI18n
64+
) => void
6165

6266
/**
6367
* VueI18n Options
@@ -85,6 +89,7 @@ export interface VueI18nOptions {
8589
pluralizationRules?: PluralizationRules
8690
postTranslation?: PostTranslationHandler
8791
sync?: boolean
92+
componentInstanceCreatedListener?: ComponentInstanceCreatedListener
8893
}
8994

9095
/**
@@ -156,6 +161,7 @@ export interface VueI18n {
156161
export interface VueI18nInternal {
157162
__id: number
158163
__composer: Composer
164+
__onComponentInstanceCreated(target: VueI18n): void
159165
}
160166

161167
/**
@@ -253,7 +259,7 @@ export function createVueI18n(
253259

254260
// defines VueI18n
255261
const vueI18n = {
256-
/*!
262+
/**
257263
* properties
258264
*/
259265

@@ -516,6 +522,14 @@ export function createVueI18n(
516522
__DEV__ &&
517523
warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX))
518524
return -1
525+
},
526+
527+
// for internal
528+
__onComponentInstanceCreated(target: VueI18n): void {
529+
const { componentInstanceCreatedListener } = options
530+
if (componentInstanceCreatedListener) {
531+
componentInstanceCreatedListener(target, vueI18n)
532+
}
519533
}
520534
}
521535

src/mixin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,15 @@ export function defineMixin(
160160
}
161161
optionsI18n.__root = composer
162162
this.$i18n = createVueI18n(optionsI18n)
163+
legacy.__onComponentInstanceCreated(this.$i18n)
163164

164165
i18n._setLegacy(instance, this.$i18n)
165166
} else if (options.__i18n) {
166167
this.$i18n = createVueI18n({
167168
__i18n: options.__i18n,
168169
__root: composer
169170
})
171+
legacy.__onComponentInstanceCreated(this.$i18n)
170172

171173
i18n._setLegacy(instance, this.$i18n)
172174
} else {

test/mixin.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,25 @@ test('$i18n', async () => {
176176
expect(vm.$i18n.t('hello')).toEqual('hello!')
177177
})
178178

179+
test('VueI18n componentInstanceCreatedListener option', async () => {
180+
const componentInstanceCreatedListener = jest.fn()
181+
const i18n = createI18n({
182+
legacy: true,
183+
locale: 'en',
184+
componentInstanceCreatedListener
185+
})
186+
187+
const App = defineComponent({
188+
template: '<br/>',
189+
i18n: {
190+
locale: 'ja'
191+
}
192+
})
193+
await mount(App, i18n)
194+
195+
expect(componentInstanceCreatedListener).toHaveBeenCalled()
196+
})
197+
179198
test.skip('beforeDestroy', async () => {
180199
const i18n = createI18n({
181200
legacy: true,

0 commit comments

Comments
 (0)