diff --git a/packages/vue-i18n-core/src/errors.ts b/packages/vue-i18n-core/src/errors.ts index 22f95825e..81af6f40b 100644 --- a/packages/vue-i18n-core/src/errors.ts +++ b/packages/vue-i18n-core/src/errors.ts @@ -71,7 +71,7 @@ export const errorMessages: { [code: number]: string } = { [I18nErrorCodes.BRIDGE_SUPPORT_VUE_2_ONLY]: 'vue-i18n-bridge support Vue 2.x only', [I18nErrorCodes.MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION]: - 'Must define ‘i18n’ option in Composition API with using local scope in Legacy API mode', + 'Must define ‘i18n’ option or custom block in Composition API with using local scope in Legacy API mode', [I18nErrorCodes.NOT_AVAILABLE_COMPOSITION_IN_LEGACY]: 'Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly' } diff --git a/packages/vue-i18n-core/src/i18n.ts b/packages/vue-i18n-core/src/i18n.ts index 3401f252e..5544e70d4 100644 --- a/packages/vue-i18n-core/src/i18n.ts +++ b/packages/vue-i18n-core/src/i18n.ts @@ -1099,7 +1099,11 @@ function useI18nForLegacy( const isLocale = scope === 'local' const _composer = shallowRef(null) - if (isLocale && instance.proxy && !instance.proxy.$options.i18n) { + if ( + isLocale && + instance.proxy && + !(instance.proxy.$options.i18n || instance.proxy.$options.__i18n) + ) { throw createI18nError( I18nErrorCodes.MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION ) diff --git a/packages/vue-i18n-core/test/i18n.test.ts b/packages/vue-i18n-core/test/i18n.test.ts index b74cac460..a94b82afb 100644 --- a/packages/vue-i18n-core/test/i18n.test.ts +++ b/packages/vue-i18n-core/test/i18n.test.ts @@ -525,6 +525,45 @@ describe('useI18n', () => { expect(html()).toEqual('

en:world!

') }) + test('use custom block', async () => { + const i18n = createI18n({ + allowComposition: true, + locale: 'ja', + messages: { + en: { + hello: 'hello!' + }, + ja: {} + } + }) + + const App = defineComponent({ + setup() { + const instance = getCurrentInstance() + if (instance == null) { + throw new Error() + } + const options = instance.type as ComponentOptions + options.__i18n = [ + { + locale: 'ja', + resource: { + hello: 'こんにちは!' + } + } + ] + const { locale, t } = useI18n({ + inheritLocale: true, + useScope: 'local' + }) + return { locale, t } + }, + template: `

{{ locale }}:{{ t('hello') }}

` + }) + const { html } = await mount(App, i18n) + expect(html()).toEqual('

ja:こんにちは!

') + }) + test('not defined i18n option in local scope', async () => { const i18n = createI18n({ allowComposition: true,