diff --git a/package.json b/package.json index 57ea27f..b3e26f5 100644 --- a/package.json +++ b/package.json @@ -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", @@ -43,6 +41,11 @@ "engines": { "node": ">= 8" }, + "files": [ + "types", + "lib", + "src" + ], "homepage": "https://github.com/kazupon/vue-i18n-locale-message#readme", "keywords": [ "i18n", @@ -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" } diff --git a/src/squeezer.ts b/src/squeezer.ts index 8820d40..2e059c0 100644 --- a/src/squeezer.ts +++ b/src/squeezer.ts @@ -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') @@ -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 { @@ -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) + } +} diff --git a/test/__snapshots__/squeezer.test.ts.snap b/test/__snapshots__/squeezer.test.ts.snap index 217ecd9..95b4954 100644 --- a/test/__snapshots__/squeezer.test.ts.snap +++ b/test/__snapshots__/squeezer.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`squeeze 1`] = ` +exports[`basic 1`] = ` Object { "en": Object { "App": Object { @@ -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", + }, + }, + }, +} +`; diff --git a/test/fixtures/format/json5.ts b/test/fixtures/format/json5.ts new file mode 100644 index 0000000..2be4143 --- /dev/null +++ b/test/fixtures/format/json5.ts @@ -0,0 +1,20 @@ +export default [{ + contentPath: '/path/to/project1/src/components/Modal.vue', + content: ` + + { + // modal contents + "ok": "OK", + cancel: "Cancel" + } + + + { + "ok": "OK", + "cancel": "キャンセル" + } + + `, + component: 'Modal', + messageHierarchy: ['components', 'Modal'] +}] diff --git a/test/fixtures/format/yaml.ts b/test/fixtures/format/yaml.ts new file mode 100644 index 0000000..9e16eb9 --- /dev/null +++ b/test/fixtures/format/yaml.ts @@ -0,0 +1,15 @@ +export default [{ + contentPath: '/path/to/project1/src/components/Modal.vue', + content: ` + + ok: "OK" + cancel: "Cancel" + + + ok: OK + cancel: キャンセル + + `, + component: 'Modal', + messageHierarchy: ['components', 'Modal'] +}] diff --git a/test/squeezer.test.ts b/test/squeezer.test.ts index 9f02fe2..c43fd5a 100644 --- a/test/squeezer.test.ts +++ b/test/squeezer.test.ts @@ -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() +}) diff --git a/yarn.lock b/yarn.lock index 808b1b6..07c8d30 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"