@vendure/ngx-translate-extract
Version:
Extract strings from projects using ngx-translate
27 lines (26 loc) • 1.11 kB
JavaScript
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);
}
}