From d4885cf1be1383dee2c03360a3763fb9cfefa953 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Thu, 27 May 2021 22:54:34 +0200 Subject: [PATCH 1/4] rename locale-import tests to src-import --- test/__snapshots__/process.test.ts.snap | 6 +++--- .../{json-locale-import.vue => json-src-import.vue} | 0 .../{json5-locale-import.vue => json5-src-import.vue} | 0 .../{yaml-locale-import.vue => yaml-src-import.vue} | 0 test/process.test.ts | 6 +++--- 5 files changed, 6 insertions(+), 6 deletions(-) rename test/fixtures/{json-locale-import.vue => json-src-import.vue} (100%) rename test/fixtures/{json5-locale-import.vue => json5-src-import.vue} (100%) rename test/fixtures/{yaml-locale-import.vue => yaml-src-import.vue} (100%) diff --git a/test/__snapshots__/process.test.ts.snap b/test/__snapshots__/process.test.ts.snap index 7a3baf3..b45e333 100644 --- a/test/__snapshots__/process.test.ts.snap +++ b/test/__snapshots__/process.test.ts.snap @@ -12,7 +12,7 @@ exports[`process (json.vue) returns expected string: json.vue 1`] = ` ]" `; -exports[`process (json-locale-import.vue) returns expected string: json-locale-import.vue 1`] = ` +exports[`process (json-src-import.vue) returns expected string: json-src-import.vue 1`] = ` "__vue__options__.__i18n = [ '{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}' ]" @@ -24,7 +24,7 @@ exports[`process (json5.vue) returns expected string: json5.vue 1`] = ` ]" `; -exports[`process (json5-locale-import.vue) returns expected string: json5-locale-import.vue 1`] = ` +exports[`process (json5-src-import.vue) returns expected string: json5-src-import.vue 1`] = ` "__vue__options__.__i18n = [ '{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}' ]" @@ -36,7 +36,7 @@ exports[`process (yaml.vue) returns expected string: yaml.vue 1`] = ` ]" `; -exports[`process (yaml-locale-import.vue) returns expected string: yaml-locale-import.vue 1`] = ` +exports[`process (yaml-src-import.vue) returns expected string: yaml-src-import.vue 1`] = ` "__vue__options__.__i18n = [ '{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}' ]" diff --git a/test/fixtures/json-locale-import.vue b/test/fixtures/json-src-import.vue similarity index 100% rename from test/fixtures/json-locale-import.vue rename to test/fixtures/json-src-import.vue diff --git a/test/fixtures/json5-locale-import.vue b/test/fixtures/json5-src-import.vue similarity index 100% rename from test/fixtures/json5-locale-import.vue rename to test/fixtures/json5-src-import.vue diff --git a/test/fixtures/yaml-locale-import.vue b/test/fixtures/yaml-src-import.vue similarity index 100% rename from test/fixtures/yaml-locale-import.vue rename to test/fixtures/yaml-src-import.vue diff --git a/test/process.test.ts b/test/process.test.ts index e71f9bb..94cf3e2 100644 --- a/test/process.test.ts +++ b/test/process.test.ts @@ -7,11 +7,11 @@ import process from '../src/process' const files = [ 'default.vue', 'json.vue', - 'json-locale-import.vue', + 'json-src-import.vue', 'yaml.vue', - 'yaml-locale-import.vue', + 'yaml-src-import.vue', 'json5.vue', - 'json5-locale-import.vue' + 'json5-src-import.vue' ] describe('process', () => { From 25818d69216a41fcf26642ebbee55da026d85cac Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Thu, 27 May 2021 22:55:59 +0200 Subject: [PATCH 2/4] coverage: extend test with other custom block, which should be ignored --- test/fixtures/default.vue | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/fixtures/default.vue b/test/fixtures/default.vue index 69710e3..cfa3d0b 100644 --- a/test/fixtures/default.vue +++ b/test/fixtures/default.vue @@ -16,4 +16,8 @@ export default { "hello": "hello!" } } - \ No newline at end of file + + + +should be ignored + From 67f1aea9bb06026129d14a8a1ced0058774354e8 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Thu, 27 May 2021 22:57:37 +0200 Subject: [PATCH 3/4] coverage: component without i18n block should set empty string --- test/__snapshots__/process.test.ts.snap | 2 ++ test/fixtures/no-i18n.vue | 8 ++++++++ test/process.test.ts | 1 + 3 files changed, 11 insertions(+) create mode 100644 test/fixtures/no-i18n.vue diff --git a/test/__snapshots__/process.test.ts.snap b/test/__snapshots__/process.test.ts.snap index b45e333..162d256 100644 --- a/test/__snapshots__/process.test.ts.snap +++ b/test/__snapshots__/process.test.ts.snap @@ -30,6 +30,8 @@ exports[`process (json5-src-import.vue) returns expected string: json5-src-impor ]" `; +exports[`process (no-i18n.vue) returns expected string: no-i18n.vue 1`] = `""`; + exports[`process (yaml.vue) returns expected string: yaml.vue 1`] = ` "__vue__options__.__i18n = [ '{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}' diff --git a/test/fixtures/no-i18n.vue b/test/fixtures/no-i18n.vue new file mode 100644 index 0000000..e5fd09b --- /dev/null +++ b/test/fixtures/no-i18n.vue @@ -0,0 +1,8 @@ + + + diff --git a/test/process.test.ts b/test/process.test.ts index 94cf3e2..21085ec 100644 --- a/test/process.test.ts +++ b/test/process.test.ts @@ -5,6 +5,7 @@ import { parseComponent } from 'vue-template-compiler' import process from '../src/process' const files = [ + 'no-i18n.vue', 'default.vue', 'json.vue', 'json-src-import.vue', From 093283c5f965ce07f20b33885a3e0a3b74dd3fa7 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Thu, 27 May 2021 23:17:21 +0200 Subject: [PATCH 4/4] feat: support locale attribute on i18n blocks --- src/process.ts | 16 +++++++++++----- test/__snapshots__/process.test.ts.snap | 21 +++++++++++++++++++++ test/fixtures/json-locale.vue | 20 ++++++++++++++++++++ test/fixtures/json5-locale.vue | 20 ++++++++++++++++++++ test/fixtures/yaml-locale.vue | 17 +++++++++++++++++ test/process.test.ts | 5 ++++- 6 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/json-locale.vue create mode 100644 test/fixtures/json5-locale.vue create mode 100644 test/fixtures/yaml-locale.vue diff --git a/src/process.ts b/src/process.ts index 0d3a942..7869cd1 100644 --- a/src/process.ts +++ b/src/process.ts @@ -53,29 +53,35 @@ export default function process ({ blocks, vueOptionsNamespace, filename }: Proc */ function parseI18nBlockToJSON (block: SFCCustomBlock, filename: string): string { const lang = block.attrs && block.attrs.lang + const locale = block.attrs && block.attrs.locale const src = block.attrs && block.attrs.src const content = src ? readFileSync(getAbsolutePath(src, filename)).toString() : block.content - return convertToJSON(content, lang) + return convertToJSON(content, lang, locale) } /** * Convert JSON/YAML/JSON5 to minified JSON string. * @param source JSON/YAML/JSON5 encoded string * @param lang Language used in `source`. Supported JSON, YAML or JSON5. + * @param locale Attribute "locale" on block will be added. * @returns {string} A minified JSON string */ -function convertToJSON (source: string, lang: string): string { +function convertToJSON (source: string, lang: string, locale: string): string { + const stringify = locale + ? (parseResult: any) => JSON.stringify({ [locale]: parseResult }) + : JSON.stringify + switch (lang) { case 'yaml': case 'yml': - return JSON.stringify(parseYAML(source)) + return stringify(parseYAML(source)) case 'json5': - return JSON.stringify(parseJSON5(source)) + return stringify(parseJSON5(source)) default: // fallback to 'json' - return JSON.stringify(JSON.parse(source)) + return stringify(JSON.parse(source)) } } diff --git a/test/__snapshots__/process.test.ts.snap b/test/__snapshots__/process.test.ts.snap index 162d256..bca3444 100644 --- a/test/__snapshots__/process.test.ts.snap +++ b/test/__snapshots__/process.test.ts.snap @@ -12,6 +12,13 @@ exports[`process (json.vue) returns expected string: json.vue 1`] = ` ]" `; +exports[`process (json-locale.vue) returns expected string: json-locale.vue 1`] = ` +"__vue__options__.__i18n = [ +'{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"}}', +'{\\"en\\":{\\"hello\\":\\"hello!\\"}}' +]" +`; + exports[`process (json-src-import.vue) returns expected string: json-src-import.vue 1`] = ` "__vue__options__.__i18n = [ '{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}' @@ -24,6 +31,13 @@ exports[`process (json5.vue) returns expected string: json5.vue 1`] = ` ]" `; +exports[`process (json5-locale.vue) returns expected string: json5-locale.vue 1`] = ` +"__vue__options__.__i18n = [ +'{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"}}', +'{\\"en\\":{\\"hello\\":\\"hello!\\"}}' +]" +`; + exports[`process (json5-src-import.vue) returns expected string: json5-src-import.vue 1`] = ` "__vue__options__.__i18n = [ '{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}' @@ -38,6 +52,13 @@ exports[`process (yaml.vue) returns expected string: yaml.vue 1`] = ` ]" `; +exports[`process (yaml-locale.vue) returns expected string: yaml-locale.vue 1`] = ` +"__vue__options__.__i18n = [ +'{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"}}', +'{\\"en\\":{\\"hello\\":\\"hello!\\"}}' +]" +`; + exports[`process (yaml-src-import.vue) returns expected string: yaml-src-import.vue 1`] = ` "__vue__options__.__i18n = [ '{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}' diff --git a/test/fixtures/json-locale.vue b/test/fixtures/json-locale.vue new file mode 100644 index 0000000..e0119c3 --- /dev/null +++ b/test/fixtures/json-locale.vue @@ -0,0 +1,20 @@ + + + + + +{ + "hello": "こんにちは!" +} + + + +{ + "hello": "hello!" +} + diff --git a/test/fixtures/json5-locale.vue b/test/fixtures/json5-locale.vue new file mode 100644 index 0000000..1edfd26 --- /dev/null +++ b/test/fixtures/json5-locale.vue @@ -0,0 +1,20 @@ + + + + + +{ + hello: "こんにちは!" +} + + + +{ + "hello": "hello!" +} + diff --git a/test/fixtures/yaml-locale.vue b/test/fixtures/yaml-locale.vue new file mode 100644 index 0000000..3e59f57 --- /dev/null +++ b/test/fixtures/yaml-locale.vue @@ -0,0 +1,17 @@ + + + + + +hello: こんにちは! + + + +hello: hello! + + diff --git a/test/process.test.ts b/test/process.test.ts index 21085ec..ce1f89a 100644 --- a/test/process.test.ts +++ b/test/process.test.ts @@ -9,10 +9,13 @@ const files = [ 'default.vue', 'json.vue', 'json-src-import.vue', + 'json-locale.vue', 'yaml.vue', 'yaml-src-import.vue', + 'yaml-locale.vue', 'json5.vue', - 'json5-src-import.vue' + 'json5-src-import.vue', + 'json5-locale.vue' ] describe('process', () => {