Skip to content

Commit

Permalink
feat: componentInstanceCreatedListener option (#58)
Browse files Browse the repository at this point in the history
* feat: componentInstanceCreatedListener option

* fix: lint errors
  • Loading branch information
kazupon authored Jun 3, 2020
1 parent 8609bd0 commit 27ccd59
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ yarn add vue-i18n@next
- [x] preserveDirectiveContent
- [x] warnHtmlInMessage
- [x] postTranslation
- [x] componentInstanceCreatedListener
- [x] t
- [x] tc
- [x] te
Expand Down
16 changes: 15 additions & 1 deletion src/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export interface Formatter {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
interpolate(message: string, values: any, path: string): Array<any> | null
}
export type ComponentInstanceCreatedListener = (
target: VueI18n,
global: VueI18n
) => void

/**
* VueI18n Options
Expand Down Expand Up @@ -85,6 +89,7 @@ export interface VueI18nOptions {
pluralizationRules?: PluralizationRules
postTranslation?: PostTranslationHandler
sync?: boolean
componentInstanceCreatedListener?: ComponentInstanceCreatedListener
}

/**
Expand Down Expand Up @@ -156,6 +161,7 @@ export interface VueI18n {
export interface VueI18nInternal {
__id: number
__composer: Composer
__onComponentInstanceCreated(target: VueI18n): void
}

/**
Expand Down Expand Up @@ -253,7 +259,7 @@ export function createVueI18n(

// defines VueI18n
const vueI18n = {
/*!
/**
* properties
*/

Expand Down Expand Up @@ -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)
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@ export function defineMixin(
}
optionsI18n.__root = composer
this.$i18n = createVueI18n(optionsI18n)
legacy.__onComponentInstanceCreated(this.$i18n)

i18n._setLegacy(instance, this.$i18n)
} else if (options.__i18n) {
this.$i18n = createVueI18n({
__i18n: options.__i18n,
__root: composer
})
legacy.__onComponentInstanceCreated(this.$i18n)

i18n._setLegacy(instance, this.$i18n)
} else {
Expand Down
19 changes: 19 additions & 0 deletions test/mixin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<br/>',
i18n: {
locale: 'ja'
}
})
await mount(App, i18n)

expect(componentInstanceCreatedListener).toHaveBeenCalled()
})

test.skip('beforeDestroy', async () => {
const i18n = createI18n({
legacy: true,
Expand Down

0 comments on commit 27ccd59

Please sign in to comment.