Merge develop

This commit is contained in:
Pieter Vander Vennet 2022-04-19 01:39:03 +02:00
commit ccf9c4b5f6
50 changed files with 1427 additions and 766 deletions

View file

@ -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] !== undefined && section[subkey][key] !== undefined) {
console.log(`${subkey}${key} is already 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))

View file

@ -297,7 +297,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 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}")`
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"
@ -340,7 +353,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 }"