Add typings to translations, move Subs into 'TypedTranslations', cleanup of duplicate parts in translation files, fix #752

This commit is contained in:
Pieter Vander Vennet 2022-04-13 01:19:28 +02:00
parent f5d5f304ae
commit e391c1ce20
12 changed files with 64 additions and 318 deletions

View file

@ -2,8 +2,6 @@ import * as fs from "fs";
import {readFileSync, writeFileSync} from "fs";
import {Utils} from "../Utils";
import ScriptUtils from "./ScriptUtils";
import {AllKnownLayouts} from "../Customizations/AllKnownLayouts";
import TranslatorsPanel from "../UI/BigComponents/TranslatorsPanel";
const knownLanguages = ["en", "nl", "de", "fr", "es", "gl", "ca"];
@ -275,7 +273,20 @@ function transformTranslation(obj: any, path: string[] = [], languageWhitelist :
}
value = nv;
}
values += `${Utils.Times((_) => " ", path.length + 1)}get ${key}() { return new Translation(${JSON.stringify(value)}, "core:${path.join(".")}.${key}") },
if(value["en"] === undefined){
throw `At ${path.join(".")}: Missing 'en' translation for ${JSON.stringify(value)}`
}
const subParts : string[] = value["en"].match(/{[^}]*}/g)
let expr = `return new Translation(${JSON.stringify(value)}, "core:${path.join(".")}.${key}")`
if(subParts !== null){
// convert '{to_substitute}' into 'to_substitute'
const types = Utils.Dedup( subParts.map(tp => tp.substring(1, tp.length - 1)))
expr = `return new TypedTranslation<{ ${types.join(", ")} }>(${JSON.stringify(value)}, "core:${path.join(".")}.${key}")`
}
values += `${Utils.Times((_) => " ", path.length + 1)}get ${key}() { ${expr} },
`
} else {
values += (Utils.Times((_) => " ", path.length + 1)) + key + ": " + transformTranslation(value, [...path, key], languageWhitelist) + ",\n"
@ -318,7 +329,7 @@ function genTranslations() {
const translations = JSON.parse(fs.readFileSync("./assets/generated/translations.json", "utf-8"))
const transformed = transformTranslation(translations);
let module = `import {Translation} from "../../UI/i18n/Translation"\n\nexport default class CompiledTranslations {\n\n`;
let module = `import {Translation, TypedTranslation} from "../../UI/i18n/Translation"\n\nexport default class CompiledTranslations {\n\n`;
module += " public static t = " + transformed;
module += "\n }"