Merge master
This commit is contained in:
commit
be95ad58f8
101 changed files with 6900 additions and 8378 deletions
|
@ -47,17 +47,33 @@ class TranslationPart {
|
|||
return;
|
||||
}
|
||||
|
||||
for (const key in object) {
|
||||
for (let key in object) {
|
||||
if (!object.hasOwnProperty(key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const v = object[key]
|
||||
|
||||
if (v == null) {
|
||||
console.warn("Got a null value for key ", key)
|
||||
continue
|
||||
}
|
||||
|
||||
if (v["id"] !== undefined && context.endsWith("tagRenderings")) {
|
||||
// We use the embedded id as key instead of the index as this is more stable
|
||||
// Note: indonesian is shortened as 'id' as well!
|
||||
if (v["en"] !== undefined || v["nl"] !== undefined) {
|
||||
// This is probably a translation already!
|
||||
// pass
|
||||
} else {
|
||||
|
||||
key = v["id"]
|
||||
if (typeof key !== "string") {
|
||||
throw "Panic: found a non-string ID at" + context
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof v !== "object") {
|
||||
continue;
|
||||
}
|
||||
|
@ -118,7 +134,10 @@ class TranslationPart {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks that the given object only contains string-values
|
||||
* @param tr
|
||||
*/
|
||||
function isTranslation(tr: any): boolean {
|
||||
for (const key in tr) {
|
||||
if (typeof tr[key] !== "string") {
|
||||
|
@ -128,6 +147,11 @@ function isTranslation(tr: any): boolean {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a translation object into something that can be added to the 'generated translations'
|
||||
* @param obj
|
||||
* @param depth
|
||||
*/
|
||||
function transformTranslation(obj: any, depth = 1) {
|
||||
|
||||
if (isTranslation(obj)) {
|
||||
|
@ -148,6 +172,9 @@ function transformTranslation(obj: any, depth = 1) {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the big compiledTranslations file
|
||||
*/
|
||||
function genTranslations() {
|
||||
const translations = JSON.parse(fs.readFileSync("./assets/generated/translations.json", "utf-8"))
|
||||
const transformed = transformTranslation(translations);
|
||||
|
@ -161,7 +188,10 @@ function genTranslations() {
|
|||
|
||||
}
|
||||
|
||||
// Read 'lang/*.json', writes to 'assets/generated/translations.json'
|
||||
/**
|
||||
* Reads 'lang/*.json', writes them into to 'assets/generated/translations.json'.
|
||||
* This is only for the core translations
|
||||
*/
|
||||
function compileTranslationsFromWeblate() {
|
||||
const translations = ScriptUtils.readDirRecSync("./langs", 1)
|
||||
.filter(path => path.indexOf(".json") > 0)
|
||||
|
@ -179,7 +209,11 @@ function compileTranslationsFromWeblate() {
|
|||
|
||||
}
|
||||
|
||||
// Get all the strings out of the layers; writes them onto the weblate paths
|
||||
/**
|
||||
* Get all the strings out of the layers; writes them onto the weblate paths
|
||||
* @param objects
|
||||
* @param target
|
||||
*/
|
||||
function generateTranslationsObjectFrom(objects: { path: string, parsed: { id: string } }[], target: string) {
|
||||
const tr = new TranslationPart();
|
||||
|
||||
|
@ -201,6 +235,7 @@ function generateTranslationsObjectFrom(objects: { path: string, parsed: { id: s
|
|||
}
|
||||
let json = tr.toJson(lang)
|
||||
try {
|
||||
|
||||
json = JSON.stringify(JSON.parse(json), null, " ");
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
|
@ -210,15 +245,34 @@ function generateTranslationsObjectFrom(objects: { path: string, parsed: { id: s
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge two objects together
|
||||
* @param source: where the tranlations come from
|
||||
* @param target: the object in which the translations should be merged
|
||||
* @param language: the language code
|
||||
* @param context: context for error handling
|
||||
* @constructor
|
||||
*/
|
||||
function MergeTranslation(source: any, target: any, language: string, context: string = "") {
|
||||
|
||||
let keyRemapping: Map<string, string> = undefined
|
||||
if (context.endsWith(".tagRenderings")) {
|
||||
keyRemapping = new Map<string, string>()
|
||||
for (const key in target) {
|
||||
keyRemapping.set(target[key].id, key)
|
||||
}
|
||||
}
|
||||
|
||||
for (const key in source) {
|
||||
if (!source.hasOwnProperty(key)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const sourceV = source[key];
|
||||
const targetV = target[key]
|
||||
const targetV = target[keyRemapping?.get(key) ?? key]
|
||||
|
||||
if (typeof sourceV === "string") {
|
||||
// Add the translation
|
||||
if (targetV === undefined) {
|
||||
if (typeof target === "string") {
|
||||
throw "Trying to merge a translation into a fixed string at " + context + " for key " + key;
|
||||
|
@ -279,9 +333,9 @@ function loadTranslationFilesFrom(target: string): Map<string, any> {
|
|||
for (const translationFilePath of translationFilePaths) {
|
||||
let language = translationFilePath.substr(translationFilePath.lastIndexOf("/") + 1)
|
||||
language = language.substr(0, language.length - 5)
|
||||
try{
|
||||
try {
|
||||
translationFiles.set(language, JSON.parse(readFileSync(translationFilePath, "utf8")))
|
||||
}catch(e){
|
||||
} catch (e) {
|
||||
console.error("Invalid JSON file or file does not exist", translationFilePath)
|
||||
throw e;
|
||||
}
|
||||
|
@ -301,6 +355,9 @@ function mergeLayerTranslations() {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the translations into the theme files
|
||||
*/
|
||||
function mergeThemeTranslations() {
|
||||
const themeFiles = ScriptUtils.getThemeFiles();
|
||||
for (const themeFile of themeFiles) {
|
||||
|
|
51
scripts/lint.ts
Normal file
51
scripts/lint.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
|
||||
/*
|
||||
* This script reads all theme and layer files and reformats them inplace
|
||||
* Use with caution, make a commit beforehand!
|
||||
*/
|
||||
|
||||
|
||||
import ScriptUtils from "./ScriptUtils";
|
||||
import {readFileSync, writeFileSync} from "fs";
|
||||
import {tag} from "@turf/turf";
|
||||
import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson";
|
||||
|
||||
/**
|
||||
* In place fix
|
||||
*/
|
||||
function fixLayerConfig(config: LayerConfigJson) : void{
|
||||
if(config.tagRenderings === undefined){
|
||||
return
|
||||
}
|
||||
|
||||
for (const tagRendering of config.tagRenderings) {
|
||||
if(tagRendering["#"] !== undefined){
|
||||
tagRendering["id"] = tagRendering["#"]
|
||||
delete tagRendering["#"]
|
||||
}
|
||||
if(tagRendering["id"] === undefined){
|
||||
if(tagRendering["freeform"]?.key !== undefined ) {
|
||||
tagRendering["id"] = config.id+"-"+tagRendering["freeform"]["key"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const layerFiles = ScriptUtils.getLayerFiles();
|
||||
for (const layerFile of layerFiles) {
|
||||
fixLayerConfig(layerFile.parsed)
|
||||
writeFileSync(layerFile.path, JSON.stringify(layerFile.parsed, null, " "))
|
||||
}
|
||||
|
||||
const themeFiles = ScriptUtils.getThemeFiles()
|
||||
for (const themeFile of themeFiles) {
|
||||
for (const layerConfig of themeFile.parsed.layers ?? []) {
|
||||
if(typeof layerConfig === "string" || layerConfig["builtin"]!== undefined){
|
||||
continue
|
||||
}
|
||||
// @ts-ignore
|
||||
fixLayerConfig(layerConfig)
|
||||
}
|
||||
writeFileSync(themeFile.path, JSON.stringify(themeFile.parsed, null, " "))
|
||||
}
|
||||
//*/
|
Loading…
Add table
Add a link
Reference in a new issue