From 9d8f0698f1f76d410168ca88dd2fb2c7bccda702 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Mon, 18 Apr 2022 01:06:22 +0200 Subject: [PATCH] Better output for errors --- scripts/automoveTranslations.ts | 61 +++++++++++++++++++++++++++++++++ scripts/generateTranslations.ts | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 scripts/automoveTranslations.ts diff --git a/scripts/automoveTranslations.ts b/scripts/automoveTranslations.ts new file mode 100644 index 000000000..27fd92228 --- /dev/null +++ b/scripts/automoveTranslations.ts @@ -0,0 +1,61 @@ +import * as languages from "../assets/generated/used_languages.json" +import {readFileSync, writeFileSync} from "fs"; + +/** + * Moves values around in 'section'. Section will be changed + * @param section + * @param referenceSection + * @param language + */ +function fixSection(section, referenceSection, language: string) { + if(section === undefined){ + return + } + outer: for (const key of Object.keys(section)) { + const v = section[key] + if(typeof v ==="string" && referenceSection[key] === undefined){ + // Not found in reference, search for a subsection with this key + for (const subkey of Object.keys(referenceSection)) { + const subreference = referenceSection[subkey] + if(subreference[key] !== undefined){ + if(section[subkey][key] !== undefined) { + console.log(`${subkey}${key} is alrady defined... Looking furhter`) + continue + } + if(typeof section[subkey] === "string"){ + console.log(`NOT overwriting '${section[subkey]}' for ${subkey} (needed for ${key})`) + }else{ + // apply fix + if(section[subkey] === undefined){ + section[subkey] = {} + } + section[subkey][key] = section[key] + delete section[key] + console.log(`Rewritten key: ${key} --> ${subkey}.${key} in language ${language}`) + continue outer + } + } + } + console.log("No solution found for "+key) + } + } +} + + +function main(args:string[]):void{ + const sectionName = args[0] + const l = args[1] + if(sectionName === undefined){ + console.log("Tries to automatically move translations to a new subsegment. Usage: 'sectionToCheck' 'language'") + return + } + const reference = JSON.parse( readFileSync("./langs/en.json","UTF8")) + const path = `./langs/${l}.json` + const file = JSON.parse( readFileSync(path,"UTF8")) + fixSection(file[sectionName], reference[sectionName], l) + writeFileSync(path, JSON.stringify(file, null, " ")+"\n") + + +} + +main(process.argv.slice(2)) \ No newline at end of file diff --git a/scripts/generateTranslations.ts b/scripts/generateTranslations.ts index 5b4f0a42d..a046eabbd 100644 --- a/scripts/generateTranslations.ts +++ b/scripts/generateTranslations.ts @@ -300,7 +300,7 @@ function transformTranslation(obj: any, path: string[] = [], languageWhitelist : if(value["en"] === undefined){ - throw `At ${path.join(".")}: Missing 'en' translation for ${JSON.stringify(value)}` + throw `At ${path.join(".")}: Missing 'en' translation at path ${path.join(".")}.${key}\n\tThe translations in other languages are ${JSON.stringify(value)}` } const subParts : string[] = value["en"].match(/{[^}]*}/g) let expr = `return new Translation(${JSON.stringify(value)}, "core:${path.join(".")}.${key}")`