forked from MapComplete/MapComplete
		
	Scripts: add option to only keep english translations
This commit is contained in:
		
							parent
							
								
									c274a01965
								
							
						
					
					
						commit
						2f66b2ee59
					
				
					 1 changed files with 102 additions and 40 deletions
				
			
		|  | @ -2,6 +2,7 @@ import * as fs from "fs" | ||||||
| import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs" | import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs" | ||||||
| import { Utils } from "../src/Utils" | import { Utils } from "../src/Utils" | ||||||
| import ScriptUtils from "./ScriptUtils" | import ScriptUtils from "./ScriptUtils" | ||||||
|  | import Script from "./Script" | ||||||
| 
 | 
 | ||||||
| const knownLanguages = ["en", "nl", "de", "fr", "es", "gl", "ca"] | const knownLanguages = ["en", "nl", "de", "fr", "es", "gl", "ca"] | ||||||
| 
 | 
 | ||||||
|  | @ -658,32 +659,64 @@ function loadTranslationFilesFrom(target: string): Map<string, any> { | ||||||
| /** | /** | ||||||
|  * Load the translations from the weblate files back into the layers |  * Load the translations from the weblate files back into the layers | ||||||
|  */ |  */ | ||||||
| function mergeLayerTranslations() { | function mergeLayerTranslations(englishOnly: boolean = false) { | ||||||
|     const layerFiles = ScriptUtils.getLayerFiles() |     const layerFiles = ScriptUtils.getLayerFiles() | ||||||
|     for (const layerFile of layerFiles) { |     for (const layerFile of layerFiles) { | ||||||
|         mergeLayerTranslation(layerFile.parsed, layerFile.path, loadTranslationFilesFrom("layers")) |         mergeLayerTranslation(layerFile.parsed, layerFile.path, loadTranslationFilesFrom("layers")) | ||||||
|         const endsWithNewline = |         const endsWithNewline = | ||||||
|             readFileSync(layerFile.path, { encoding: "utf8" })?.endsWith("\n") ?? true |             readFileSync(layerFile.path, { encoding: "utf8" })?.endsWith("\n") ?? true | ||||||
|  |         let config = layerFile.parsed | ||||||
|  |         if (englishOnly) { | ||||||
|  |             config = Utils.Clone(config) | ||||||
|  |             removeNonEnglishTranslations(config) | ||||||
|  |         } | ||||||
|         writeFileSync( |         writeFileSync( | ||||||
|             layerFile.path, |             layerFile.path, | ||||||
|             JSON.stringify(layerFile.parsed, null, "  ") + (endsWithNewline ? "\n" : "") |             JSON.stringify(config, null, "  ") + (endsWithNewline ? "\n" : "") | ||||||
|         ) // layers use 2 spaces
 |         ) // layers use 2 spaces
 | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | function removeNonEnglishTranslations(object: any) { | ||||||
|  |     Utils.WalkObject( | ||||||
|  |         object, | ||||||
|  |         (leaf: any) => { | ||||||
|  |             const en = leaf["en"] | ||||||
|  |             if (!en) { | ||||||
|  |                 return | ||||||
|  |             } | ||||||
|  |             for (const key in leaf) { | ||||||
|  |                 if (key.startsWith("#")) { | ||||||
|  |                     continue | ||||||
|  |                 } | ||||||
|  |                 delete leaf[key] | ||||||
|  |             } | ||||||
|  |             leaf["en"] = en | ||||||
|  |         }, | ||||||
|  |         (possibleLeaf) => | ||||||
|  |             possibleLeaf !== null && typeof possibleLeaf === "object" && isTranslation(possibleLeaf) | ||||||
|  |     ) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * Load the translations into the theme files |  * Load the translations into the theme files | ||||||
|  */ |  */ | ||||||
| function mergeThemeTranslations() { | function mergeThemeTranslations(englishOnly: boolean = false) { | ||||||
|     const themeFiles = ScriptUtils.getThemeFiles() |     const themeFiles = ScriptUtils.getThemeFiles() | ||||||
|     for (const themeFile of themeFiles) { |     for (const themeFile of themeFiles) { | ||||||
|         const config = themeFile.parsed |         let config = themeFile.parsed | ||||||
|         mergeLayerTranslation(config, themeFile.path, loadTranslationFilesFrom("themes")) |         mergeLayerTranslation(config, themeFile.path, loadTranslationFilesFrom("themes")) | ||||||
| 
 | 
 | ||||||
|         const allTranslations = new TranslationPart() |         const allTranslations = new TranslationPart() | ||||||
|         allTranslations.recursiveAdd(config, themeFile.path) |         allTranslations.recursiveAdd(config, themeFile.path) | ||||||
|         const endsWithNewline = |         const endsWithNewline = | ||||||
|             readFileSync(themeFile.path, { encoding: "utf8" })?.endsWith("\n") ?? true |             readFileSync(themeFile.path, { encoding: "utf8" })?.endsWith("\n") ?? true | ||||||
|  | 
 | ||||||
|  |         if (englishOnly) { | ||||||
|  |             config = Utils.Clone(config) | ||||||
|  |             removeNonEnglishTranslations(config) | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         writeFileSync( |         writeFileSync( | ||||||
|             themeFile.path, |             themeFile.path, | ||||||
|             JSON.stringify(config, null, "  ") + (endsWithNewline ? "\n" : "") |             JSON.stringify(config, null, "  ") + (endsWithNewline ? "\n" : "") | ||||||
|  | @ -691,41 +724,70 @@ function mergeThemeTranslations() { | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| if (!existsSync("./langs/themes")) { | class GenerateTranslations extends Script { | ||||||
|  |     constructor() { | ||||||
|  |         super("Syncs translations from/to the theme and layer files") | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * OUtputs the 'used_languages.json'-file | ||||||
|  |      */ | ||||||
|  |     detectUsedLanguages() { | ||||||
|  |         { | ||||||
|  |             const l1 = generateTranslationsObjectFrom(ScriptUtils.getLayerFiles(), "layers") | ||||||
|  |             const l2 = generateTranslationsObjectFrom( | ||||||
|  |                 ScriptUtils.getThemeFiles().filter( | ||||||
|  |                     (th) => th.parsed.mustHaveLanguage === undefined | ||||||
|  |                 ), | ||||||
|  |                 "themes" | ||||||
|  |             ) | ||||||
|  | 
 | ||||||
|  |             const usedLanguages: string[] = Utils.Dedup(l1.concat(l2)).filter((v) => v !== "*") | ||||||
|  |             usedLanguages.sort() | ||||||
|  |             fs.writeFileSync( | ||||||
|  |                 "./src/assets/used_languages.json", | ||||||
|  |                 JSON.stringify({ languages: usedLanguages }) | ||||||
|  |             ) | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     async main(args: string[]): Promise<void> { | ||||||
|  |         if (!existsSync("./langs/themes")) { | ||||||
|             mkdirSync("./langs/themes") |             mkdirSync("./langs/themes") | ||||||
| } |         } | ||||||
| const themeOverwritesWeblate = process.argv[2] === "--ignore-weblate" |         const themeOverwritesWeblate = args[0] === "--ignore-weblate" | ||||||
| if (!themeOverwritesWeblate) { |         const englishOnly = args[0] === "--english-only" | ||||||
|  |         if (!themeOverwritesWeblate) { | ||||||
|             mergeLayerTranslations() |             mergeLayerTranslations() | ||||||
|             mergeThemeTranslations() |             mergeThemeTranslations() | ||||||
| } else { |  | ||||||
|     console.log("Ignore weblate") |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| const l1 = generateTranslationsObjectFrom(ScriptUtils.getLayerFiles(), "layers") |  | ||||||
| const l2 = generateTranslationsObjectFrom( |  | ||||||
|     ScriptUtils.getThemeFiles().filter((th) => th.parsed.mustHaveLanguage === undefined), |  | ||||||
|     "themes" |  | ||||||
| ) |  | ||||||
| 
 |  | ||||||
| const usedLanguages: string[] = Utils.Dedup(l1.concat(l2)).filter((v) => v !== "*") |  | ||||||
| usedLanguages.sort() |  | ||||||
| fs.writeFileSync("./src/assets/used_languages.json", JSON.stringify({ languages: usedLanguages })) |  | ||||||
| 
 |  | ||||||
| if (!themeOverwritesWeblate) { |  | ||||||
|     // Generates the core translations
 |  | ||||||
|             compileTranslationsFromWeblate() |             compileTranslationsFromWeblate() | ||||||
| } |         } else { | ||||||
| genTranslations() |             console.log("Ignore weblate") | ||||||
| const allTranslationFiles = ScriptUtils.readDirRecSync("langs").filter((path) => |         } | ||||||
|  | 
 | ||||||
|  |         this.detectUsedLanguages() | ||||||
|  |         genTranslations() | ||||||
|  |         { | ||||||
|  |             const allTranslationFiles = ScriptUtils.readDirRecSync("langs").filter((path) => | ||||||
|                 path.endsWith(".json") |                 path.endsWith(".json") | ||||||
| ) |             ) | ||||||
| for (const path of allTranslationFiles) { |             for (const path of allTranslationFiles) { | ||||||
|                 formatFile(path) |                 formatFile(path) | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         // Some validation
 | ||||||
|  |         TranslationPart.fromDirectory("./langs").validateStrict("./langs") | ||||||
|  |         TranslationPart.fromDirectory("./langs/layers").validateStrict("layers") | ||||||
|  |         TranslationPart.fromDirectory("./langs/themes").validateStrict("themes") | ||||||
|  | 
 | ||||||
|  |         if (englishOnly) { | ||||||
|  |             mergeLayerTranslations(true) | ||||||
|  |             mergeThemeTranslations(true) | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         console.log("All done!") | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Some validation
 | new GenerateTranslations().run() | ||||||
| TranslationPart.fromDirectory("./langs").validateStrict("./langs") |  | ||||||
| TranslationPart.fromDirectory("./langs/layers").validateStrict("layers") |  | ||||||
| TranslationPart.fromDirectory("./langs/themes").validateStrict("themes") |  | ||||||
| console.log("All done!") |  | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue