UNPKG

@sap-ux/i18n

Version:

Library for i18n

47 lines 1.26 kB
import { parseCsv } from '../../parser/csv/parser/index.js'; /** * Convert csv field to text node. * * @param field csv field * @returns text node or undefined */ function toTextNode(field) { if (!field) { return undefined; } return { value: field.value, range: field.range }; } /** * Convert CSV content to i18n bundles. * * @param text csv text * @param filePath file path of csv text * @returns i18n bundles */ export function csvToI18nBundle(text, filePath = '') { const bundle = {}; const { ast } = parseCsv(text); for (let columnIndex = 1; columnIndex < ast.header.fields.length; columnIndex++) { const locale = ast.header.fields[columnIndex]; const entries = []; for (const row of ast.rows) { if (row) { const key = toTextNode(row.fields[0]); const value = toTextNode(row.fields[columnIndex]); if (key && value) { entries.push({ filePath, key, value }); } } } bundle[locale.value] = entries; } return bundle; } //# sourceMappingURL=csv.js.map