Skip to content

Commit 5228b0a

Browse files
authored
breaking: change sfc custom block interface (#246)
* breaking: change sfc custom block interface * fix lint errors
1 parent 5f56f69 commit 5228b0a

File tree

5 files changed

+93
-158
lines changed

5 files changed

+93
-158
lines changed

packages/vue-i18n/src/composer.ts

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
isObject
2727
} from '@intlify/shared'
2828
import {
29-
parse as parsePath,
3029
resolveValue,
3130
createCoreContext,
3231
MISSING_RESOLVE_VALUE,
@@ -108,9 +107,12 @@ export type PreCompileHandler<Message = VueMessageType> = () => {
108107
functions: MessageFunctions<Message>
109108
}
110109

111-
export type CustomBlocks<Message = VueMessageType> =
112-
| Array<string | LocaleMessages<Message>>
113-
| PreCompileHandler<Message>
110+
export interface CustomBlock<Message = VueMessageType> {
111+
locale: Locale
112+
resource: LocaleMessages<Message> | LocaleMessageDictionary<Message>
113+
}
114+
115+
export type CustomBlocks<Message = VueMessageType> = Array<CustomBlock<Message>>
114116

115117
/**
116118
* Composer Options
@@ -950,15 +952,14 @@ export function getLocaleMessages<Message = VueMessageType>(
950952

951953
// merge locale messages of i18n custom block
952954
if (isArray(__i18n)) {
953-
__i18n.forEach(raw => {
954-
deepCopy(isString(raw) ? JSON.parse(raw) : raw, ret)
955+
__i18n.forEach(({ locale, resource }) => {
956+
if (locale) {
957+
ret[locale] = ret[locale] || {}
958+
deepCopy(resource, ret[locale])
959+
} else {
960+
deepCopy(resource, ret)
961+
}
955962
})
956-
return ret
957-
}
958-
959-
if (isFunction(__i18n)) {
960-
const { functions } = __i18n()
961-
addPreCompileMessages<Message>(ret, functions as MessageFunctions<Message>)
962963
}
963964

964965
return ret
@@ -985,41 +986,6 @@ function deepCopy(source: any, destination: any): void {
985986
}
986987
}
987988

988-
export function addPreCompileMessages<Message = VueMessageType>(
989-
messages: LocaleMessages<Message>,
990-
functions: MessageFunctions<Message>
991-
): void {
992-
const keys = Object.keys(functions)
993-
keys.forEach(key => {
994-
const compiled = functions[key]
995-
const { l, k } = JSON.parse(key)
996-
if (!messages[l]) {
997-
messages[l] = {}
998-
}
999-
const targetLocaleMessage = messages[l]
1000-
const paths = parsePath(k)
1001-
if (paths != null) {
1002-
const len = paths.length
1003-
let last = targetLocaleMessage as any // eslint-disable-line @typescript-eslint/no-explicit-any
1004-
let i = 0
1005-
while (i < len) {
1006-
const path = paths[i]
1007-
if (i === len - 1) {
1008-
last[path] = compiled
1009-
break
1010-
} else {
1011-
let val = last[path]
1012-
if (!val) {
1013-
last[path] = val = {}
1014-
}
1015-
last = val
1016-
i++
1017-
}
1018-
}
1019-
}
1020-
})
1021-
}
1022-
1023989
/**
1024990
* Create composer interface factory
1025991
*

packages/vue-i18n/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export {
2121
MissingHandler,
2222
ComposerOptions,
2323
Composer,
24+
CustomBlock,
2425
CustomBlocks
2526
} from './composer'
2627
export {

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

Lines changed: 57 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ import { isString, warn } from '@intlify/shared'
1010
import {
1111
createComposer,
1212
MissingHandler,
13-
addPreCompileMessages,
1413
ComposerOptions,
1514
VueMessageType,
1615
TransrateVNodeSymbol,
1716
NumberPartsSymbol,
1817
DatetimePartsSymbol
1918
} from '../src/composer'
20-
import { generateFormatCacheKey } from '@intlify/shared'
2119
import { watch, watchEffect, nextTick, Text, createVNode } from 'vue'
2220
import {
2321
Locale,
@@ -960,122 +958,97 @@ describe('getNumberFormat / setNumberFormat / mergeNumberFormat', () => {
960958
})
961959

962960
describe('__i18n', () => {
963-
test('default value', () => {
964-
const options = {
965-
__i18n: [
966-
JSON.stringify({ en: { hello: 'Hello,world!' } }),
967-
JSON.stringify({
968-
ja: {
969-
hello: 'こんにちは、世界!',
970-
nest: {
971-
foo: {
972-
bar: 'ばー'
973-
}
961+
test('locale included locale messages', () => {
962+
const enResource = {
963+
locale: '',
964+
resource: {
965+
en: { hello: () => 'Hello,world!' }
966+
}
967+
}
968+
const jaResource = {
969+
locale: '',
970+
resource: {
971+
ja: {
972+
hello: () => 'こんにちは、世界!',
973+
nest: {
974+
foo: {
975+
bar: () => 'ばー'
974976
}
975977
}
976-
})
977-
]
978+
}
979+
}
980+
}
981+
const options = {
982+
__i18n: [enResource, jaResource]
978983
}
979984
const { messages } = createComposer(
980985
options as ComposerOptions<VueMessageType>
981986
)
982987
expect(messages.value).toEqual({
983-
en: { hello: 'Hello,world!' },
984-
ja: {
985-
hello: 'こんにちは、世界!',
986-
nest: {
987-
foo: {
988-
bar: 'ばー'
989-
}
990-
}
991-
}
988+
en: enResource.resource.en,
989+
ja: jaResource.resource.ja
992990
})
993991
})
994992

995-
test('locale messages object', () => {
996-
const options = {
997-
__i18n: [
998-
{ en: { hello: 'Hello,world!' } },
999-
{
1000-
ja: {
1001-
hello: 'こんにちは、世界!',
1002-
nest: {
1003-
foo: {
1004-
bar: 'ばー'
1005-
}
1006-
}
1007-
}
1008-
}
1009-
]
993+
test('locale not included locale messages', () => {
994+
const enResource = {
995+
locale: 'en',
996+
resource: { hello: () => 'Hello,world!' }
1010997
}
1011-
const { messages } = createComposer(
1012-
options as ComposerOptions<VueMessageType>
1013-
)
1014-
expect(messages.value).toEqual({
1015-
en: { hello: 'Hello,world!' },
1016-
ja: {
1017-
hello: 'こんにちは、世界!',
998+
const jaResource = {
999+
locale: 'ja',
1000+
resource: {
1001+
hello: () => 'こんにちは、世界!',
10181002
nest: {
10191003
foo: {
1020-
bar: 'ばー'
1004+
bar: () => 'ばー'
10211005
}
10221006
}
10231007
}
1024-
})
1025-
})
1026-
1027-
test('merge locale messages', () => {
1028-
const msgFn = () => 'ふー'
1008+
}
10291009
const options = {
1030-
__i18n: [
1031-
JSON.stringify({ en: { hello: 'Hello,world!' } }),
1032-
JSON.stringify({ ja: { hello: 'こんにちは、世界!' } })
1033-
],
1034-
messages: {
1035-
en: { foo: 'foo' },
1036-
ja: { foo: msgFn }
1037-
}
1010+
__i18n: [enResource, jaResource]
10381011
}
10391012
const { messages } = createComposer(
10401013
options as ComposerOptions<VueMessageType>
10411014
)
1042-
expect(messages.value!.en).toEqual({
1043-
hello: 'Hello,world!',
1044-
foo: 'foo'
1045-
})
1046-
expect(messages.value!.ja).toEqual({
1047-
hello: 'こんにちは、世界!',
1048-
foo: msgFn
1015+
expect(messages.value).toEqual({
1016+
en: enResource.resource,
1017+
ja: jaResource.resource
10491018
})
10501019
})
10511020

1052-
test('function + locale messages', () => {
1053-
const functions = Object.create(null)
1054-
const msg1 = () => {}
1055-
const msg2 = () => {}
1056-
functions[generateFormatCacheKey('en', 'hello', 'hello,world')] = msg1
1057-
functions[
1058-
generateFormatCacheKey('ja', 'hello.hello', 'こんにちは、世界')
1059-
] = msg2
1021+
test('merge locale messages', () => {
1022+
const msgFnEn = () => 'foo'
1023+
const msgFnJa = () => 'ふー'
1024+
const enI18nFn = () => 'Hello,world!'
1025+
const jaI18nFn = () => 'こんにちは、世界!'
10601026
const options = {
1061-
__i18n: () => ({ functions }),
1027+
__i18n: [
1028+
{
1029+
locale: 'en',
1030+
resource: { hello: enI18nFn }
1031+
},
1032+
{
1033+
locale: 'ja',
1034+
resource: { hello: jaI18nFn }
1035+
}
1036+
],
10621037
messages: {
1063-
en: { foo: 'foo' },
1064-
ja: { foo: 'ふー' }
1038+
en: { foo: msgFnEn },
1039+
ja: { foo: msgFnJa }
10651040
}
10661041
}
10671042
const { messages } = createComposer(
10681043
options as ComposerOptions<VueMessageType>
10691044
)
10701045
expect(messages.value!.en).toEqual({
1071-
hello: msg1,
1072-
foo: 'foo'
1046+
hello: enI18nFn,
1047+
foo: msgFnEn
10731048
})
10741049
expect(messages.value!.ja).toEqual({
1075-
hello: {
1076-
hello: msg2
1077-
},
1078-
foo: 'ふー'
1050+
hello: jaI18nFn,
1051+
foo: msgFnJa
10791052
})
10801053
})
10811054
})
@@ -1208,28 +1181,6 @@ describe('__datetimeParts', () => {
12081181
})
12091182
})
12101183

1211-
test('addPreCompileMessages', () => {
1212-
const messages: any = {}
1213-
const functions = Object.create(null)
1214-
const msg1 = () => {}
1215-
const msg2 = () => {}
1216-
functions[generateFormatCacheKey('en', 'hello', 'hello,world')] = msg1
1217-
functions[
1218-
generateFormatCacheKey('ja', 'foo.bar.hello', 'こんにちは、世界')
1219-
] = msg2
1220-
addPreCompileMessages(messages, functions)
1221-
expect(messages!['en']).toMatchObject({
1222-
hello: msg1
1223-
})
1224-
expect(messages!['ja']).toMatchObject({
1225-
foo: {
1226-
bar: {
1227-
hello: msg2
1228-
}
1229-
}
1230-
})
1231-
})
1232-
12331184
describe('root', () => {
12341185
test('global', () => {
12351186
const __root = createComposer({

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,14 @@ describe('useI18n', () => {
222222
}
223223
const options = instance.type as ComponentOptions
224224
options.__i18n = [
225-
JSON.stringify({ en: { hello: 'Hello,world!' } }),
226-
JSON.stringify({ ja: { hello: 'こんにちは、世界!' } })
225+
{
226+
locale: '',
227+
resource: { en: { hello: 'Hello,world!' } }
228+
},
229+
{
230+
locale: '',
231+
resource: { ja: { hello: 'こんにちは、世界!' } }
232+
}
227233
]
228234
composer = useI18n()
229235
return { t: (composer as Composer).t }
@@ -627,8 +633,14 @@ test('merge i18n custom blocks to global scope', async () => {
627633
}
628634
const options = instance.type as ComponentOptions
629635
options.__i18nGlobal = [
630-
JSON.stringify({ en: { foo: 'foo!' } }),
631-
JSON.stringify({ ja: { foo: 'ふー!' } })
636+
{
637+
locale: 'en',
638+
resource: { foo: 'foo!' }
639+
},
640+
{
641+
locale: 'ja',
642+
resource: { foo: 'ふー!' }
643+
}
632644
]
633645
useI18n({
634646
useScope: 'global',

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ describe('beforeCreate', () => {
5555
}
5656
const App = defineComponent({
5757
template: `<p>{{ $t('bye') }}</p>`,
58-
__i18n: [JSON.stringify(messages)]
58+
__i18n: [
59+
{
60+
locale: '',
61+
resource: messages
62+
}
63+
]
5964
})
6065
const { html } = await mount(App, i18n)
6166

0 commit comments

Comments
 (0)