UNPKG

@hi18n/cli

Version:

Message internationalization meets immutability and type-safety - command line tool

49 lines 2.06 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.connector = connector; const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); function connector(configPath, params) { const { path: relativePath } = params; if (typeof relativePath !== "string") { throw new Error("connectorOptions.path is not a string"); } const jsonPath = node_path_1.default.resolve(node_path_1.default.dirname(configPath), relativePath); async function exportData(data) { let json = "{\n"; const locales = Object.keys(data.translations).sort(); locales.forEach((locale, i) => { const catalog = data.translations[locale]; json += ` ${JSON.stringify(locale)}: {\n`; const ids = Object.keys(catalog).sort(); ids.forEach((id, j) => { const msg = catalog[id]; const comma = j + 1 === ids.length ? "" : ","; json += ` ${JSON.stringify(id)}: ${JSON.stringify(msg.raw)}${comma}\n`; }); const comma = i + 1 === locales.length ? "" : ","; json += ` }${comma}\n`; }); json += "}\n"; await node_fs_1.default.promises.writeFile(jsonPath, json, "utf-8"); } async function importData() { const json = await node_fs_1.default.promises.readFile(jsonPath, "utf-8"); const obj = JSON.parse(json); const translations = {}; for (const [locale, catalogObj] of Object.entries(obj)) { const catalog = (translations[locale] = {}); for (const [id, msg] of Object.entries(catalogObj)) { catalog[id] = { raw: msg }; } } return { translations, }; } return { exportData, importData }; } //# sourceMappingURL=json-mf-connector.js.map