Skip to content

Commit 39a3356

Browse files
author
Kim Biesbjerg
committed
Add support for parsing NamespacedJson in Json compiler. Closes #44
1 parent 3b95619 commit 39a3356

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/compilers/json.compiler.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { CompilerInterface } from './compiler.interface';
22
import { TranslationCollection } from '../utils/translation.collection';
33

4+
import * as flat from 'flat';
5+
46
export class JsonCompiler implements CompilerInterface {
57

68
public indentation: string = '\t';
@@ -18,7 +20,15 @@ export class JsonCompiler implements CompilerInterface {
1820
}
1921

2022
public parse(contents: string): TranslationCollection {
21-
return new TranslationCollection(JSON.parse(contents));
23+
let values: any = JSON.parse(contents);
24+
if (this._isNamespacedJsonFormat(values)) {
25+
values = flat.flatten(values);
26+
}
27+
return new TranslationCollection(values);
28+
}
29+
30+
protected _isNamespacedJsonFormat(values: any): boolean {
31+
return Object.keys(values).some(key => typeof values[key] === 'object');
2232
}
2333

2434
}

0 commit comments

Comments
 (0)