From 43866acd242e21bb2d6913102dc7c89d36690f8c Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Wed, 19 May 2021 23:40:55 +0200 Subject: [PATCH] Fix output for theme languages --- scripts/ScriptUtils.ts | 6 ++++-- scripts/generateLayerOverview.ts | 3 ++- scripts/generateTranslations.ts | 37 ++++++++++++++------------------ 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/scripts/ScriptUtils.ts b/scripts/ScriptUtils.ts index f04576c91..93521089e 100644 --- a/scripts/ScriptUtils.ts +++ b/scripts/ScriptUtils.ts @@ -1,6 +1,7 @@ import {lstatSync, readdirSync, readFileSync} from "fs"; import * as https from "https"; import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson"; +import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson"; export default class ScriptUtils { public static readDirRecSync(path): string[] { @@ -73,13 +74,14 @@ export default class ScriptUtils { }) } - public static getThemeFiles() { + public static getThemeFiles() : {parsed: LayoutConfigJson, path: string}[] { return ScriptUtils.readDirRecSync("./assets/themes") .filter(path => path.endsWith(".json")) .filter(path => path.indexOf("license_info.json") < 0) .map(path => { try { - return JSON.parse(readFileSync(path, "UTF8")); + const parsed = JSON.parse(readFileSync(path, "UTF8")); + return {parsed: parsed, path: path} } catch (e) { console.error("Could not read file ", path, "due to ", e) throw e diff --git a/scripts/generateLayerOverview.ts b/scripts/generateLayerOverview.ts index a4e3a46ff..6f09acec2 100644 --- a/scripts/generateLayerOverview.ts +++ b/scripts/generateLayerOverview.ts @@ -8,6 +8,7 @@ import * as licenses from "../assets/generated/license_info.json" import LayoutConfig from "../Customizations/JSON/LayoutConfig"; import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson"; import {Translation} from "../UI/i18n/Translation"; +import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson"; // This scripts scans 'assets/layers/*.json' for layer definition files and 'assets/themes/*.json' for theme definition files. // It spits out an overview of those to be used to load them @@ -23,7 +24,7 @@ class LayerOverviewUtils { const layerFiles = ScriptUtils.getLayerFiles(); - const themeFiles: any[] = ScriptUtils.getThemeFiles(); + const themeFiles: LayoutConfigJson[] = ScriptUtils.getThemeFiles().map(x => x.parsed); console.log("Discovered", layerFiles.length, "layers and", themeFiles.length, "themes\n") return { diff --git a/scripts/generateTranslations.ts b/scripts/generateTranslations.ts index cc9e6e2d8..9cd8a0470 100644 --- a/scripts/generateTranslations.ts +++ b/scripts/generateTranslations.ts @@ -3,8 +3,6 @@ import {readFileSync, writeFileSync} from "fs"; import {Utils} from "../Utils"; import ScriptUtils from "./ScriptUtils"; import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson"; -import LayoutConfig from "../Customizations/JSON/LayoutConfig"; -import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson"; const knownLanguages = ["en", "nl", "de", "fr", "es", "gl", "ca"]; @@ -42,17 +40,6 @@ class TranslationPart { this.contents.set(translationsKey, v) } } - - isLeaf() { - for (let key of Array.from(this.contents.keys())) { - const value = this.contents.get(key); - if (typeof value !== "string") { - return false; - } - } - return true; - } - recursiveAdd(object: any) { @@ -107,6 +94,7 @@ class TranslationPart { if (typeof value === "string") { value = value.replace(/"/g, "\\\"") + .replace(/\n/g, "\\n") if (neededLanguage === undefined) { parts.push(`\"${key}\": \"${value}\"`) } else if (key === neededLanguage) { @@ -190,18 +178,25 @@ function compileTranslationsFromWeblate() { } // Get all the strings out of the layers -function generateTranslationsObjectFrom(objects: {path: string, parsed: {id: string}}[], target: string) { +function generateTranslationsObjectFrom(objects: { path: string, parsed: { id: string } }[], target: string) { const tr = new TranslationPart(); for (const layerFile of objects) { - const config: {id: string} = layerFile.parsed; - const layerTr =new TranslationPart(); + const config: { id: string } = layerFile.parsed; + const layerTr = new TranslationPart(); + if (config === undefined) { + throw "Got something not parsed! Path is " + layerFile.path + } layerTr.recursiveAdd(config) tr.contents.set(config.id, layerTr) } const langs = tr.knownLanguages(); for (const lang of langs) { + if(lang === "#"){ + // Lets not export our comments + continue; + } console.log("Exporting ", lang) let json = tr.toJson(lang) @@ -227,14 +222,14 @@ function MergeTranslation(source: any, target: any, language: string, context: s // Already the same continue; } - - if(typeof targetV === "string"){ - console.error("Could not add a translation to string ", targetV, ". The translation is", sourceV, " in "+context) + + if (typeof targetV === "string") { + console.error("Could not add a translation to string ", targetV, ". The translation is", sourceV, " in " + context) continue; } targetV[language] = sourceV; - console.log(" + ",context + "." + language, "-->", sourceV) + console.log(" + ", context + "." + language, "-->", sourceV) continue } if (typeof sourceV === "object") { @@ -281,11 +276,11 @@ function mergeLayerTranslations() { mergeLayerTranslation(layerFile.parsed, layerFile.path, translationFiles) } } + mergeLayerTranslations(); generateTranslationsObjectFrom(ScriptUtils.getLayerFiles(), "layers") generateTranslationsObjectFrom(ScriptUtils.getThemeFiles(), "themes") - compileTranslationsFromWeblate(); genTranslations() \ No newline at end of file