diff --git a/README.md b/README.md index 05e106da2..7dd47954f 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,7 @@ yarn add vue-i18n@next - [x] preserveDirectiveContent - [x] warnHtmlInMessage - [x] postTranslation + - [x] componentInstanceCreatedListener - [x] t - [x] tc - [x] te diff --git a/src/legacy.ts b/src/legacy.ts index a1feba4a5..4bd94cd65 100644 --- a/src/legacy.ts +++ b/src/legacy.ts @@ -58,6 +58,10 @@ export interface Formatter { // eslint-disable-next-line @typescript-eslint/no-explicit-any interpolate(message: string, values: any, path: string): Array | null } +export type ComponentInstanceCreatedListener = ( + target: VueI18n, + global: VueI18n +) => void /** * VueI18n Options @@ -85,6 +89,7 @@ export interface VueI18nOptions { pluralizationRules?: PluralizationRules postTranslation?: PostTranslationHandler sync?: boolean + componentInstanceCreatedListener?: ComponentInstanceCreatedListener } /** @@ -156,6 +161,7 @@ export interface VueI18n { export interface VueI18nInternal { __id: number __composer: Composer + __onComponentInstanceCreated(target: VueI18n): void } /** @@ -253,7 +259,7 @@ export function createVueI18n( // defines VueI18n const vueI18n = { - /*! + /** * properties */ @@ -516,6 +522,14 @@ export function createVueI18n( __DEV__ && warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX)) return -1 + }, + + // for internal + __onComponentInstanceCreated(target: VueI18n): void { + const { componentInstanceCreatedListener } = options + if (componentInstanceCreatedListener) { + componentInstanceCreatedListener(target, vueI18n) + } } } diff --git a/src/mixin.ts b/src/mixin.ts index e2c353c5f..5f33649b7 100644 --- a/src/mixin.ts +++ b/src/mixin.ts @@ -160,6 +160,7 @@ export function defineMixin( } optionsI18n.__root = composer this.$i18n = createVueI18n(optionsI18n) + legacy.__onComponentInstanceCreated(this.$i18n) i18n._setLegacy(instance, this.$i18n) } else if (options.__i18n) { @@ -167,6 +168,7 @@ export function defineMixin( __i18n: options.__i18n, __root: composer }) + legacy.__onComponentInstanceCreated(this.$i18n) i18n._setLegacy(instance, this.$i18n) } else { diff --git a/test/mixin.test.ts b/test/mixin.test.ts index 53788df36..e9ff15cdb 100644 --- a/test/mixin.test.ts +++ b/test/mixin.test.ts @@ -176,6 +176,25 @@ test('$i18n', async () => { expect(vm.$i18n.t('hello')).toEqual('hello!') }) +test('VueI18n componentInstanceCreatedListener option', async () => { + const componentInstanceCreatedListener = jest.fn() + const i18n = createI18n({ + legacy: true, + locale: 'en', + componentInstanceCreatedListener + }) + + const App = defineComponent({ + template: '
', + i18n: { + locale: 'ja' + } + }) + await mount(App, i18n) + + expect(componentInstanceCreatedListener).toHaveBeenCalled() +}) + test.skip('beforeDestroy', async () => { const i18n = createI18n({ legacy: true,