Skip to content

Commit 08a81e3

Browse files
authored
fix: error on duplicate useI18n calling on local scope (#2203)
* fix: error on duplicate `useI18n` calling on local scope * chore: wrong commits * chore: remove more wrong commits * fix
1 parent 97d167f commit 08a81e3

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

packages/vue-i18n-core/src/errors.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
createCompileError,
3-
CORE_ERROR_CODES_EXTEND_POINT
2+
CORE_ERROR_CODES_EXTEND_POINT,
3+
createCompileError
44
} from '@intlify/core-base'
55

66
import type { BaseError } from '@intlify/shared'
@@ -26,7 +26,9 @@ export const I18nErrorCodes = {
2626
// not compatible legacy vue-i18n constructor
2727
NOT_COMPATIBLE_LEGACY_VUE_I18N: 33,
2828
// Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly
29-
NOT_AVAILABLE_COMPOSITION_IN_LEGACY: 34
29+
NOT_AVAILABLE_COMPOSITION_IN_LEGACY: 34,
30+
// duplicate `useI18n` calling
31+
DUPLICATE_USE_I18N_CALLING: 35
3032
} as const
3133

3234
type I18nErrorCodes = (typeof I18nErrorCodes)[keyof typeof I18nErrorCodes]
@@ -57,5 +59,7 @@ export const errorMessages: { [code: number]: string } = {
5759
[I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N]:
5860
'Not compatible legacy VueI18n.',
5961
[I18nErrorCodes.NOT_AVAILABLE_COMPOSITION_IN_LEGACY]:
60-
'Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly'
62+
'Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly',
63+
[I18nErrorCodes.DUPLICATE_USE_I18N_CALLING]:
64+
"Duplicate `useI18n` calling by local scope. Please don't call it on local scope"
6165
}

packages/vue-i18n-core/src/i18n.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,10 @@ export function useI18n<
772772
setupLifeCycle(i18nInternal, instance, composer)
773773

774774
i18nInternal.__setInstance(instance, composer)
775+
} else {
776+
if (__DEV__ && scope === 'local') {
777+
throw createI18nError(I18nErrorCodes.DUPLICATE_USE_I18N_CALLING)
778+
}
775779
}
776780

777781
return composer as unknown as Composer<

packages/vue-i18n-core/test/i18n.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,66 @@ describe('useI18n', () => {
622622
errorMessages[I18nErrorCodes.NOT_INSTALLED_WITH_PROVIDE]
623623
)
624624
})
625+
626+
test(errorMessages[I18nErrorCodes.DUPLICATE_USE_I18N_CALLING], async () => {
627+
const i18n = createI18n<false>({
628+
legacy: false,
629+
locale: 'en',
630+
fallbackLocale: ['en'],
631+
messages: {
632+
en: { hello: 'hello!' }
633+
}
634+
})
635+
636+
const useMyComposable = () => {
637+
const count = ref(0)
638+
const { t } = useI18n({
639+
messages: {
640+
en: {
641+
there: 'hi there! {count}'
642+
}
643+
}
644+
})
645+
return { message: t('there', { count: count.value }) }
646+
}
647+
648+
let error = ''
649+
const App = defineComponent({
650+
setup() {
651+
let message: string = ''
652+
let t: any // eslint-disable-line @typescript-eslint/no-explicit-any
653+
try {
654+
const i18n = useI18n({
655+
messages: {
656+
en: {
657+
hi: 'hi!'
658+
}
659+
}
660+
})
661+
t = i18n.t
662+
const ret = useMyComposable()
663+
message = ret.message
664+
} catch (e: any) {
665+
error = e.message
666+
}
667+
return { t, message, error }
668+
},
669+
template: `
670+
<h1>Root</h1>
671+
<form>
672+
<select v-model="locale">
673+
<option value="en">en</option>
674+
<option value="ja">ja</option>
675+
</select>
676+
</form>
677+
<p>{{ t('hi') }}</p>
678+
<p>{{ message }}</p>
679+
<p>{{ error }}</p>
680+
`
681+
})
682+
await mount(App, i18n as any) // eslint-disable-line @typescript-eslint/no-explicit-any
683+
expect(error).toBe(errorMessages[I18nErrorCodes.DUPLICATE_USE_I18N_CALLING])
684+
})
625685
})
626686

627687
describe('slot reactivity', () => {

0 commit comments

Comments
 (0)