Skip to content

breaking: i18n resource to object from string #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions src/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ export function generateCode(
query: ParsedUrlQuery,
options: VueI18nLoaderOptions
): string {
const data = convert(source, query.lang as string)
let value = JSON.parse(data)

if (query.locale && typeof query.locale === 'string') {
value = Object.assign({}, { [query.locale]: value })
}
const value = merge(parse(source, query), query)

let code = ''
const preCompile = !!options.preCompile
Expand All @@ -37,28 +32,41 @@ export function generateCode(
Component.__i18n = Component.__i18n || _getResource
}\n`
} else {
value = friendlyJSONstringify(value)
code += `export default function (Component) {
Component.__i18n = Component.__i18n || []
Component.__i18n.push('${value}')
Component.__i18n.push(${stringify(value)})
}\n`
}

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

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

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function merge(data: any, query: ParsedUrlQuery): any {
if (query.locale && typeof query.locale === 'string') {
return Object.assign({}, { [query.locale]: data })
} else {
return data
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parse(source: string | Buffer, query: ParsedUrlQuery): any {
const value = Buffer.isBuffer(source) ? source.toString() : source
const { lang } = query
switch (lang) {
case 'yaml':
case 'yml':
const data = yaml.safeLoad(value)
return JSON.stringify(data, undefined, '\t')
return yaml.safeLoad(value)
case 'json5':
return JSON.stringify(JSON5.parse(value))
return JSON5.parse(value)
default:
return value
return JSON.parse(value)
}
}

Expand Down
74 changes: 61 additions & 13 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,106 @@

exports[`basic 1`] = `
Array [
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
Object {
"en": Object {
"hello": "hello world!",
},
},
]
`;

exports[`import 1`] = `
Array [
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
Object {
"en": Object {
"hello": "hello world!",
},
},
]
`;

exports[`json5 1`] = `
Array [
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
Object {
"en": Object {
"hello": "hello world!",
},
},
]
`;

exports[`locale attr 1`] = `
Array [
"{\\"ja\\":{\\"hello\\":\\"こんにちは、世界!\\"}}",
Object {
"ja": Object {
"hello": "こんにちは、世界!",
},
},
]
`;

exports[`locale attr and basic 1`] = `
Array [
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
"{\\"ja\\":{\\"hello\\":\\"こんにちは、世界!\\"}}",
Object {
"en": Object {
"hello": "hello world!",
},
},
Object {
"ja": Object {
"hello": "こんにちは、世界!",
},
},
]
`;

exports[`locale attr and import 1`] = `
Array [
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
Object {
"en": Object {
"hello": "hello world!",
},
},
]
`;

exports[`multiple 1`] = `
Array [
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
"{\\"ja\\":{\\"hello\\":\\"こんにちは、世界!\\"}}",
Object {
"en": Object {
"hello": "hello world!",
},
},
Object {
"ja": Object {
"hello": "こんにちは、世界!",
},
},
]
`;

exports[`special characters 1`] = `
Array [
"{\\"en\\":{\\"hello\\":\\"hello
great \\"world\\"\\"}}",
Object {
"en": Object {
"hello": "hello
great \\"world\\"",
},
},
]
`;

exports[`yaml 1`] = `
Array [
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
"{\\"ja\\":{\\"hello\\":\\"こんにちは、世界!\\"}}",
Object {
"en": Object {
"hello": "hello world!",
},
},
Object {
"ja": Object {
"hello": "こんにちは、世界!",
},
},
]
`;