forked from MapComplete/MapComplete
		
	
		
			
	
	
		
			57 lines
		
	
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
		
		
			
		
	
	
			57 lines
		
	
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
|  | import {DesugaringStep} from "./Conversion"; | ||
|  | import {Utils} from "../../../Utils"; | ||
|  | import Translations from "../../../UI/i18n/Translations"; | ||
|  | 
 | ||
|  | export class AddContextToTranslations<T> extends DesugaringStep<T> { | ||
|  |     private readonly _prefix: string; | ||
|  | 
 | ||
|  |     constructor(prefix = "") { | ||
|  |         super("Adds a '_context' to every object that is probably a translation", ["_context"], "AddContextToTranslation"); | ||
|  |         this._prefix = prefix; | ||
|  |     } | ||
|  | 
 | ||
|  |     /** | ||
|  |      * const theme = { | ||
|  |      *   layers: [ | ||
|  |      *       { | ||
|  |      *           builtin: ["abc"], | ||
|  |      *           override: { | ||
|  |      *               title:{ | ||
|  |      *                   en: "Some title" | ||
|  |      *               } | ||
|  |      *           } | ||
|  |      *       } | ||
|  |      *   ]   | ||
|  |      * } | ||
|  |      * const rewritten = new AddContextToTranslations<any>("prefix:").convert(theme, "context").result | ||
|  |      * const expected = { | ||
|  |      *   layers: [ | ||
|  |      *       { | ||
|  |      *           builtin: ["abc"], | ||
|  |      *           override: { | ||
|  |      *               title:{ | ||
|  |      *                  _context: "prefix:context.layers.0.override.title" | ||
|  |      *                   en: "Some title" | ||
|  |      *               } | ||
|  |      *           } | ||
|  |      *       } | ||
|  |      *   ]   | ||
|  |      * } | ||
|  |      * rewritten // => expected
 | ||
|  |      */ | ||
|  |     convert(json: T, context: string): { result: T; errors?: string[]; warnings?: string[]; information?: string[] } { | ||
|  | 
 | ||
|  |         const result = Utils.WalkJson(json, (leaf, path) => { | ||
|  |             if (typeof leaf === "object") { | ||
|  |                 return {...leaf, _context: this._prefix + context + "." + path.join(".")} | ||
|  |             } else { | ||
|  |                 return leaf | ||
|  |             } | ||
|  |         }, obj => obj !== undefined && obj !== null && Translations.isProbablyATranslation(obj)) | ||
|  | 
 | ||
|  |         return { | ||
|  |             result | ||
|  |         }; | ||
|  |     } | ||
|  | 
 | ||
|  | } |