From a144daa2f030aca6835e9d427a2cb89ea124f28e Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Sat, 26 Jul 2025 00:58:43 +0200 Subject: [PATCH] Chore: translations are now sorted --- scripts/generateTranslations.ts | 69 +++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/scripts/generateTranslations.ts b/scripts/generateTranslations.ts index b4f6df30d..fdd3306a3 100644 --- a/scripts/generateTranslations.ts +++ b/scripts/generateTranslations.ts @@ -526,6 +526,25 @@ function generateTranslationsObjectFrom( return langs } +/** + * Inline sort of the object + * @param obj + */ +function sortTranslationObject(obj: Record): void { + const keys = Object.keys(obj).filter(k => k !== "en" && k !== "_context") + keys.sort(/* fresh copy */) + keys.unshift("en") + keys.push("_context") + + const copy = {...obj} + for (const k in obj) { + delete obj[k] + } + for (const key of keys) { + obj[key] = copy[key] + } +} + /** * Merge two objects together * @param source: where the translations come from @@ -548,22 +567,10 @@ function MergeTranslation(source: any, target: any, language: string, context: s 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 for ${language} into a fixed string at ${context} for key ${key}` - } - target[key] = source[key] - continue - } - - if (targetV[language] === sourceV) { - // Already the same - continue - } - if (sourceV === "") { console.log("Ignoring empty string in the translations") + delete target[key] + return } if (typeof targetV === "string") { @@ -572,12 +579,20 @@ function MergeTranslation(source: any, target: any, language: string, context: s )}` } - targetV[language] = sourceV - let was = "" - if (targetV[language] !== undefined && targetV[language] !== sourceV) { - was = " (overwritten " + targetV[language] + ")" + if (targetV[language] === sourceV) { + // Already the same + continue } - // console.log(" + ", context + "." + language, "-->", sourceV, was) + + if (targetV === undefined) { + if (typeof target === "string") { + throw `Trying to merge a translation for ${language} into a fixed string at ${context} for key ${key}` + } + target[key] = sourceV + continue + } + + targetV[language] = sourceV continue } if (typeof sourceV === "object") { @@ -642,6 +657,7 @@ function mergeLayerTranslations(englishOnly: boolean = false) { config = Utils.Clone(config) removeNonEnglishTranslations(config) } + sortTranslations(config) writeFileSync( layerFile.path, JSON.stringify(config, null, " ") + (endsWithNewline ? "\n" : "") @@ -672,6 +688,19 @@ function removeNonEnglishTranslations(object: any) { ) } +function sortTranslations(object: any) { + Utils.WalkObject( + object, + (leaf: any) => { + sortTranslationObject(leaf) + }, + (possibleLeaf) => + possibleLeaf !== null && + typeof possibleLeaf === "object" && + GenerateTranslations.isTranslation(possibleLeaf) + ) +} + /** * Load the translations into the theme files */ @@ -690,7 +719,7 @@ function mergeThemeTranslations(englishOnly: boolean = false) { config = Utils.Clone(config) removeNonEnglishTranslations(config) } - + sortTranslations(config) writeFileSync( themeFile.path, JSON.stringify(config, null, " ") + (endsWithNewline ? "\n" : "")