Add support for parsing NamespacedJson in Json compiler. Closes #44
This commit is contained in:
		| @@ -1,6 +1,8 @@ | |||||||
| import { CompilerInterface } from './compiler.interface'; | import { CompilerInterface } from './compiler.interface'; | ||||||
| import { TranslationCollection } from '../utils/translation.collection'; | import { TranslationCollection } from '../utils/translation.collection'; | ||||||
|  |  | ||||||
|  | import * as flat from 'flat'; | ||||||
|  |  | ||||||
| export class JsonCompiler implements CompilerInterface { | export class JsonCompiler implements CompilerInterface { | ||||||
|  |  | ||||||
| 	public indentation: string = '\t'; | 	public indentation: string = '\t'; | ||||||
| @@ -18,7 +20,15 @@ export class JsonCompiler implements CompilerInterface { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	public parse(contents: string): TranslationCollection { | 	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'); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user