Add support for parsing NamespacedJson in Json compiler. Closes #44

This commit is contained in:
Kim Biesbjerg 2017-05-09 14:39:39 +02:00
parent 3b9561916b
commit 39a335638b

View File

@ -1,6 +1,8 @@
import { CompilerInterface } from './compiler.interface';
import { TranslationCollection } from '../utils/translation.collection';
import * as flat from 'flat';
export class JsonCompiler implements CompilerInterface {
public indentation: string = '\t';
@ -18,7 +20,15 @@ export class JsonCompiler implements CompilerInterface {
}
public parse(contents: string): TranslationCollection {
return new TranslationCollection(JSON.parse(contents));
let values: any = JSON.parse(contents);
if (this._isNamespacedJsonFormat(values)) {
values = flat.flatten(values);
}
return new TranslationCollection(values);
}
protected _isNamespacedJsonFormat(values: any): boolean {
return Object.keys(values).some(key => typeof values[key] === 'object');
}
}