Skip to content

Commit b226c65

Browse files
committed
🐛 bug: fix multiple i18n custom blocks
1 parent 5552f42 commit b226c65

File tree

4 files changed

+189
-3
lines changed

4 files changed

+189
-3
lines changed

src/squeezer.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export default function sqeeze (basePath: string, files: SFCFileInfo[]): LocaleM
1111

1212
return descriptors.reduce((messages, descriptor) => {
1313
const blockMessages = squeezeFromI18nBlock(descriptor.customBlocks)
14+
debug('squeezeFromI18nBlock: blockMessages', JSON.stringify(blockMessages, null, 2))
15+
1416
const locales = Object.keys(blockMessages)
1517
return locales.reduce((messages, locale) => {
1618
if (!messages[locale]) {
@@ -37,6 +39,7 @@ export default function sqeeze (basePath: string, files: SFCFileInfo[]): LocaleM
3739
function squeezeFromI18nBlock (blocks: SFCBlock[]): LocaleMessages {
3840
return blocks.reduce((messages, block) => {
3941
debug('i18n block attrs', block.attrs)
42+
debug('i18n block messages', JSON.stringify(messages, null, 2))
4043

4144
if (block.type === 'i18n') {
4245
let lang = block.attrs.lang
@@ -45,12 +48,25 @@ function squeezeFromI18nBlock (blocks: SFCBlock[]): LocaleMessages {
4548

4649
const locale = block.attrs.locale
4750
if (!locale || typeof locale !== 'string') {
48-
return Object.assign(messages, obj)
51+
const locales = [...new Set([...Object.keys(messages), ...Object.keys(obj)])]
52+
locales.forEach(locale => {
53+
if (messages[locale] && obj[locale]) {
54+
messages[locale] = Object.assign(messages[locale], obj[locale])
55+
} else if (!messages[locale] && obj[locale]) {
56+
messages = Object.assign(messages, obj)
57+
}
58+
})
59+
return messages
4960
} else {
50-
return Object.assign(messages, { [locale]: obj })
61+
if (messages[locale]) {
62+
messages[locale] = Object.assign(messages[locale], obj)
63+
} else {
64+
messages = Object.assign(messages, { [locale]: obj })
65+
}
66+
return messages
5167
}
5268
} else {
5369
return messages
5470
}
55-
}, {})
71+
}, {} as LocaleMessages)
5672
}

test/__snapshots__/infuser.test.ts.snap

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,62 @@ export default {}
7575
</i18n>"
7676
`;
7777
78+
exports[`json: /path/to/project1/src/pages/Dashboard1.vue 1`] = `
79+
"<template>
80+
<p>this is the dashboard1</p>
81+
</template>
82+
83+
<script>
84+
export default {
85+
name: 'Dashboard1'
86+
}
87+
</script>
88+
89+
<i18n>
90+
{
91+
\\"ja\\": {
92+
\\"title\\": \\"ダッシュボード\\",
93+
\\"loading\\": \\"読込中...\\"
94+
}
95+
}
96+
</i18n>
97+
98+
<i18n>
99+
{
100+
\\"ja\\": {
101+
\\"title\\": \\"ダッシュボード\\",
102+
\\"loading\\": \\"読込中...\\"
103+
}
104+
}
105+
</i18n>"
106+
`;
107+
108+
exports[`json: /path/to/project1/src/pages/Dashboard2.vue 1`] = `
109+
"<template>
110+
<p>this is the dashboard2</p>
111+
</template>
112+
113+
<script>
114+
export default {
115+
name: 'Dashboard2'
116+
}
117+
</script>
118+
119+
<i18n locale=\\"ja\\">
120+
{
121+
\\"title\\": \\"ダッシュボード\\",
122+
\\"loading\\": \\"読込中...\\"
123+
}
124+
</i18n>
125+
126+
<i18n locale=\\"ja\\">
127+
{
128+
\\"title\\": \\"ダッシュボード\\",
129+
\\"loading\\": \\"読込中...\\"
130+
}
131+
</i18n>"
132+
`;
133+
78134
exports[`json: /path/to/project1/src/pages/Login.vue 1`] = `
79135
"<template>
80136
<p>template</p>
@@ -204,6 +260,62 @@ export default {}
204260
</i18n>"
205261
`;
206262
263+
exports[`not full localitation: /path/to/project1/src/pages/Dashboard1.vue 1`] = `
264+
"<template>
265+
<p>this is the dashboard1</p>
266+
</template>
267+
268+
<script>
269+
export default {
270+
name: 'Dashboard1'
271+
}
272+
</script>
273+
274+
<i18n>
275+
{
276+
\\"ja\\": {
277+
\\"title\\": \\"ダッシュボード\\",
278+
\\"loading\\": \\"読込中...\\"
279+
}
280+
}
281+
</i18n>
282+
283+
<i18n>
284+
{
285+
\\"ja\\": {
286+
\\"title\\": \\"ダッシュボード\\",
287+
\\"loading\\": \\"読込中...\\"
288+
}
289+
}
290+
</i18n>"
291+
`;
292+
293+
exports[`not full localitation: /path/to/project1/src/pages/Dashboard2.vue 1`] = `
294+
"<template>
295+
<p>this is the dashboard2</p>
296+
</template>
297+
298+
<script>
299+
export default {
300+
name: 'Dashboard2'
301+
}
302+
</script>
303+
304+
<i18n locale=\\"ja\\">
305+
{
306+
\\"title\\": \\"ダッシュボード\\",
307+
\\"loading\\": \\"読込中...\\"
308+
}
309+
</i18n>
310+
311+
<i18n locale=\\"ja\\">
312+
{
313+
\\"title\\": \\"ダッシュボード\\",
314+
\\"loading\\": \\"読込中...\\"
315+
}
316+
</i18n>"
317+
`;
318+
207319
exports[`not full localitation: /path/to/project1/src/pages/Login.vue 1`] = `
208320
"<template>
209321
<p>template</p>

test/__snapshots__/squeezer.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ Object {
3333
},
3434
},
3535
"pages": Object {
36+
"Dashboard1": Object {
37+
"loading": "読込中...",
38+
"title": "ダッシュボード",
39+
},
40+
"Dashboard2": Object {
41+
"loading": "読込中...",
42+
"title": "ダッシュボード",
43+
},
3644
"Login": Object {
3745
"button": "ログイン",
3846
"confirm": "パスワードの確認入力",

test/fixtures/file/json.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,54 @@ export default {}
7676
}
7777
}
7878
</i18n>`
79+
}, {
80+
path: '/path/to/project1/src/pages/Dashboard1.vue',
81+
content: `<template>
82+
<p>this is the dashboard1</p>
83+
</template>
84+
85+
<script>
86+
export default {
87+
name: 'Dashboard1'
88+
}
89+
</script>
90+
91+
<i18n>
92+
{
93+
"ja": {
94+
"title": "ダッシュボード"
95+
}
96+
}
97+
</i18n>
98+
99+
<i18n>
100+
{
101+
"ja": {
102+
"loading": "読込中..."
103+
}
104+
}
105+
</i18n>`
106+
}, {
107+
path: '/path/to/project1/src/pages/Dashboard2.vue',
108+
content: `<template>
109+
<p>this is the dashboard2</p>
110+
</template>
111+
112+
<script>
113+
export default {
114+
name: 'Dashboard2'
115+
}
116+
</script>
117+
118+
<i18n locale="ja">
119+
{
120+
"title": "ダッシュボード"
121+
}
122+
</i18n>
123+
124+
<i18n locale="ja">
125+
{
126+
"loading": "読込中..."
127+
}
128+
</i18n>`
79129
}]

0 commit comments

Comments
 (0)