Skip to content

Commit 8947026

Browse files
authored
breaking: i18n resource to object from string (#109)
1 parent ba3b786 commit 8947026

File tree

2 files changed

+83
-27
lines changed

2 files changed

+83
-27
lines changed

src/gen.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ export function generateCode(
2121
query: ParsedUrlQuery,
2222
options: VueI18nLoaderOptions
2323
): string {
24-
const data = convert(source, query.lang as string)
25-
let value = JSON.parse(data)
26-
27-
if (query.locale && typeof query.locale === 'string') {
28-
value = Object.assign({}, { [query.locale]: value })
29-
}
24+
const value = merge(parse(source, query), query)
3025

3126
let code = ''
3227
const preCompile = !!options.preCompile
@@ -37,28 +32,41 @@ export function generateCode(
3732
Component.__i18n = Component.__i18n || _getResource
3833
}\n`
3934
} else {
40-
value = friendlyJSONstringify(value)
4135
code += `export default function (Component) {
4236
Component.__i18n = Component.__i18n || []
43-
Component.__i18n.push('${value}')
37+
Component.__i18n.push(${stringify(value)})
4438
}\n`
4539
}
4640

4741
return prettier.format(code, { parser: 'babel' })
4842
}
4943

50-
function convert(source: string | Buffer, lang: string): string {
51-
const value = Buffer.isBuffer(source) ? source.toString() : source
44+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
45+
function stringify(data: any): string {
46+
return friendlyJSONstringify(data)
47+
}
5248

49+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
50+
function merge(data: any, query: ParsedUrlQuery): any {
51+
if (query.locale && typeof query.locale === 'string') {
52+
return Object.assign({}, { [query.locale]: data })
53+
} else {
54+
return data
55+
}
56+
}
57+
58+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
59+
function parse(source: string | Buffer, query: ParsedUrlQuery): any {
60+
const value = Buffer.isBuffer(source) ? source.toString() : source
61+
const { lang } = query
5362
switch (lang) {
5463
case 'yaml':
5564
case 'yml':
56-
const data = yaml.safeLoad(value)
57-
return JSON.stringify(data, undefined, '\t')
65+
return yaml.safeLoad(value)
5866
case 'json5':
59-
return JSON.stringify(JSON5.parse(value))
67+
return JSON5.parse(value)
6068
default:
61-
return value
69+
return JSON.parse(value)
6270
}
6371
}
6472

test/__snapshots__/index.test.ts.snap

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,106 @@
22

33
exports[`basic 1`] = `
44
Array [
5-
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
5+
Object {
6+
"en": Object {
7+
"hello": "hello world!",
8+
},
9+
},
610
]
711
`;
812

913
exports[`import 1`] = `
1014
Array [
11-
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
15+
Object {
16+
"en": Object {
17+
"hello": "hello world!",
18+
},
19+
},
1220
]
1321
`;
1422

1523
exports[`json5 1`] = `
1624
Array [
17-
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
25+
Object {
26+
"en": Object {
27+
"hello": "hello world!",
28+
},
29+
},
1830
]
1931
`;
2032

2133
exports[`locale attr 1`] = `
2234
Array [
23-
"{\\"ja\\":{\\"hello\\":\\"こんにちは、世界!\\"}}",
35+
Object {
36+
"ja": Object {
37+
"hello": "こんにちは、世界!",
38+
},
39+
},
2440
]
2541
`;
2642

2743
exports[`locale attr and basic 1`] = `
2844
Array [
29-
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
30-
"{\\"ja\\":{\\"hello\\":\\"こんにちは、世界!\\"}}",
45+
Object {
46+
"en": Object {
47+
"hello": "hello world!",
48+
},
49+
},
50+
Object {
51+
"ja": Object {
52+
"hello": "こんにちは、世界!",
53+
},
54+
},
3155
]
3256
`;
3357

3458
exports[`locale attr and import 1`] = `
3559
Array [
36-
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
60+
Object {
61+
"en": Object {
62+
"hello": "hello world!",
63+
},
64+
},
3765
]
3866
`;
3967

4068
exports[`multiple 1`] = `
4169
Array [
42-
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
43-
"{\\"ja\\":{\\"hello\\":\\"こんにちは、世界!\\"}}",
70+
Object {
71+
"en": Object {
72+
"hello": "hello world!",
73+
},
74+
},
75+
Object {
76+
"ja": Object {
77+
"hello": "こんにちは、世界!",
78+
},
79+
},
4480
]
4581
`;
4682

4783
exports[`special characters 1`] = `
4884
Array [
49-
"{\\"en\\":{\\"hello\\":\\"hello
50-
great \\"world\\"\\"}}",
85+
Object {
86+
"en": Object {
87+
"hello": "hello
88+
great \\"world\\"",
89+
},
90+
},
5191
]
5292
`;
5393

5494
exports[`yaml 1`] = `
5595
Array [
56-
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
57-
"{\\"ja\\":{\\"hello\\":\\"こんにちは、世界!\\"}}",
96+
Object {
97+
"en": Object {
98+
"hello": "hello world!",
99+
},
100+
},
101+
Object {
102+
"ja": Object {
103+
"hello": "こんにちは、世界!",
104+
},
105+
},
58106
]
59107
`;

0 commit comments

Comments
 (0)