diff --git a/lib/infuser.js b/lib/infuser.js index 2050507..85aaaaa 100644 --- a/lib/infuser.js +++ b/lib/infuser.js @@ -3,23 +3,23 @@ Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./utils"); const debug_1 = require("debug"); const debug = debug_1.debug('vue-i18n-locale-message:infuser'); -function infuse(basePath, sources, meta) { +function infuse(basePath, sources, meta, options) { const descriptors = utils_1.reflectSFCDescriptor(basePath, sources); return descriptors.map(descriptor => { return { - content: generate(meta, descriptor), + content: generate(meta, descriptor, options), path: descriptor.contentPath }; }); } exports.default = infuse; -function generate(meta, descriptor) { +function generate(meta, descriptor, options) { const i18nBlocks = meta.components[descriptor.contentPath]; debug('target i18n blocks\n', i18nBlocks); const blocks = getBlocks(descriptor); blocks.forEach(b => debug(`block: type=${b.type}, start=${b.start}, end=${b.end}`)); const { raw } = descriptor; - const content = buildContent(i18nBlocks, raw, blocks); + const content = buildContent(i18nBlocks, raw, blocks, options); debug(`build content:\n${content}`); debug(`content size: raw=${raw.length}, content=${content.length}`); return content; @@ -32,7 +32,7 @@ function getBlocks(descriptor) { blocks.sort((a, b) => { return a.start - b.start; }); return blocks; } -function buildContent(i18nBlocks, raw, blocks) { +function buildContent(i18nBlocks, raw, blocks, options) { let offset = 0; let i18nBlockCounter = 0; let contents = []; @@ -57,7 +57,7 @@ function buildContent(i18nBlocks, raw, blocks) { messages = utils_1.parseContent(block.content, lang); } contents = contents.concat(raw.slice(offset, block.start)); - const serialized = `\n${utils_1.stringifyContent(messages, lang)}`; + const serialized = `\n${utils_1.stringifyContent(messages, lang, options)}`; contents = contents.concat(serialized); offset = block.end; i18nBlockCounter++; @@ -71,13 +71,13 @@ function buildContent(i18nBlocks, raw, blocks) { contents = contents.concat(raw.slice(offset, raw.length)); if (i18nBlocks.length > i18nBlockCounter) { i18nBlocks.slice(i18nBlockCounter).reduce((contents, i18nBlock) => { - contents.push(buildI18nTag(i18nBlock)); + contents.push(buildI18nTag(i18nBlock, options)); return contents; }, contents); } return contents.join(''); } -function buildI18nTag(i18nBlock) { +function buildI18nTag(i18nBlock, options) { const { locale, lang, messages } = i18nBlock; let tag = '`; +${utils_1.stringifyContent(locale ? messages[locale] : messages, lang, options)}`; } diff --git a/lib/utils.js b/lib/utils.js index abc3cf9..df4a962 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -77,17 +77,28 @@ function parseContent(content, lang) { } } exports.parseContent = parseContent; -function stringifyContent(content, lang) { +function stringifyContent(content, lang, options) { + var _a, _b; + const indent = ((_a = options) === null || _a === void 0 ? void 0 : _a.intend) || 2; + const eof = ((_b = options) === null || _b === void 0 ? void 0 : _b.eof) || '\n'; + let result = ''; switch (lang) { case 'yaml': case 'yml': - return js_yaml_1.default.safeDump(content, { indent: 2 }); + result = js_yaml_1.default.safeDump(content, { indent }); + break; case 'json5': - return json5_1.default.stringify(content, null, 2); + result = json5_1.default.stringify(content, null, indent); + break; case 'json': default: - return JSON.stringify(content, null, 2); + result = JSON.stringify(content, null, indent); + break; } + if (!result.endsWith(eof)) { + result += eof; + } + return result; } exports.stringifyContent = stringifyContent; function readSFC(target) {