Skip to content

Commit 3e88c18

Browse files
committed
🆙 update: fix infuser
1 parent bc267fe commit 3e88c18

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

src/infuser.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,27 @@ function generate (locales: Locale[], messages: LocaleMessages, descriptor: SFCD
3636

3737
function getTargetLocaleMessages (locales: Locale[], messages: LocaleMessages, descriptor: SFCDescriptor): LocaleMessages {
3838
return locales.reduce((target, locale) => {
39+
debug(`processing curernt: locale=${locale}, target=${target}`)
40+
3941
const obj = messages[locale]
4042
if (obj) {
4143
let o: any = obj
44+
let prev: any = null
4245
const hierarchy = descriptor.hierarchy.concat()
4346
while (hierarchy.length > 0) {
4447
const key = hierarchy.shift()
45-
if (!key) { break }
48+
debug('processing hierarchy key: ', key)
49+
50+
if (!key || !o) { break }
4651
o = o[key]
52+
prev = o
53+
}
54+
55+
if (!o && !prev) {
56+
return target
57+
} else {
58+
return Object.assign(target, { [locale]: ((!o && prev) ? prev : o) }) as LocaleMessages
4759
}
48-
return Object.assign(target, { [locale]: o }) as LocaleMessages
4960
} else {
5061
return target
5162
}
@@ -112,6 +123,8 @@ ${format(stringfyContent(target[locale], 'json'), 'json')}</i18n>`)
112123
}
113124

114125
function format (source: string, lang: string): string {
126+
debug(`format: lang=${lang}, source=${source}`)
127+
115128
switch (lang) {
116129
case 'vue':
117130
return prettier.format(source, { parser: 'vue' })

test/__snapshots__/infuser.test.ts.snap

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,106 @@ export default {};
134134
"
135135
`;
136136
137+
exports[`not full localitation: /path/to/project1/src/App.vue 1`] = `
138+
"<template>
139+
<p>template</p>
140+
</template>
141+
142+
<script>
143+
export default {};
144+
</script>
145+
146+
<i18n>
147+
{
148+
\\"en\\": {
149+
\\"title\\": \\"Application\\"
150+
},
151+
\\"ja\\": {
152+
\\"title\\": \\"アプリケーション\\"
153+
}
154+
}
155+
</i18n>
156+
"
157+
`;
158+
159+
exports[`not full localitation: /path/to/project1/src/components/Modal.vue 1`] = `
160+
"<template>
161+
<p>template</p>
162+
</template>
163+
164+
<script>
165+
export default {};
166+
</script>
167+
168+
<i18n locale=\\"en\\">
169+
{
170+
\\"ok\\": \\"OK\\",
171+
\\"cancel\\": \\"Cancel\\"
172+
}
173+
</i18n>
174+
175+
<i18n locale=\\"ja\\">
176+
{
177+
\\"ok\\": \\"OK\\",
178+
\\"cancel\\": \\"キャンセル\\"
179+
}
180+
</i18n>
181+
"
182+
`;
183+
184+
exports[`not full localitation: /path/to/project1/src/components/nest/RankingTable.vue 1`] = `
185+
"<template>
186+
<p>template</p>
187+
</template>
188+
189+
<script>
190+
export default {};
191+
</script>
192+
193+
<i18n locale=\\"en\\">
194+
{
195+
\\"headers\\": {
196+
\\"rank\\": \\"Rank\\",
197+
\\"name\\": \\"Name\\",
198+
\\"score\\": \\"Score\\"
199+
}
200+
}
201+
</i18n>
202+
203+
<i18n locale=\\"ja\\">
204+
{
205+
\\"headers\\": {
206+
\\"rank\\": \\"ランク\\",
207+
\\"name\\": \\"名前\\",
208+
\\"score\\": \\"スコア\\"
209+
}
210+
}
211+
</i18n>
212+
"
213+
`;
214+
215+
exports[`not full localitation: /path/to/project1/src/pages/Login.vue 1`] = `
216+
"<template>
217+
<p>template</p>
218+
</template>
219+
220+
<script>
221+
export default {};
222+
</script>
223+
224+
<i18n>
225+
{
226+
\\"ja\\": {
227+
\\"id\\": \\"ユーザーID\\",
228+
\\"passowrd\\": \\"パスワード\\",
229+
\\"confirm\\": \\"パスワードの確認入力\\",
230+
\\"button\\": \\"ログイン\\"
231+
}
232+
}
233+
</i18n>
234+
"
235+
`;
236+
137237
exports[`yaml: /path/to/project1/src/components/Modal.vue 1`] = `
138238
"<template>
139239
<p>template</p>

test/infuser.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,23 @@ test('json5', () => {
9797
const outputFiles = infuse(basePath, json5Files, messages)
9898
outputFiles.forEach(file => expect(file.content).toMatchSnapshot(file.path))
9999
})
100+
101+
test('not full localitation', () => {
102+
const messages = squeeze(basePath, jsonFiles as SFCFileInfo[])
103+
104+
// edit locale messages 'ja' only (e.g. translator)
105+
Object.assign(messages['ja']['components'], {
106+
nest: {
107+
'RankingTable': {
108+
headers: {
109+
rank: 'ランク',
110+
name: '名前',
111+
score: 'スコア'
112+
}
113+
}
114+
}
115+
})
116+
117+
const outputFiles = infuse(basePath, jsonFiles, messages)
118+
outputFiles.forEach(file => expect(file.content).toMatchSnapshot(file.path))
119+
})

0 commit comments

Comments
 (0)