Fix build, preparing theme string translations

This commit is contained in:
pietervdvn 2021-05-19 23:31:00 +02:00
parent 3e2fa65414
commit 90b30ca71e
3 changed files with 46 additions and 55 deletions

View file

@ -1,5 +1,6 @@
import {lstatSync, readdirSync} from "fs"; import {lstatSync, readdirSync, readFileSync} from "fs";
import * as https from "https"; import * as https from "https";
import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson";
export default class ScriptUtils { export default class ScriptUtils {
public static readDirRecSync(path): string[] { public static readDirRecSync(path): string[] {
@ -58,5 +59,33 @@ export default class ScriptUtils {
}).then(() => ScriptUtils.sleep(ms - 1000)); }).then(() => ScriptUtils.sleep(ms - 1000));
} }
public static getLayerFiles(): { parsed: LayerConfigJson, path: string }[] {
return ScriptUtils.readDirRecSync("./assets/layers")
.filter(path => path.indexOf(".json") > 0)
.filter(path => path.indexOf("license_info.json") < 0)
.map(path => {
try {
const parsed = JSON.parse(readFileSync(path, "UTF8"));
return {parsed: parsed, path: path}
} catch (e) {
console.error("Could not parse file ", "./assets/layers/" + path, "due to ", e)
}
})
}
public static getThemeFiles() {
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"));
} catch (e) {
console.error("Could not read file ", path, "due to ", e)
throw e
}
});
}
} }

View file

@ -17,43 +17,13 @@ interface LayersAndThemes {
} }
export default class LayerOverviewUtils { class LayerOverviewUtils {
getLayerFiles(): {parsed: LayerConfigJson, path: string}[] {
return ScriptUtils.readDirRecSync("./assets/layers")
.filter(path => path.indexOf(".json") > 0)
.filter(path => path.indexOf("license_info.json") < 0)
.map(path => {
try {
const parsed = JSON.parse(readFileSync(path, "UTF8"));
return {parsed: parsed, path: path}
} catch (e) {
console.error("Could not parse file ", "./assets/layers/" + path, "due to ", e)
}
})
}
getThemeFiles() {
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"));
} catch (e) {
console.error("Could not read file ", path, "due to ", e)
throw e
}
});
}
loadThemesAndLayers(): LayersAndThemes { loadThemesAndLayers(): LayersAndThemes {
const layerFiles = this.getLayerFiles(); const layerFiles = ScriptUtils.getLayerFiles();
const themeFiles: any[] = this.getThemeFiles(); const themeFiles: any[] = ScriptUtils.getThemeFiles();
console.log("Discovered", layerFiles.length, "layers and", themeFiles.length, "themes\n") console.log("Discovered", layerFiles.length, "layers and", themeFiles.length, "themes\n")
return { return {

View file

@ -1,12 +1,10 @@
import * as fs from "fs"; import * as fs from "fs";
import {readFileSync, writeFileSync} from "fs";
import {Utils} from "../Utils"; import {Utils} from "../Utils";
import ScriptUtils from "./ScriptUtils"; import ScriptUtils from "./ScriptUtils";
import {readFileSync, writeFileSync} from "fs";
import LayerConfig from "../Customizations/JSON/LayerConfig";
import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson"; import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson";
import * as bookcases from "../assets/layers/public_bookcase/public_bookcase.json" import LayoutConfig from "../Customizations/JSON/LayoutConfig";
import LayerOverviewUtils from "./generateLayerOverview"; import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson";
import {Script} from "vm";
const knownLanguages = ["en", "nl", "de", "fr", "es", "gl", "ca"]; const knownLanguages = ["en", "nl", "de", "fr", "es", "gl", "ca"];
@ -191,21 +189,14 @@ function compileTranslationsFromWeblate() {
} }
function generateTranslationFromLayerConfig(layerConfig: LayerConfigJson): TranslationPart { // Get all the strings out of the layers
const tr = new TranslationPart(); function generateTranslationsObjectFrom(objects: {path: string, parsed: {id: string}}[], target: string) {
tr.recursiveAdd(layerConfig)
return tr;
}
// Get all the string out of the themes
function generateLayerTranslationsObject() {
const layerFiles = new LayerOverviewUtils().getLayerFiles();
const tr = new TranslationPart(); const tr = new TranslationPart();
for (const layerFile of layerFiles) { for (const layerFile of objects) {
const config: LayerConfigJson = layerFile.parsed; const config: {id: string} = layerFile.parsed;
const layerTr = generateTranslationFromLayerConfig(config) const layerTr =new TranslationPart();
layerTr.recursiveAdd(config)
tr.contents.set(config.id, layerTr) tr.contents.set(config.id, layerTr)
} }
@ -220,7 +211,7 @@ function generateLayerTranslationsObject() {
console.error(e) console.error(e)
} }
writeFileSync("langs/layers/" + lang + ".json", json) writeFileSync(`langs/${target}/${lang}.json`, json)
} }
} }
@ -285,13 +276,14 @@ function mergeLayerTranslations() {
translationFiles.set(language, JSON.parse(readFileSync(translationFilePath, "utf8"))) translationFiles.set(language, JSON.parse(readFileSync(translationFilePath, "utf8")))
} }
const layerFiles = new LayerOverviewUtils().getLayerFiles(); const layerFiles = ScriptUtils.getLayerFiles();
for (const layerFile of layerFiles) { for (const layerFile of layerFiles) {
mergeLayerTranslation(layerFile.parsed, layerFile.path, translationFiles) mergeLayerTranslation(layerFile.parsed, layerFile.path, translationFiles)
} }
} }
mergeLayerTranslations(); mergeLayerTranslations();
generateLayerTranslationsObject() generateTranslationsObjectFrom(ScriptUtils.getLayerFiles(), "layers")
generateTranslationsObjectFrom(ScriptUtils.getThemeFiles(), "themes")