Fix output for theme languages

This commit is contained in:
Pieter Vander Vennet 2021-05-19 23:40:55 +02:00
parent 90b30ca71e
commit 43866acd24
3 changed files with 22 additions and 24 deletions

View file

@ -1,6 +1,7 @@
import {lstatSync, readdirSync, readFileSync} from "fs"; import {lstatSync, readdirSync, readFileSync} from "fs";
import * as https from "https"; import * as https from "https";
import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson"; import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson";
import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson";
export default class ScriptUtils { export default class ScriptUtils {
public static readDirRecSync(path): string[] { public static readDirRecSync(path): string[] {
@ -73,13 +74,14 @@ export default class ScriptUtils {
}) })
} }
public static getThemeFiles() { public static getThemeFiles() : {parsed: LayoutConfigJson, path: string}[] {
return ScriptUtils.readDirRecSync("./assets/themes") return ScriptUtils.readDirRecSync("./assets/themes")
.filter(path => path.endsWith(".json")) .filter(path => path.endsWith(".json"))
.filter(path => path.indexOf("license_info.json") < 0) .filter(path => path.indexOf("license_info.json") < 0)
.map(path => { .map(path => {
try { try {
return JSON.parse(readFileSync(path, "UTF8")); const parsed = JSON.parse(readFileSync(path, "UTF8"));
return {parsed: parsed, path: path}
} catch (e) { } catch (e) {
console.error("Could not read file ", path, "due to ", e) console.error("Could not read file ", path, "due to ", e)
throw e throw e

View file

@ -8,6 +8,7 @@ import * as licenses from "../assets/generated/license_info.json"
import LayoutConfig from "../Customizations/JSON/LayoutConfig"; import LayoutConfig from "../Customizations/JSON/LayoutConfig";
import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson"; import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson";
import {Translation} from "../UI/i18n/Translation"; import {Translation} from "../UI/i18n/Translation";
import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson";
// This scripts scans 'assets/layers/*.json' for layer definition files and 'assets/themes/*.json' for theme definition files. // This scripts scans 'assets/layers/*.json' for layer definition files and 'assets/themes/*.json' for theme definition files.
// It spits out an overview of those to be used to load them // It spits out an overview of those to be used to load them
@ -23,7 +24,7 @@ class LayerOverviewUtils {
const layerFiles = ScriptUtils.getLayerFiles(); const layerFiles = ScriptUtils.getLayerFiles();
const themeFiles: any[] = ScriptUtils.getThemeFiles(); const themeFiles: LayoutConfigJson[] = ScriptUtils.getThemeFiles().map(x => x.parsed);
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

@ -3,8 +3,6 @@ import {readFileSync, writeFileSync} from "fs";
import {Utils} from "../Utils"; import {Utils} from "../Utils";
import ScriptUtils from "./ScriptUtils"; import ScriptUtils from "./ScriptUtils";
import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson"; import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson";
import LayoutConfig from "../Customizations/JSON/LayoutConfig";
import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson";
const knownLanguages = ["en", "nl", "de", "fr", "es", "gl", "ca"]; const knownLanguages = ["en", "nl", "de", "fr", "es", "gl", "ca"];
@ -42,17 +40,6 @@ class TranslationPart {
this.contents.set(translationsKey, v) this.contents.set(translationsKey, v)
} }
} }
isLeaf() {
for (let key of Array.from(this.contents.keys())) {
const value = this.contents.get(key);
if (typeof value !== "string") {
return false;
}
}
return true;
}
recursiveAdd(object: any) { recursiveAdd(object: any) {
@ -107,6 +94,7 @@ class TranslationPart {
if (typeof value === "string") { if (typeof value === "string") {
value = value.replace(/"/g, "\\\"") value = value.replace(/"/g, "\\\"")
.replace(/\n/g, "\\n")
if (neededLanguage === undefined) { if (neededLanguage === undefined) {
parts.push(`\"${key}\": \"${value}\"`) parts.push(`\"${key}\": \"${value}\"`)
} else if (key === neededLanguage) { } else if (key === neededLanguage) {
@ -190,18 +178,25 @@ function compileTranslationsFromWeblate() {
} }
// Get all the strings out of the layers // Get all the strings out of the layers
function generateTranslationsObjectFrom(objects: {path: string, parsed: {id: string}}[], target: string) { function generateTranslationsObjectFrom(objects: { path: string, parsed: { id: string } }[], target: string) {
const tr = new TranslationPart(); const tr = new TranslationPart();
for (const layerFile of objects) { for (const layerFile of objects) {
const config: {id: string} = layerFile.parsed; const config: { id: string } = layerFile.parsed;
const layerTr =new TranslationPart(); const layerTr = new TranslationPart();
if (config === undefined) {
throw "Got something not parsed! Path is " + layerFile.path
}
layerTr.recursiveAdd(config) layerTr.recursiveAdd(config)
tr.contents.set(config.id, layerTr) tr.contents.set(config.id, layerTr)
} }
const langs = tr.knownLanguages(); const langs = tr.knownLanguages();
for (const lang of langs) { for (const lang of langs) {
if(lang === "#"){
// Lets not export our comments
continue;
}
console.log("Exporting ", lang) console.log("Exporting ", lang)
let json = tr.toJson(lang) let json = tr.toJson(lang)
@ -227,14 +222,14 @@ function MergeTranslation(source: any, target: any, language: string, context: s
// Already the same // Already the same
continue; continue;
} }
if(typeof targetV === "string"){ if (typeof targetV === "string") {
console.error("Could not add a translation to string ", targetV, ". The translation is", sourceV, " in "+context) console.error("Could not add a translation to string ", targetV, ". The translation is", sourceV, " in " + context)
continue; continue;
} }
targetV[language] = sourceV; targetV[language] = sourceV;
console.log(" + ",context + "." + language, "-->", sourceV) console.log(" + ", context + "." + language, "-->", sourceV)
continue continue
} }
if (typeof sourceV === "object") { if (typeof sourceV === "object") {
@ -281,11 +276,11 @@ function mergeLayerTranslations() {
mergeLayerTranslation(layerFile.parsed, layerFile.path, translationFiles) mergeLayerTranslation(layerFile.parsed, layerFile.path, translationFiles)
} }
} }
mergeLayerTranslations(); mergeLayerTranslations();
generateTranslationsObjectFrom(ScriptUtils.getLayerFiles(), "layers") generateTranslationsObjectFrom(ScriptUtils.getLayerFiles(), "layers")
generateTranslationsObjectFrom(ScriptUtils.getThemeFiles(), "themes") generateTranslationsObjectFrom(ScriptUtils.getThemeFiles(), "themes")
compileTranslationsFromWeblate(); compileTranslationsFromWeblate();
genTranslations() genTranslations()