UNPKG

@vendure/ngx-translate-extract

Version:
27 lines (26 loc) 1.11 kB
import { TranslationCollection } from '../utils/translation.collection.js'; import { stripBOM } from '../utils/utils.js'; import { flatten, unflatten } from 'flat'; export class NamespacedJsonCompiler { indentation = '\t'; trailingNewline = false; extension = 'json'; constructor(options) { if (options && typeof options.indentation !== 'undefined') { this.indentation = options.indentation; } if (options && typeof options.trailingNewline !== 'undefined') { this.trailingNewline = options.trailingNewline; } } compile(collection) { const values = unflatten(collection.toKeyValueObject(), { object: true, overwrite: true }); return JSON.stringify(values, null, this.indentation) + (this.trailingNewline ? '\n' : ''); } parse(contents) { const values = flatten(JSON.parse(stripBOM(contents))); const newValues = {}; Object.entries(values).forEach(([key, value]) => newValues[key] = { value: value, sourceFiles: [] }); return new TranslationCollection(newValues); } }