mapcomplete/scripts/automoveTranslations.ts

65 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-04-18 01:06:22 +02:00
import * as languages from "../assets/generated/used_languages.json"
2022-09-08 21:40:48 +02:00
import { readFileSync, writeFileSync } from "fs"
2022-04-18 01:06:22 +02:00
/**
* Moves values around in 'section'. Section will be changed
* @param section
* @param referenceSection
* @param language
*/
function fixSection(section, referenceSection, language: string) {
2022-09-08 21:40:48 +02:00
if (section === undefined) {
2022-04-18 01:06:22 +02:00
return
}
outer: for (const key of Object.keys(section)) {
const v = section[key]
2022-09-08 21:40:48 +02:00
if (typeof v === "string" && referenceSection[key] === undefined) {
2022-04-18 01:06:22 +02:00
// Not found in reference, search for a subsection with this key
for (const subkey of Object.keys(referenceSection)) {
const subreference = referenceSection[subkey]
2022-09-08 21:40:48 +02:00
if (subreference[key] !== undefined) {
if (section[subkey] !== undefined && section[subkey][key] !== undefined) {
2022-04-18 01:40:00 +02:00
console.log(`${subkey}${key} is already defined... Looking furhter`)
2022-04-18 01:06:22 +02:00
continue
}
2022-09-08 21:40:48 +02:00
if (typeof section[subkey] === "string") {
console.log(
`NOT overwriting '${section[subkey]}' for ${subkey} (needed for ${key})`
)
} else {
2022-04-18 01:06:22 +02:00
// apply fix
2022-09-08 21:40:48 +02:00
if (section[subkey] === undefined) {
2022-04-18 01:06:22 +02:00
section[subkey] = {}
}
section[subkey][key] = section[key]
2022-09-08 21:40:48 +02:00
delete section[key]
console.log(
`Rewritten key: ${key} --> ${subkey}.${key} in language ${language}`
)
2022-04-18 01:06:22 +02:00
continue outer
}
}
}
2022-09-08 21:40:48 +02:00
console.log("No solution found for " + key)
2022-04-18 01:06:22 +02:00
}
}
}
2022-09-08 21:40:48 +02:00
function main(args: string[]): void {
2022-04-18 01:06:22 +02:00
const sectionName = args[0]
const l = args[1]
2022-09-08 21:40:48 +02:00
if (sectionName === undefined) {
console.log(
"Tries to automatically move translations to a new subsegment. Usage: 'sectionToCheck' 'language'"
)
2022-04-18 01:06:22 +02:00
return
}
2022-09-08 21:40:48 +02:00
const reference = JSON.parse(readFileSync("./langs/en.json", "UTF8"))
2022-04-18 01:06:22 +02:00
const path = `./langs/${l}.json`
2022-09-08 21:40:48 +02:00
const file = JSON.parse(readFileSync(path, "UTF8"))
2022-04-18 01:06:22 +02:00
fixSection(file[sectionName], reference[sectionName], l)
2022-09-08 21:40:48 +02:00
writeFileSync(path, JSON.stringify(file, null, " ") + "\n")
2022-04-18 01:06:22 +02:00
}
2022-09-08 21:40:48 +02:00
main(process.argv.slice(2))