UNPKG

@criticalmanufacturing/dev-i18n-transform

Version:
112 lines 4.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const os = require("os"); const util_1 = require("../util"); const translation_1 = require("../model/translation"); const index_1 = require("../logger/index"); const beautify = require("js-beautify"); class TypescriptWriter { constructor(pack, language) { this._package = pack; this._language = language; this._util = new util_1.Util(); } writeFile(file) { let fileName = file.translatedFileName(this._language); // Join the references in the header // Ex: import i18nControls from "cmf.core.controls/src/i18n/main.default"; let referenceBuffer = null; if (Array.isArray(file.references) && file.references.length > 0) { referenceBuffer = new Buffer(file.references.join(os.EOL) + os.EOL + os.EOL); } else { referenceBuffer = new Buffer(""); } // Build the object to print. If literal don't use "" let literal = {}; file.messages.map((msg) => { let translation = msg.getTranslation(this._language); if (translation == null) { index_1.default.warn(`Translation not found for ${msg.id} (language: ${this._language})`); } else if (translation.text != null && typeof translation.text === "string" && translation.text.length > 0) { this._util.setNestedPropertyByArray(literal, msg.id.split("."), translation, true); } }); let outputText = `export default ${this.stringifyTS(literal)};`; outputText = beautify(outputText, { indent_size: 4 }); // Give an extra line in the end of the file outputText += os.EOL; return { file: fileName, content: Buffer.concat([referenceBuffer, new Buffer(outputText)]) }; } isTemplateString(text) { return text.indexOf("${") >= 0; } stringifyTS(obj) { let arrOfKeyVal = [], objKeys = []; // Handle leafs if (obj instanceof translation_1.Translation) { if (obj.isLiteral === true) { return obj.text; } else { return this.isTemplateString(obj.text) ? `\`${obj.text}\`` : `"${obj.text}"`; } } /*********CHECK FOR ARRAY**********/ else if (Array.isArray(obj)) { // Check for empty array if (obj.length === 0) return "[]"; else { let arrVal = []; obj.forEach((el) => { arrVal.push(this.stringifyTS(el)); }); return `[${arrVal}]`; } } /*********CHECK FOR OBJECT**********/ else if (obj instanceof Object) { // Get object keys objKeys = Object.keys(obj); // Set key output; objKeys.forEach((key) => { let keyOut = `${key}`; let keyValOut = obj[key]; // Skip functions and undefined properties if (keyValOut instanceof Function || typeof keyValOut === undefined) { arrOfKeyVal.push(""); } else if (typeof keyValOut === "string") { keyValOut = this.isTemplateString(keyValOut) ? `\`${keyValOut}\`` : `"${keyValOut}"`; arrOfKeyVal.push(`${keyOut}: ${keyValOut}`); } else if (typeof keyValOut === "boolean" || typeof keyValOut === "number" || keyValOut === null) { arrOfKeyVal.push(`${keyOut}: ${keyValOut}`); // Check for nested objects, call recursively until no more objects } else if (keyValOut instanceof Object) { arrOfKeyVal.push(`${keyOut}: ${this.stringifyTS(keyValOut)}`); } }); return `{${arrOfKeyVal}}`; } throw new Error("Not Implemented"); } run() { // A package may have multiple files (most of the times it does) // So, we need to run through all files and, for each, create a correspondent // Typescript file return this._package.files.map((file) => { return this.writeFile(file); }); } } exports.TypescriptWriter = TypescriptWriter; //# sourceMappingURL=typescript.writer.js.map