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 | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * 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 | ||||
|  * @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" : "") | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue