Skip to content

Commit

Permalink
🐛 bug: fix multiple i18n custom blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Sep 2, 2019
1 parent 5552f42 commit b226c65
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/squeezer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default function sqeeze (basePath: string, files: SFCFileInfo[]): LocaleM

return descriptors.reduce((messages, descriptor) => {
const blockMessages = squeezeFromI18nBlock(descriptor.customBlocks)
debug('squeezeFromI18nBlock: blockMessages', JSON.stringify(blockMessages, null, 2))

const locales = Object.keys(blockMessages)
return locales.reduce((messages, locale) => {
if (!messages[locale]) {
Expand All @@ -37,6 +39,7 @@ export default function sqeeze (basePath: string, files: SFCFileInfo[]): LocaleM
function squeezeFromI18nBlock (blocks: SFCBlock[]): LocaleMessages {
return blocks.reduce((messages, block) => {
debug('i18n block attrs', block.attrs)
debug('i18n block messages', JSON.stringify(messages, null, 2))

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

const locale = block.attrs.locale
if (!locale || typeof locale !== 'string') {
return Object.assign(messages, obj)
const locales = [...new Set([...Object.keys(messages), ...Object.keys(obj)])]
locales.forEach(locale => {
if (messages[locale] && obj[locale]) {
messages[locale] = Object.assign(messages[locale], obj[locale])
} else if (!messages[locale] && obj[locale]) {
messages = Object.assign(messages, obj)
}
})
return messages
} else {
return Object.assign(messages, { [locale]: obj })
if (messages[locale]) {
messages[locale] = Object.assign(messages[locale], obj)
} else {
messages = Object.assign(messages, { [locale]: obj })
}
return messages
}
} else {
return messages
}
}, {})
}, {} as LocaleMessages)
}
112 changes: 112 additions & 0 deletions test/__snapshots__/infuser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,62 @@ export default {}
</i18n>"
`;
exports[`json: /path/to/project1/src/pages/Dashboard1.vue 1`] = `
"<template>
<p>this is the dashboard1</p>
</template>
<script>
export default {
name: 'Dashboard1'
}
</script>
<i18n>
{
\\"ja\\": {
\\"title\\": \\"ダッシュボード\\",
\\"loading\\": \\"読込中...\\"
}
}
</i18n>
<i18n>
{
\\"ja\\": {
\\"title\\": \\"ダッシュボード\\",
\\"loading\\": \\"読込中...\\"
}
}
</i18n>"
`;
exports[`json: /path/to/project1/src/pages/Dashboard2.vue 1`] = `
"<template>
<p>this is the dashboard2</p>
</template>
<script>
export default {
name: 'Dashboard2'
}
</script>
<i18n locale=\\"ja\\">
{
\\"title\\": \\"ダッシュボード\\",
\\"loading\\": \\"読込中...\\"
}
</i18n>
<i18n locale=\\"ja\\">
{
\\"title\\": \\"ダッシュボード\\",
\\"loading\\": \\"読込中...\\"
}
</i18n>"
`;
exports[`json: /path/to/project1/src/pages/Login.vue 1`] = `
"<template>
<p>template</p>
Expand Down Expand Up @@ -204,6 +260,62 @@ export default {}
</i18n>"
`;
exports[`not full localitation: /path/to/project1/src/pages/Dashboard1.vue 1`] = `
"<template>
<p>this is the dashboard1</p>
</template>
<script>
export default {
name: 'Dashboard1'
}
</script>
<i18n>
{
\\"ja\\": {
\\"title\\": \\"ダッシュボード\\",
\\"loading\\": \\"読込中...\\"
}
}
</i18n>
<i18n>
{
\\"ja\\": {
\\"title\\": \\"ダッシュボード\\",
\\"loading\\": \\"読込中...\\"
}
}
</i18n>"
`;
exports[`not full localitation: /path/to/project1/src/pages/Dashboard2.vue 1`] = `
"<template>
<p>this is the dashboard2</p>
</template>
<script>
export default {
name: 'Dashboard2'
}
</script>
<i18n locale=\\"ja\\">
{
\\"title\\": \\"ダッシュボード\\",
\\"loading\\": \\"読込中...\\"
}
</i18n>
<i18n locale=\\"ja\\">
{
\\"title\\": \\"ダッシュボード\\",
\\"loading\\": \\"読込中...\\"
}
</i18n>"
`;
exports[`not full localitation: /path/to/project1/src/pages/Login.vue 1`] = `
"<template>
<p>template</p>
Expand Down
8 changes: 8 additions & 0 deletions test/__snapshots__/squeezer.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ Object {
},
},
"pages": Object {
"Dashboard1": Object {
"loading": "読込中...",
"title": "ダッシュボード",
},
"Dashboard2": Object {
"loading": "読込中...",
"title": "ダッシュボード",
},
"Login": Object {
"button": "ログイン",
"confirm": "パスワードの確認入力",
Expand Down
50 changes: 50 additions & 0 deletions test/fixtures/file/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,54 @@ export default {}
}
}
</i18n>`
}, {
path: '/path/to/project1/src/pages/Dashboard1.vue',
content: `<template>
<p>this is the dashboard1</p>
</template>
<script>
export default {
name: 'Dashboard1'
}
</script>
<i18n>
{
"ja": {
"title": "ダッシュボード"
}
}
</i18n>
<i18n>
{
"ja": {
"loading": "読込中..."
}
}
</i18n>`
}, {
path: '/path/to/project1/src/pages/Dashboard2.vue',
content: `<template>
<p>this is the dashboard2</p>
</template>
<script>
export default {
name: 'Dashboard2'
}
</script>
<i18n locale="ja">
{
"title": "ダッシュボード"
}
</i18n>
<i18n locale="ja">
{
"loading": "読込中..."
}
</i18n>`
}]

0 comments on commit b226c65

Please sign in to comment.