Skip to content

Commit 8a2a5a2

Browse files
authored
improvement: allow custom block useI18n on Legacy API mode (#1022)
resolve #1020
1 parent b978f08 commit 8a2a5a2

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const errorMessages: { [code: number]: string } = {
7171
[I18nErrorCodes.BRIDGE_SUPPORT_VUE_2_ONLY]:
7272
'vue-i18n-bridge support Vue 2.x only',
7373
[I18nErrorCodes.MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION]:
74-
'Must define ‘i18n’ option in Composition API with using local scope in Legacy API mode',
74+
'Must define ‘i18n’ option or custom block in Composition API with using local scope in Legacy API mode',
7575
[I18nErrorCodes.NOT_AVAILABLE_COMPOSITION_IN_LEGACY]:
7676
'Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly'
7777
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,11 @@ function useI18nForLegacy(
10991099
const isLocale = scope === 'local'
11001100
const _composer = shallowRef<Composer | null>(null)
11011101

1102-
if (isLocale && instance.proxy && !instance.proxy.$options.i18n) {
1102+
if (
1103+
isLocale &&
1104+
instance.proxy &&
1105+
!(instance.proxy.$options.i18n || instance.proxy.$options.__i18n)
1106+
) {
11031107
throw createI18nError(
11041108
I18nErrorCodes.MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION
11051109
)

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,45 @@ describe('useI18n', () => {
525525
expect(html()).toEqual('<p>en:world!</p>')
526526
})
527527

528+
test('use custom block', async () => {
529+
const i18n = createI18n({
530+
allowComposition: true,
531+
locale: 'ja',
532+
messages: {
533+
en: {
534+
hello: 'hello!'
535+
},
536+
ja: {}
537+
}
538+
})
539+
540+
const App = defineComponent({
541+
setup() {
542+
const instance = getCurrentInstance()
543+
if (instance == null) {
544+
throw new Error()
545+
}
546+
const options = instance.type as ComponentOptions
547+
options.__i18n = [
548+
{
549+
locale: 'ja',
550+
resource: {
551+
hello: 'こんにちは!'
552+
}
553+
}
554+
]
555+
const { locale, t } = useI18n({
556+
inheritLocale: true,
557+
useScope: 'local'
558+
})
559+
return { locale, t }
560+
},
561+
template: `<p>{{ locale }}:{{ t('hello') }}</p>`
562+
})
563+
const { html } = await mount(App, i18n)
564+
expect(html()).toEqual('<p>ja:こんにちは!</p>')
565+
})
566+
528567
test('not defined i18n option in local scope', async () => {
529568
const i18n = createI18n({
530569
allowComposition: true,

0 commit comments

Comments
 (0)