Skip to content

Commit 5e21036

Browse files
committed
⭐ new(api): support yaml and json5 format
1 parent 9f4c47c commit 5e21036

File tree

7 files changed

+136
-16
lines changed

7 files changed

+136
-16
lines changed

package.json

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,18 @@
99
"bugs": {
1010
"url": "https://github.com/kazupon/vue-i18n-locale-message/issues"
1111
},
12-
"types": "types/index.d.ts",
13-
"files": [
14-
"types",
15-
"lib",
16-
"src"
17-
],
1812
"dependencies": {
1913
"@vue/component-compiler-utils": "^3.0.0",
2014
"debug": "^4.1.1",
15+
"js-yaml": "^3.13.1",
16+
"json5": "^2.1.0",
2117
"vue-template-compiler": "^2.6.10"
2218
},
2319
"devDependencies": {
2420
"@types/debug": "^4.1.4",
2521
"@types/jest": "^24.0.15",
22+
"@types/js-yaml": "^3.12.1",
23+
"@types/json5": "^0.0.30",
2624
"@types/node": "^12.6.8",
2725
"@typescript-eslint/eslint-plugin": "^1.13.0",
2826
"@typescript-eslint/parser": "^1.13.0",
@@ -43,6 +41,11 @@
4341
"engines": {
4442
"node": ">= 8"
4543
},
44+
"files": [
45+
"types",
46+
"lib",
47+
"src"
48+
],
4649
"homepage": "https://github.com/kazupon/vue-i18n-locale-message#readme",
4750
"keywords": [
4851
"i18n",
@@ -59,14 +62,15 @@
5962
},
6063
"scripts": {
6164
"build": "tsc -p .",
62-
"test:cover": "npm run test:unit -- --coverage",
63-
"test:unit": "jest --env node",
64-
"watch": "tsc -p . --watch",
65+
"changelog": "conventional-changelog -i CHANGELOG.md -s -n ./node_modules/git-commit-message-convention/convention.js",
66+
"clean": "rm -rf ./coverage && rm -rf ./lib/*.js*",
67+
"coverage": "opener coverage/lcov-report/index.html",
6568
"lint": "eslint ./src ./test --ext .ts",
6669
"release": "conventional-github-releaser -n ./node_modules/git-commit-message-convention/convention.js",
6770
"test": "npm run lint && npm run test:cover",
68-
"changelog": "conventional-changelog -i CHANGELOG.md -s -n ./node_modules/git-commit-message-convention/convention.js",
69-
"clean": "rm -rf ./coverage && rm -rf ./lib/*.js*",
70-
"coverage": "opener coverage/lcov-report/index.html"
71-
}
71+
"test:cover": "npm run test:unit -- --coverage",
72+
"test:unit": "jest --env node",
73+
"watch": "tsc -p . --watch"
74+
},
75+
"types": "types/index.d.ts"
7276
}

src/squeezer.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { VueTemplateCompiler } from '@vue/component-compiler-utils/dist/types'
33

44
import { parse } from '@vue/component-compiler-utils'
55
import * as compiler from 'vue-template-compiler'
6+
import JSON5 from 'json5'
7+
import yaml from 'js-yaml'
68

79
import { debug as Debug } from 'debug'
810
const debug = Debug('vue-i18n-locale-messages:squeezer')
@@ -37,8 +39,10 @@ function squeezeFromI18nBlock (content: string): LocaleMessages {
3739
})
3840

3941
return desc.customBlocks.reduce((messages, block) => {
42+
debug('i18n block attrs', block.attrs)
4043
if (block.type === 'i18n') {
41-
const obj = JSON.parse(block.content)
44+
const lang = block.attrs.lang as string || 'json'
45+
const obj = parseContent(block.content, lang)
4246
if (block.attrs.locale) {
4347
return Object.assign(messages, { [block.attrs.locale as string]: obj })
4448
} else {
@@ -49,3 +53,16 @@ function squeezeFromI18nBlock (content: string): LocaleMessages {
4953
}
5054
}, {})
5155
}
56+
57+
function parseContent (content: string, lang: string): any {
58+
switch (lang) {
59+
case 'yaml':
60+
case 'yml':
61+
return yaml.safeLoad(content)
62+
case 'json5':
63+
return JSON5.parse(content)
64+
case 'json':
65+
default:
66+
return JSON.parse(content)
67+
}
68+
}

test/__snapshots__/squeezer.test.ts.snap

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`squeeze 1`] = `
3+
exports[`basic 1`] = `
44
Object {
55
"en": Object {
66
"App": Object {
@@ -45,3 +45,45 @@ Object {
4545
},
4646
}
4747
`;
48+
49+
exports[`json5 1`] = `
50+
Object {
51+
"en": Object {
52+
"Modal": Object {
53+
"components": Object {
54+
"cancel": "Cancel",
55+
"ok": "OK",
56+
},
57+
},
58+
},
59+
"ja": Object {
60+
"Modal": Object {
61+
"components": Object {
62+
"cancel": "キャンセル",
63+
"ok": "OK",
64+
},
65+
},
66+
},
67+
}
68+
`;
69+
70+
exports[`yaml 1`] = `
71+
Object {
72+
"en": Object {
73+
"Modal": Object {
74+
"components": Object {
75+
"cancel": "Cancel",
76+
"ok": "OK",
77+
},
78+
},
79+
},
80+
"ja": Object {
81+
"Modal": Object {
82+
"components": Object {
83+
"cancel": "キャンセル",
84+
"ok": "OK",
85+
},
86+
},
87+
},
88+
}
89+
`;

test/fixtures/format/json5.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default [{
2+
contentPath: '/path/to/project1/src/components/Modal.vue',
3+
content: `
4+
<i18n locale="en" lang="json5">
5+
{
6+
// modal contents
7+
"ok": "OK",
8+
cancel: "Cancel"
9+
}
10+
</i18n>
11+
<i18n locale="ja">
12+
{
13+
"ok": "OK",
14+
"cancel": "キャンセル"
15+
}
16+
</i18n>
17+
`,
18+
component: 'Modal',
19+
messageHierarchy: ['components', 'Modal']
20+
}]

test/fixtures/format/yaml.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default [{
2+
contentPath: '/path/to/project1/src/components/Modal.vue',
3+
content: `
4+
<i18n locale="en" lang="yaml">
5+
ok: "OK"
6+
cancel: "Cancel"
7+
</i18n>
8+
<i18n locale="ja" lang="yml">
9+
ok: OK
10+
cancel: キャンセル
11+
</i18n>
12+
`,
13+
component: 'Modal',
14+
messageHierarchy: ['components', 'Modal']
15+
}]

test/squeezer.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import squeeze from '../src/squeezer'
22
import metaInfo from './fixtures/meta'
3+
import yamlMetaInfo from './fixtures/format/yaml'
4+
import json5MetaInfo from './fixtures/format/json5'
35
import { LocaleMessages, LocaleMessageMeta } from '../types'
46

5-
test('squeeze', () => {
7+
test('basic', () => {
68
const mesasges: LocaleMessages = squeeze(metaInfo as LocaleMessageMeta[])
79
expect(mesasges).toMatchSnapshot()
810
})
11+
12+
test('yaml', () => {
13+
const mesasges: LocaleMessages = squeeze(yamlMetaInfo as LocaleMessageMeta[])
14+
expect(mesasges).toMatchSnapshot()
15+
})
16+
17+
test('json5', () => {
18+
const mesasges: LocaleMessages = squeeze(json5MetaInfo as LocaleMessageMeta[])
19+
expect(mesasges).toMatchSnapshot()
20+
})

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,21 @@
365365
dependencies:
366366
"@types/jest-diff" "*"
367367

368+
"@types/js-yaml@^3.12.1":
369+
version "3.12.1"
370+
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.12.1.tgz#5c6f4a1eabca84792fbd916f0cb40847f123c656"
371+
integrity sha512-SGGAhXLHDx+PK4YLNcNGa6goPf9XRWQNAUUbffkwVGGXIxmDKWyGGL4inzq2sPmExu431Ekb9aEMn9BkPqEYFA==
372+
368373
"@types/json-schema@^7.0.3":
369374
version "7.0.3"
370375
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"
371376
integrity sha512-Il2DtDVRGDcqjDtE+rF8iqg1CArehSK84HZJCT7AMITlyXRBpuPhqGLDQMowraqqu1coEaimg4ZOqggt6L6L+A==
372377

378+
"@types/json5@^0.0.30":
379+
version "0.0.30"
380+
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.30.tgz#44cb52f32a809734ca562e685c6473b5754a7818"
381+
integrity sha512-sqm9g7mHlPY/43fcSNrCYfOeX9zkTTK+euO5E6+CVijSMm5tTjkVdwdqRkY3ljjIAf8679vps5jKUoJBCLsMDA==
382+
373383
"@types/node@^12.6.8":
374384
version "12.6.8"
375385
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.6.8.tgz#e469b4bf9d1c9832aee4907ba8a051494357c12c"

0 commit comments

Comments
 (0)