diff --git a/scripts/generateLayerOverview.ts b/scripts/generateLayerOverview.ts index d8543e4659..d753ea001f 100644 --- a/scripts/generateLayerOverview.ts +++ b/scripts/generateLayerOverview.ts @@ -37,10 +37,8 @@ import { Translatable } from "../src/Models/ThemeConfig/Json/Translatable" import { ValidateThemeAndLayers } from "../src/Models/ThemeConfig/Conversion/ValidateThemeAndLayers" import { ExtractImages } from "../src/Models/ThemeConfig/Conversion/FixImages" import { TagRenderingConfigJson } from "../src/Models/ThemeConfig/Json/TagRenderingConfigJson" -import { - LayerConfigDependencyGraph, - LevelInfo, -} from "../src/Models/ThemeConfig/LayerConfigDependencyGraph" +import { LayerConfigDependencyGraph, LevelInfo } from "../src/Models/ThemeConfig/LayerConfigDependencyGraph" +import { AddContextToTranslations } from "../src/Models/ThemeConfig/Conversion/AddContextToTranslations" // This scripts scans 'src/assets/layers/*.json' for layer definition files and 'src/assets/themes/*.json' for theme definition files. // It spits out an overview of those to be used to load them @@ -224,9 +222,16 @@ class LayerBuilder extends Conversion> { context.err("Invalid ID: expected", id, "but got", id) } const prepped = this.prepareLayer.convert(config, context) + const withContext = new AddContextToTranslations("layers:").convertStrict( + prepped, + ConversionContext.construct( + [prepped.id], + ["AddContextToTranslations"] + ) + ) this._loadedIds.add(id) - this._desugaringState.sharedLayers.set(id, prepped) - return prepped + this._desugaringState.sharedLayers.set(id, withContext) + return withContext } private buildLooping(ids: string[], context: ConversionContext) { @@ -690,8 +695,8 @@ class LayerOverviewUtils extends Script { ) const path = "assets/layers/questions/questions.json" - const sharedQuestions = this.parseLayer(doesImageExist, prepareLayer, path).raw - + const sharedQuestionsRaw = this.parseLayer(doesImageExist, prepareLayer, path).raw + const sharedQuestions = new AddContextToTranslations("").convertStrict(sharedQuestionsRaw, ConversionContext.construct(["layers:questions"], [])) const dict = new Map() for (const tr of sharedQuestions.tagRenderings) { diff --git a/src/Models/ThemeConfig/Conversion/AddContextToTranslations.ts b/src/Models/ThemeConfig/Conversion/AddContextToTranslations.ts index 8c5b1de517..4db97ef002 100644 --- a/src/Models/ThemeConfig/Conversion/AddContextToTranslations.ts +++ b/src/Models/ThemeConfig/Conversion/AddContextToTranslations.ts @@ -128,24 +128,28 @@ export class AddContextToTranslations extends DesugaringStep { if (leaf === undefined || leaf === null) { return leaf } - if (typeof leaf === "object") { - // 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"] - } - } - - return { - ...leaf, - _context: this._prefix + context.path.concat(path).join("."), - } - } else { + if (typeof leaf !== "object") { return leaf } + if(leaf["_context"] !== undefined){ + // Context is already set + return leaf + } + // 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"] + } + } + const pth = this._prefix + Utils.NoEmpty(context.path.concat(path).map(x => "" + x)).join(".") + console.log("Setting _context to: ",pth) + return { + ...leaf, + _context: pth, + } }, (obj) => obj === undefined || obj === null || Translations.isProbablyATranslation(obj) )