forked from MapComplete/MapComplete
Prepare script to let translations flow back into the themes
This commit is contained in:
parent
8361cfac7a
commit
e057f10409
2 changed files with 83 additions and 9 deletions
4
Utils.ts
4
Utils.ts
|
@ -161,6 +161,9 @@ export class Utils {
|
||||||
|
|
||||||
static Merge(source: any, target: any) {
|
static Merge(source: any, target: any) {
|
||||||
for (const key in source) {
|
for (const key in source) {
|
||||||
|
if(!source.hasOwnProperty(key)){
|
||||||
|
continue
|
||||||
|
}
|
||||||
const sourceV = source[key];
|
const sourceV = source[key];
|
||||||
const targetV = target[key]
|
const targetV = target[key]
|
||||||
if (typeof sourceV === "object") {
|
if (typeof sourceV === "object") {
|
||||||
|
@ -322,6 +325,7 @@ export class Utils {
|
||||||
}
|
}
|
||||||
return bestColor ?? hex;
|
return bestColor ?? hex;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static tile2long(x, z) {
|
private static tile2long(x, z) {
|
||||||
return (x / Math.pow(2, z) * 360 - 180);
|
return (x / Math.pow(2, z) * 360 - 180);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ 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 * as bookcases from "../assets/layers/public_bookcase/public_bookcase.json"
|
||||||
import LayerOverviewUtils from "./generateLayerOverview";
|
import LayerOverviewUtils from "./generateLayerOverview";
|
||||||
|
import {Script} from "vm";
|
||||||
|
|
||||||
const knownLanguages = ["en", "nl", "de", "fr", "es", "gl", "ca"];
|
const knownLanguages = ["en", "nl", "de", "fr", "es", "gl", "ca"];
|
||||||
|
|
||||||
|
@ -108,12 +109,12 @@ class TranslationPart {
|
||||||
|
|
||||||
if (typeof value === "string") {
|
if (typeof value === "string") {
|
||||||
value = value.replace(/"/g, "\\\"")
|
value = value.replace(/"/g, "\\\"")
|
||||||
if(neededLanguage === undefined){
|
if (neededLanguage === undefined) {
|
||||||
parts.push(`\"${key}\": \"${value}\"`)
|
parts.push(`\"${key}\": \"${value}\"`)
|
||||||
}else if (key === neededLanguage){
|
} else if (key === neededLanguage) {
|
||||||
return `"${value}"`
|
return `"${value}"`
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
const sub = (value as TranslationPart).toJson(neededLanguage)
|
const sub = (value as TranslationPart).toJson(neededLanguage)
|
||||||
if (sub !== "") {
|
if (sub !== "") {
|
||||||
|
@ -196,6 +197,7 @@ function generateTranslationFromLayerConfig(layerConfig: LayerConfigJson): Trans
|
||||||
return tr;
|
return tr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get all the string out of the themes
|
||||||
function generateLayerTranslationsObject() {
|
function generateLayerTranslationsObject() {
|
||||||
const layerFiles = new LayerOverviewUtils().getLayerFiles();
|
const layerFiles = new LayerOverviewUtils().getLayerFiles();
|
||||||
|
|
||||||
|
@ -210,20 +212,88 @@ function generateLayerTranslationsObject() {
|
||||||
const langs = tr.knownLanguages();
|
const langs = tr.knownLanguages();
|
||||||
for (const lang of langs) {
|
for (const lang of langs) {
|
||||||
console.log("Exporting ", lang)
|
console.log("Exporting ", lang)
|
||||||
|
|
||||||
let json = tr.toJson(lang)
|
let json = tr.toJson(lang)
|
||||||
try{
|
try {
|
||||||
json = JSON.stringify(JSON.parse(json), null, " ");
|
json = JSON.stringify(JSON.parse(json), null, " ");
|
||||||
}catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
writeFileSync("langs/layers/" + lang + ".json", json)
|
writeFileSync("langs/layers/" + lang + ".json", json)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function MergeTranslation(source: any, target: any, language: string, context: string = "") {
|
||||||
|
for (const key in source) {
|
||||||
|
if (!source.hasOwnProperty(key)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const sourceV = source[key];
|
||||||
|
const targetV = target[key]
|
||||||
|
if (typeof sourceV === "string") {
|
||||||
|
if (targetV[language] === sourceV) {
|
||||||
|
// Already the same
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (typeof sourceV === "object") {
|
||||||
|
if (targetV === undefined) {
|
||||||
|
throw "MergingTranslations failed: source object has a path that does not exist anymore in the target: " + context
|
||||||
|
} else {
|
||||||
|
MergeTranslation(sourceV, targetV, language, context + "." + key);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
throw "Case fallthrough"
|
||||||
|
|
||||||
|
}
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergeLayerTranslation(layerConfig: LayerConfigJson, path: string, translationFiles: Map<string, any>) {
|
||||||
|
const id = layerConfig.id;
|
||||||
|
translationFiles.forEach((translations, lang) => {
|
||||||
|
const translationsForLayer = translations[id]
|
||||||
|
// MergeTranslation(translationsForLayer, layerConfig, lang, id)
|
||||||
|
})
|
||||||
|
writeFileSync(path, JSON.stringify(layerConfig, null, " "))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the translations back into the layers
|
||||||
|
*/
|
||||||
|
function mergeLayerTranslations() {
|
||||||
|
|
||||||
|
|
||||||
|
const translationFilePaths = ScriptUtils.readDirRecSync("./langs/layers")
|
||||||
|
.filter(path => path.endsWith(".json"))
|
||||||
|
|
||||||
|
const translationFiles = new Map<string, any>();
|
||||||
|
for (const translationFilePath of translationFilePaths) {
|
||||||
|
let language = translationFilePath.substr(translationFilePath.lastIndexOf("/") + 1)
|
||||||
|
language = language.substr(0, language.length - 5)
|
||||||
|
translationFiles.set(language, JSON.parse(readFileSync(translationFilePath, "utf8")))
|
||||||
|
}
|
||||||
|
|
||||||
|
const layerFiles = new LayerOverviewUtils().getLayerFiles();
|
||||||
|
for (const layerFile of layerFiles) {
|
||||||
|
mergeLayerTranslation(layerFile.parsed, layerFile.path, translationFiles)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// generateLayerTranslationsObject()
|
||||||
|
mergeLayerTranslations();
|
||||||
|
|
||||||
generateLayerTranslationsObject()
|
|
||||||
|
|
||||||
compileTranslationsFromWeblate();
|
compileTranslationsFromWeblate();
|
||||||
genTranslations()
|
genTranslations()
|
Loading…
Add table
Add a link
Reference in a new issue