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 7a3baf3..bca3444 100644 --- a/test/__snapshots__/process.test.ts.snap +++ b/test/__snapshots__/process.test.ts.snap @@ -12,7 +12,14 @@ 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-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,19 +31,35 @@ 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-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!\\"}}' ]" `; +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!\\"}}' ]" `; -exports[`process (yaml-locale-import.vue) returns expected string: yaml-locale-import.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/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 + 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/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.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/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/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/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/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..ce1f89a 100644 --- a/test/process.test.ts +++ b/test/process.test.ts @@ -5,13 +5,17 @@ import { parseComponent } from 'vue-template-compiler' import process from '../src/process' const files = [ + 'no-i18n.vue', 'default.vue', 'json.vue', - 'json-locale-import.vue', + 'json-src-import.vue', + 'json-locale.vue', 'yaml.vue', - 'yaml-locale-import.vue', + 'yaml-src-import.vue', + 'yaml-locale.vue', 'json5.vue', - 'json5-locale-import.vue' + 'json5-src-import.vue', + 'json5-locale.vue' ] describe('process', () => {