Skip to content

Commit caeb392

Browse files
authored
fix: merge global resources of i18n custom block (#806)
closes #805
1 parent 504675d commit caeb392

File tree

4 files changed

+140
-84
lines changed

4 files changed

+140
-84
lines changed

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

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import { defineMixin as defineMixinBridge } from './mixins/bridge'
3333
import { enableDevTools, addTimelineEvent } from './devtools'
3434
import {
3535
isLegacyVueI18n,
36-
getLocaleMessages,
37-
getComponentOptions
36+
getComponentOptions,
37+
adjustI18nResources
3838
} from './utils'
3939

4040
import type { ComponentInternalInstance, App } from 'vue'
@@ -867,47 +867,6 @@ function getGlobalComposer(i18n: I18n): Composer {
867867
: (i18n.global as unknown as Composer)
868868
}
869869

870-
function adjustI18nResources(
871-
global: Composer,
872-
options: UseI18nOptions,
873-
componentOptions: any // eslint-disable-line @typescript-eslint/no-explicit-any
874-
): void {
875-
let messages = isObject(options.messages) ? options.messages : {}
876-
if ('__i18nGlobal' in componentOptions) {
877-
messages = getLocaleMessages(global.locale.value as Locale, {
878-
messages,
879-
__i18n: componentOptions.__i18nGlobal
880-
})
881-
}
882-
// merge locale messages
883-
const locales = Object.keys(messages)
884-
if (locales.length) {
885-
locales.forEach(locale => {
886-
global.mergeLocaleMessage(locale, messages[locale])
887-
})
888-
}
889-
if (!__LITE__) {
890-
// merge datetime formats
891-
if (isObject(options.datetimeFormats)) {
892-
const locales = Object.keys(options.datetimeFormats)
893-
if (locales.length) {
894-
locales.forEach(locale => {
895-
global.mergeDateTimeFormat(locale, options.datetimeFormats![locale])
896-
})
897-
}
898-
}
899-
// merge number formats
900-
if (isObject(options.numberFormats)) {
901-
const locales = Object.keys(options.numberFormats)
902-
if (locales.length) {
903-
locales.forEach(locale => {
904-
global.mergeNumberFormat(locale, options.numberFormats![locale])
905-
})
906-
}
907-
}
908-
}
909-
}
910-
911870
function getComposer(
912871
i18n: I18n,
913872
target: ComponentInternalInstance,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createVueI18n } from '../legacy'
33
import { createI18nError, I18nErrorCodes } from '../errors'
44
import { SetPluralRulesSymbol } from '../symbols'
55
import { addTimelineEvent } from '../devtools'
6-
import { getLocaleMessages } from '../utils'
6+
import { getLocaleMessages, adjustI18nResources } from '../utils'
77
import { createEmitter } from '@intlify/shared'
88

99
import type { ComponentOptions } from 'vue'
@@ -16,6 +16,7 @@ import type {
1616
import type {
1717
Composer,
1818
ComposerInternalOptions,
19+
ComposerOptions,
1920
VueMessageType
2021
} from '../composer'
2122
import type {
@@ -75,6 +76,10 @@ export function defineMixin(
7576
this.$i18n = vuei18n
7677
}
7778

79+
if (options.__i18nGlobal) {
80+
adjustI18nResources(composer, options as ComposerOptions, options)
81+
}
82+
7883
;(vuei18n as unknown as VueI18nInternal).__onComponentInstanceCreated(
7984
this.$i18n
8085
)

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import {
99
import { I18nErrorCodes, createI18nError } from './errors'
1010

1111
import type { Locale, MessageResolver } from '@intlify/core-base'
12-
import type { CustomBlocks, VueMessageType } from './composer'
12+
import type {
13+
Composer,
14+
ComposerOptions,
15+
CustomBlocks,
16+
VueMessageType
17+
} from './composer'
1318
import type { ComponentInternalInstance } from 'vue'
1419

1520
type GetLocaleMessagesOptions<Messages = {}> = {
@@ -144,4 +149,44 @@ export function getComponentOptions(instance: ComponentInternalInstance): any {
144149
return !__BRIDGE__ ? instance.type : instance.proxy!.$options
145150
}
146151

152+
export function adjustI18nResources(
153+
global: Composer,
154+
options: ComposerOptions,
155+
componentOptions: any // eslint-disable-line @typescript-eslint/no-explicit-any
156+
): void {
157+
let messages = isObject(options.messages) ? options.messages : {}
158+
if ('__i18nGlobal' in componentOptions) {
159+
messages = getLocaleMessages(global.locale.value as Locale, {
160+
messages,
161+
__i18n: componentOptions.__i18nGlobal
162+
})
163+
}
164+
// merge locale messages
165+
const locales = Object.keys(messages)
166+
if (locales.length) {
167+
locales.forEach(locale => {
168+
global.mergeLocaleMessage(locale, messages[locale])
169+
})
170+
}
171+
if (!__LITE__) {
172+
// merge datetime formats
173+
if (isObject(options.datetimeFormats)) {
174+
const locales = Object.keys(options.datetimeFormats)
175+
if (locales.length) {
176+
locales.forEach(locale => {
177+
global.mergeDateTimeFormat(locale, options.datetimeFormats![locale])
178+
})
179+
}
180+
}
181+
// merge number formats
182+
if (isObject(options.numberFormats)) {
183+
const locales = Object.keys(options.numberFormats)
184+
if (locales.length) {
185+
locales.forEach(locale => {
186+
global.mergeNumberFormat(locale, options.numberFormats![locale])
187+
})
188+
}
189+
}
190+
}
191+
}
147192
/* eslint-enable @typescript-eslint/no-explicit-any */

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

Lines changed: 86 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -718,25 +718,81 @@ test('merge useI18n resources to global scope', async () => {
718718
})
719719
})
720720

721-
test('merge i18n custom blocks to global scope', async () => {
722-
const i18n = createI18n({
723-
legacy: false,
724-
locale: 'ja',
725-
messages: {
726-
en: {
727-
hi: { hello: 'hello!' }
721+
describe('merge i18n custom blocks to global scope', () => {
722+
test('composition mode', async () => {
723+
const i18n = createI18n({
724+
legacy: false,
725+
locale: 'ja',
726+
messages: {
727+
en: {
728+
hi: { hello: 'hello!' }
729+
}
728730
}
729-
}
731+
})
732+
733+
const App = defineComponent({
734+
setup() {
735+
const instance = getCurrentInstance()
736+
if (instance == null) {
737+
throw new Error()
738+
}
739+
const options = instance.type as ComponentOptions
740+
options.__i18nGlobal = [
741+
{
742+
locale: 'en',
743+
resource: {
744+
hi: { hi: 'hi!' },
745+
foo: 'foo!'
746+
}
747+
},
748+
{
749+
locale: 'ja',
750+
resource: { foo: 'ふー!' }
751+
}
752+
]
753+
useI18n({
754+
useScope: 'global',
755+
messages: {
756+
ja: {
757+
hello: 'こんにちは!'
758+
}
759+
}
760+
})
761+
return {}
762+
},
763+
template: `<p>foo</p>`
764+
})
765+
await mount(App, i18n)
766+
767+
expect(i18n.global.getLocaleMessage('en')).toEqual({
768+
hi: {
769+
hi: 'hi!',
770+
hello: 'hello!'
771+
},
772+
foo: 'foo!'
773+
})
774+
expect(i18n.global.getLocaleMessage('ja')).toEqual({
775+
hello: 'こんにちは!',
776+
foo: 'ふー!'
777+
})
730778
})
731779

732-
const App = defineComponent({
733-
setup() {
734-
const instance = getCurrentInstance()
735-
if (instance == null) {
736-
throw new Error()
780+
test('legacy mode', async () => {
781+
const i18n = createI18n({
782+
legacy: true,
783+
locale: 'ja',
784+
messages: {
785+
en: {
786+
hi: { hello: 'hello!' }
787+
},
788+
ja: {
789+
hello: 'こんにちは!'
790+
}
737791
}
738-
const options = instance.type as ComponentOptions
739-
options.__i18nGlobal = [
792+
})
793+
794+
const App = defineComponent({
795+
__i18nGlobal: [
740796
{
741797
locale: 'en',
742798
resource: {
@@ -748,31 +804,22 @@ test('merge i18n custom blocks to global scope', async () => {
748804
locale: 'ja',
749805
resource: { foo: 'ふー!' }
750806
}
751-
]
752-
useI18n({
753-
useScope: 'global',
754-
messages: {
755-
ja: {
756-
hello: 'こんにちは!'
757-
}
758-
}
759-
})
760-
return {}
761-
},
762-
template: `<p>foo</p>`
763-
})
764-
await mount(App, i18n)
807+
],
808+
template: `<p>foo</p>`
809+
})
810+
await mount(App, i18n)
765811

766-
expect(i18n.global.getLocaleMessage('en')).toEqual({
767-
hi: {
768-
hi: 'hi!',
769-
hello: 'hello!'
770-
},
771-
foo: 'foo!'
772-
})
773-
expect(i18n.global.getLocaleMessage('ja')).toEqual({
774-
hello: 'こんにちは!',
775-
foo: 'ふー!'
812+
expect(i18n.global.getLocaleMessage('en')).toEqual({
813+
hi: {
814+
hi: 'hi!',
815+
hello: 'hello!'
816+
},
817+
foo: 'foo!'
818+
})
819+
expect(i18n.global.getLocaleMessage('ja')).toEqual({
820+
hello: 'こんにちは!',
821+
foo: 'ふー!'
822+
})
776823
})
777824
})
778825

0 commit comments

Comments
 (0)