@hi18n/cli
Version:
Message internationalization meets immutability and type-safety - command line tool
69 lines (56 loc) • 1.88 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.connector = connector;
var _nodeFs = _interopRequireDefault(require("node:fs"));
var _nodePath = _interopRequireDefault(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 = _nodePath.default.resolve(_nodePath.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 _nodeFs.default.promises.writeFile(jsonPath, json, "utf-8");
}
async function importData() {
const json = await _nodeFs.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