UNPKG

@hi18n/cli

Version:

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

43 lines 1.7 kB
import fs from "node:fs"; import path from "node:path"; export function connector(configPath, params) { const { path: relativePath } = params; if (typeof relativePath !== "string") { throw new Error("connectorOptions.path is not a string"); } const jsonPath = path.resolve(path.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 fs.promises.writeFile(jsonPath, json, "utf-8"); } async function importData() { const json = await fs.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