forked from MapComplete/MapComplete
Fix output for theme languages
This commit is contained in:
parent
90b30ca71e
commit
43866acd24
3 changed files with 22 additions and 24 deletions
|
@ -1,6 +1,7 @@
|
|||
import {lstatSync, readdirSync, readFileSync} from "fs";
|
||||
import * as https from "https";
|
||||
import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson";
|
||||
import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson";
|
||||
|
||||
export default class ScriptUtils {
|
||||
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")
|
||||
.filter(path => path.endsWith(".json"))
|
||||
.filter(path => path.indexOf("license_info.json") < 0)
|
||||
.map(path => {
|
||||
try {
|
||||
return JSON.parse(readFileSync(path, "UTF8"));
|
||||
const parsed = JSON.parse(readFileSync(path, "UTF8"));
|
||||
return {parsed: parsed, path: path}
|
||||
} catch (e) {
|
||||
console.error("Could not read file ", path, "due to ", e)
|
||||
throw e
|
||||
|
|
|
@ -8,6 +8,7 @@ import * as licenses from "../assets/generated/license_info.json"
|
|||
import LayoutConfig from "../Customizations/JSON/LayoutConfig";
|
||||
import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson";
|
||||
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.
|
||||
// It spits out an overview of those to be used to load them
|
||||
|
||||
|
@ -23,7 +24,7 @@ class LayerOverviewUtils {
|
|||
|
||||
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")
|
||||
return {
|
||||
|
|
|
@ -3,8 +3,6 @@ import {readFileSync, writeFileSync} from "fs";
|
|||
import {Utils} from "../Utils";
|
||||
import ScriptUtils from "./ScriptUtils";
|
||||
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"];
|
||||
|
||||
|
@ -42,17 +40,6 @@ class TranslationPart {
|
|||
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) {
|
||||
|
||||
|
||||
|
@ -107,6 +94,7 @@ class TranslationPart {
|
|||
|
||||
if (typeof value === "string") {
|
||||
value = value.replace(/"/g, "\\\"")
|
||||
.replace(/\n/g, "\\n")
|
||||
if (neededLanguage === undefined) {
|
||||
parts.push(`\"${key}\": \"${value}\"`)
|
||||
} else if (key === neededLanguage) {
|
||||
|
@ -190,18 +178,25 @@ function compileTranslationsFromWeblate() {
|
|||
}
|
||||
|
||||
// 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();
|
||||
|
||||
for (const layerFile of objects) {
|
||||
const config: {id: string} = layerFile.parsed;
|
||||
const layerTr =new TranslationPart();
|
||||
const config: { id: string } = layerFile.parsed;
|
||||
const layerTr = new TranslationPart();
|
||||
if (config === undefined) {
|
||||
throw "Got something not parsed! Path is " + layerFile.path
|
||||
}
|
||||
layerTr.recursiveAdd(config)
|
||||
tr.contents.set(config.id, layerTr)
|
||||
}
|
||||
|
||||
const langs = tr.knownLanguages();
|
||||
for (const lang of langs) {
|
||||
if(lang === "#"){
|
||||
// Lets not export our comments
|
||||
continue;
|
||||
}
|
||||
console.log("Exporting ", lang)
|
||||
|
||||
let json = tr.toJson(lang)
|
||||
|
@ -227,14 +222,14 @@ function MergeTranslation(source: any, target: any, language: string, context: s
|
|||
// Already the same
|
||||
continue;
|
||||
}
|
||||
|
||||
if(typeof targetV === "string"){
|
||||
console.error("Could not add a translation to string ", targetV, ". The translation is", sourceV, " in "+context)
|
||||
|
||||
if (typeof targetV === "string") {
|
||||
console.error("Could not add a translation to string ", targetV, ". The translation is", sourceV, " in " + context)
|
||||
continue;
|
||||
}
|
||||
|
||||
targetV[language] = sourceV;
|
||||
console.log(" + ",context + "." + language, "-->", sourceV)
|
||||
console.log(" + ", context + "." + language, "-->", sourceV)
|
||||
continue
|
||||
}
|
||||
if (typeof sourceV === "object") {
|
||||
|
@ -281,11 +276,11 @@ function mergeLayerTranslations() {
|
|||
mergeLayerTranslation(layerFile.parsed, layerFile.path, translationFiles)
|
||||
}
|
||||
}
|
||||
|
||||
mergeLayerTranslations();
|
||||
generateTranslationsObjectFrom(ScriptUtils.getLayerFiles(), "layers")
|
||||
generateTranslationsObjectFrom(ScriptUtils.getThemeFiles(), "themes")
|
||||
|
||||
|
||||
|
||||
compileTranslationsFromWeblate();
|
||||
genTranslations()
|
Loading…
Reference in a new issue