| 
									
										
										
										
											2022-04-08 21:53:08 +02:00
										 |  |  | 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" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-04-08 21:53:08 +02:00
										 |  |  |         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
 | 
					
						
							| 
									
										
										
										
											2022-04-13 00:31:13 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-04-14 19:46:14 +02:00
										 |  |  |      * // should use the ID if one is present instead of the index
 | 
					
						
							|  |  |  |      * const theme = { | 
					
						
							|  |  |  |      *   layers: [ | 
					
						
							|  |  |  |      *       { | 
					
						
							|  |  |  |      *           tagRenderings:[ | 
					
						
							|  |  |  |      *               {id: "some-tr", | 
					
						
							|  |  |  |      *               question:{ | 
					
						
							|  |  |  |      *                   en:"Question?" | 
					
						
							|  |  |  |      *               } | 
					
						
							|  |  |  |      *               } | 
					
						
							|  |  |  |      *           ] | 
					
						
							|  |  |  |      *       } | 
					
						
							|  |  |  |      *   ] | 
					
						
							|  |  |  |      * } | 
					
						
							|  |  |  |      * const rewritten = new AddContextToTranslations<any>("prefix:").convert(theme, "context").result | 
					
						
							|  |  |  |      * const expected = { | 
					
						
							|  |  |  |      *   layers: [ | 
					
						
							|  |  |  |      *       { | 
					
						
							|  |  |  |      *           tagRenderings:[ | 
					
						
							|  |  |  |      *               {id: "some-tr", | 
					
						
							|  |  |  |      *               question:{ | 
					
						
							|  |  |  |      *                  _context: "prefix:context.layers.0.tagRenderings.some-tr.question" | 
					
						
							|  |  |  |      *                   en:"Question?" | 
					
						
							|  |  |  |      *               } | 
					
						
							|  |  |  |      *               } | 
					
						
							|  |  |  |      *           ] | 
					
						
							|  |  |  |      *       } | 
					
						
							|  |  |  |      *   ] | 
					
						
							|  |  |  |      * } | 
					
						
							|  |  |  |      * rewritten // => expected
 | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-04-13 00:31:13 +02:00
										 |  |  |      * // should preserve nulls
 | 
					
						
							|  |  |  |      * const theme = { | 
					
						
							|  |  |  |      *   layers: [ | 
					
						
							|  |  |  |      *       { | 
					
						
							|  |  |  |      *           builtin: ["abc"], | 
					
						
							|  |  |  |      *           override: { | 
					
						
							|  |  |  |      *               name:null | 
					
						
							|  |  |  |      *           } | 
					
						
							|  |  |  |      *       } | 
					
						
							|  |  |  |      *   ] | 
					
						
							|  |  |  |      * } | 
					
						
							|  |  |  |      * const rewritten = new AddContextToTranslations<any>("prefix:").convert(theme, "context").result | 
					
						
							|  |  |  |      * const expected = { | 
					
						
							|  |  |  |      *   layers: [ | 
					
						
							|  |  |  |      *       { | 
					
						
							|  |  |  |      *           builtin: ["abc"], | 
					
						
							|  |  |  |      *           override: { | 
					
						
							|  |  |  |      *               name: null | 
					
						
							|  |  |  |      *           } | 
					
						
							|  |  |  |      *       } | 
					
						
							|  |  |  |      *   ] | 
					
						
							|  |  |  |      * } | 
					
						
							|  |  |  |      * rewritten // => expected
 | 
					
						
							| 
									
										
										
										
											2022-07-11 09:14:26 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * // Should ignore all if '#dont-translate' is set
 | 
					
						
							|  |  |  |      * const theme = { | 
					
						
							|  |  |  |      *  "#dont-translate": "*", | 
					
						
							|  |  |  |      *   layers: [ | 
					
						
							|  |  |  |      *       { | 
					
						
							|  |  |  |      *           builtin: ["abc"], | 
					
						
							|  |  |  |      *           override: { | 
					
						
							|  |  |  |      *               title:{ | 
					
						
							|  |  |  |      *                   en: "Some title" | 
					
						
							|  |  |  |      *               } | 
					
						
							|  |  |  |      *           } | 
					
						
							|  |  |  |      *       } | 
					
						
							|  |  |  |      *   ] | 
					
						
							|  |  |  |      * } | 
					
						
							|  |  |  |      * const rewritten = new AddContextToTranslations<any>("prefix:").convert(theme, "context").result | 
					
						
							|  |  |  |      * rewritten // => theme
 | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-04-08 21:53:08 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     convert( | 
					
						
							|  |  |  |         json: T, | 
					
						
							|  |  |  |         context: string | 
					
						
							|  |  |  |     ): { result: T; errors?: string[]; warnings?: string[]; information?: string[] } { | 
					
						
							| 
									
										
										
										
											2022-07-11 09:14:26 +02:00
										 |  |  |         if (json["#dont-translate"] === "*") { | 
					
						
							|  |  |  |             return { result: json } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 21:53:08 +02:00
										 |  |  |         const result = Utils.WalkJson( | 
					
						
							|  |  |  |             json, | 
					
						
							|  |  |  |             (leaf, path) => { | 
					
						
							| 
									
										
										
										
											2022-04-13 00:31:13 +02:00
										 |  |  |                 if (leaf === undefined || leaf === null) { | 
					
						
							|  |  |  |                     return leaf | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-04-08 21:53:08 +02:00
										 |  |  |                 if (typeof leaf === "object") { | 
					
						
							| 
									
										
										
										
											2022-04-14 19:46:14 +02:00
										 |  |  |                     // follow the path. If we encounter a number, check that there is no ID we can use instead
 | 
					
						
							|  |  |  |                     let breadcrumb = json | 
					
						
							|  |  |  |                     for (let i = 0; i < path.length; i++) { | 
					
						
							|  |  |  |                         const pointer = path[i] | 
					
						
							|  |  |  |                         breadcrumb = breadcrumb[pointer] | 
					
						
							|  |  |  |                         if (pointer.match("[0-9]+") && breadcrumb["id"] !== undefined) { | 
					
						
							|  |  |  |                             path[i] = breadcrumb["id"] | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         } | 
					
						
							| 
									
										
										
										
											2022-04-14 19:46:14 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 21:53:08 +02:00
										 |  |  |                     return { ...leaf, _context: this._prefix + context + "." + path.join(".") } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2022-04-08 21:53:08 +02:00
										 |  |  |                     return leaf | 
					
						
							| 
									
										
										
										
											2022-04-14 19:46:14 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-04-13 00:31:13 +02:00
										 |  |  |             }, | 
					
						
							|  |  |  |             (obj) => obj === undefined || obj === null || Translations.isProbablyATranslation(obj) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-04-08 21:53:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             result, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |