Skip to content

Commit

Permalink
⭐ new(api): support yaml and json5 format
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Jul 29, 2019
1 parent 9f4c47c commit 5e21036
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 16 deletions.
30 changes: 17 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@
"bugs": {
"url": "https://github.com/kazupon/vue-i18n-locale-message/issues"
},
"types": "types/index.d.ts",
"files": [
"types",
"lib",
"src"
],
"dependencies": {
"@vue/component-compiler-utils": "^3.0.0",
"debug": "^4.1.1",
"js-yaml": "^3.13.1",
"json5": "^2.1.0",
"vue-template-compiler": "^2.6.10"
},
"devDependencies": {
"@types/debug": "^4.1.4",
"@types/jest": "^24.0.15",
"@types/js-yaml": "^3.12.1",
"@types/json5": "^0.0.30",
"@types/node": "^12.6.8",
"@typescript-eslint/eslint-plugin": "^1.13.0",
"@typescript-eslint/parser": "^1.13.0",
Expand All @@ -43,6 +41,11 @@
"engines": {
"node": ">= 8"
},
"files": [
"types",
"lib",
"src"
],
"homepage": "https://github.com/kazupon/vue-i18n-locale-message#readme",
"keywords": [
"i18n",
Expand All @@ -59,14 +62,15 @@
},
"scripts": {
"build": "tsc -p .",
"test:cover": "npm run test:unit -- --coverage",
"test:unit": "jest --env node",
"watch": "tsc -p . --watch",
"changelog": "conventional-changelog -i CHANGELOG.md -s -n ./node_modules/git-commit-message-convention/convention.js",
"clean": "rm -rf ./coverage && rm -rf ./lib/*.js*",
"coverage": "opener coverage/lcov-report/index.html",
"lint": "eslint ./src ./test --ext .ts",
"release": "conventional-github-releaser -n ./node_modules/git-commit-message-convention/convention.js",
"test": "npm run lint && npm run test:cover",
"changelog": "conventional-changelog -i CHANGELOG.md -s -n ./node_modules/git-commit-message-convention/convention.js",
"clean": "rm -rf ./coverage && rm -rf ./lib/*.js*",
"coverage": "opener coverage/lcov-report/index.html"
}
"test:cover": "npm run test:unit -- --coverage",
"test:unit": "jest --env node",
"watch": "tsc -p . --watch"
},
"types": "types/index.d.ts"
}
19 changes: 18 additions & 1 deletion src/squeezer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { VueTemplateCompiler } from '@vue/component-compiler-utils/dist/types'

import { parse } from '@vue/component-compiler-utils'
import * as compiler from 'vue-template-compiler'
import JSON5 from 'json5'
import yaml from 'js-yaml'

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

return desc.customBlocks.reduce((messages, block) => {
debug('i18n block attrs', block.attrs)
if (block.type === 'i18n') {
const obj = JSON.parse(block.content)
const lang = block.attrs.lang as string || 'json'
const obj = parseContent(block.content, lang)
if (block.attrs.locale) {
return Object.assign(messages, { [block.attrs.locale as string]: obj })
} else {
Expand All @@ -49,3 +53,16 @@ function squeezeFromI18nBlock (content: string): LocaleMessages {
}
}, {})
}

function parseContent (content: string, lang: string): any {
switch (lang) {
case 'yaml':
case 'yml':
return yaml.safeLoad(content)
case 'json5':
return JSON5.parse(content)
case 'json':
default:
return JSON.parse(content)
}
}
44 changes: 43 additions & 1 deletion test/__snapshots__/squeezer.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`squeeze 1`] = `
exports[`basic 1`] = `
Object {
"en": Object {
"App": Object {
Expand Down Expand Up @@ -45,3 +45,45 @@ Object {
},
}
`;

exports[`json5 1`] = `
Object {
"en": Object {
"Modal": Object {
"components": Object {
"cancel": "Cancel",
"ok": "OK",
},
},
},
"ja": Object {
"Modal": Object {
"components": Object {
"cancel": "キャンセル",
"ok": "OK",
},
},
},
}
`;

exports[`yaml 1`] = `
Object {
"en": Object {
"Modal": Object {
"components": Object {
"cancel": "Cancel",
"ok": "OK",
},
},
},
"ja": Object {
"Modal": Object {
"components": Object {
"cancel": "キャンセル",
"ok": "OK",
},
},
},
}
`;
20 changes: 20 additions & 0 deletions test/fixtures/format/json5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default [{
contentPath: '/path/to/project1/src/components/Modal.vue',
content: `
<i18n locale="en" lang="json5">
{
// modal contents
"ok": "OK",
cancel: "Cancel"
}
</i18n>
<i18n locale="ja">
{
"ok": "OK",
"cancel": "キャンセル"
}
</i18n>
`,
component: 'Modal',
messageHierarchy: ['components', 'Modal']
}]
15 changes: 15 additions & 0 deletions test/fixtures/format/yaml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default [{
contentPath: '/path/to/project1/src/components/Modal.vue',
content: `
<i18n locale="en" lang="yaml">
ok: "OK"
cancel: "Cancel"
</i18n>
<i18n locale="ja" lang="yml">
ok: OK
cancel: キャンセル
</i18n>
`,
component: 'Modal',
messageHierarchy: ['components', 'Modal']
}]
14 changes: 13 additions & 1 deletion test/squeezer.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import squeeze from '../src/squeezer'
import metaInfo from './fixtures/meta'
import yamlMetaInfo from './fixtures/format/yaml'
import json5MetaInfo from './fixtures/format/json5'
import { LocaleMessages, LocaleMessageMeta } from '../types'

test('squeeze', () => {
test('basic', () => {
const mesasges: LocaleMessages = squeeze(metaInfo as LocaleMessageMeta[])
expect(mesasges).toMatchSnapshot()
})

test('yaml', () => {
const mesasges: LocaleMessages = squeeze(yamlMetaInfo as LocaleMessageMeta[])
expect(mesasges).toMatchSnapshot()
})

test('json5', () => {
const mesasges: LocaleMessages = squeeze(json5MetaInfo as LocaleMessageMeta[])
expect(mesasges).toMatchSnapshot()
})
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,21 @@
dependencies:
"@types/jest-diff" "*"

"@types/js-yaml@^3.12.1":
version "3.12.1"
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.12.1.tgz#5c6f4a1eabca84792fbd916f0cb40847f123c656"
integrity sha512-SGGAhXLHDx+PK4YLNcNGa6goPf9XRWQNAUUbffkwVGGXIxmDKWyGGL4inzq2sPmExu431Ekb9aEMn9BkPqEYFA==

"@types/json-schema@^7.0.3":
version "7.0.3"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"
integrity sha512-Il2DtDVRGDcqjDtE+rF8iqg1CArehSK84HZJCT7AMITlyXRBpuPhqGLDQMowraqqu1coEaimg4ZOqggt6L6L+A==

"@types/json5@^0.0.30":
version "0.0.30"
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.30.tgz#44cb52f32a809734ca562e685c6473b5754a7818"
integrity sha512-sqm9g7mHlPY/43fcSNrCYfOeX9zkTTK+euO5E6+CVijSMm5tTjkVdwdqRkY3ljjIAf8679vps5jKUoJBCLsMDA==

"@types/node@^12.6.8":
version "12.6.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.6.8.tgz#e469b4bf9d1c9832aee4907ba8a051494357c12c"
Expand Down

0 comments on commit 5e21036

Please sign in to comment.