forked from MapComplete/MapComplete
Chore: translations are now sorted
This commit is contained in:
parent
9723b5898a
commit
a144daa2f0
1 changed files with 49 additions and 20 deletions
|
@ -526,6 +526,25 @@ function generateTranslationsObjectFrom(
|
||||||
return langs
|
return langs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inline sort of the object
|
||||||
|
* @param obj
|
||||||
|
*/
|
||||||
|
function sortTranslationObject(obj: Record<string, string>): 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
|
* Merge two objects together
|
||||||
* @param source: where the translations come from
|
* @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]
|
const targetV = target[keyRemapping?.get(key) ?? key]
|
||||||
|
|
||||||
if (typeof sourceV === "string") {
|
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 === "") {
|
if (sourceV === "") {
|
||||||
console.log("Ignoring empty string in the translations")
|
console.log("Ignoring empty string in the translations")
|
||||||
|
delete target[key]
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof targetV === "string") {
|
if (typeof targetV === "string") {
|
||||||
|
@ -572,12 +579,20 @@ function MergeTranslation(source: any, target: any, language: string, context: s
|
||||||
)}`
|
)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
targetV[language] = sourceV
|
if (targetV[language] === sourceV) {
|
||||||
let was = ""
|
// Already the same
|
||||||
if (targetV[language] !== undefined && targetV[language] !== sourceV) {
|
continue
|
||||||
was = " (overwritten " + targetV[language] + ")"
|
|
||||||
}
|
}
|
||||||
// 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
|
continue
|
||||||
}
|
}
|
||||||
if (typeof sourceV === "object") {
|
if (typeof sourceV === "object") {
|
||||||
|
@ -642,6 +657,7 @@ function mergeLayerTranslations(englishOnly: boolean = false) {
|
||||||
config = Utils.Clone(config)
|
config = Utils.Clone(config)
|
||||||
removeNonEnglishTranslations(config)
|
removeNonEnglishTranslations(config)
|
||||||
}
|
}
|
||||||
|
sortTranslations(config)
|
||||||
writeFileSync(
|
writeFileSync(
|
||||||
layerFile.path,
|
layerFile.path,
|
||||||
JSON.stringify(config, null, " ") + (endsWithNewline ? "\n" : "")
|
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
|
* Load the translations into the theme files
|
||||||
*/
|
*/
|
||||||
|
@ -690,7 +719,7 @@ function mergeThemeTranslations(englishOnly: boolean = false) {
|
||||||
config = Utils.Clone(config)
|
config = Utils.Clone(config)
|
||||||
removeNonEnglishTranslations(config)
|
removeNonEnglishTranslations(config)
|
||||||
}
|
}
|
||||||
|
sortTranslations(config)
|
||||||
writeFileSync(
|
writeFileSync(
|
||||||
themeFile.path,
|
themeFile.path,
|
||||||
JSON.stringify(config, null, " ") + (endsWithNewline ? "\n" : "")
|
JSON.stringify(config, null, " ") + (endsWithNewline ? "\n" : "")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue