forked from MapComplete/MapComplete
		
	Fix build, preparing theme string translations
This commit is contained in:
		
							parent
							
								
									3e2fa65414
								
							
						
					
					
						commit
						90b30ca71e
					
				
					 3 changed files with 46 additions and 55 deletions
				
			
		|  | @ -1,5 +1,6 @@ | |||
| import {lstatSync, readdirSync} from "fs"; | ||||
| import {lstatSync, readdirSync, readFileSync} from "fs"; | ||||
| import * as https from "https"; | ||||
| import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson"; | ||||
| 
 | ||||
| export default class ScriptUtils { | ||||
|     public static readDirRecSync(path): string[] { | ||||
|  | @ -58,5 +59,33 @@ export default class ScriptUtils { | |||
|         }).then(() => ScriptUtils.sleep(ms - 1000)); | ||||
|     } | ||||
| 
 | ||||
|     public static getLayerFiles(): { parsed: LayerConfigJson, path: string }[] { | ||||
|         return ScriptUtils.readDirRecSync("./assets/layers") | ||||
|             .filter(path => path.indexOf(".json") > 0) | ||||
|             .filter(path => path.indexOf("license_info.json") < 0) | ||||
|             .map(path => { | ||||
|                 try { | ||||
|                     const parsed = JSON.parse(readFileSync(path, "UTF8")); | ||||
|                     return {parsed: parsed, path: path} | ||||
|                 } catch (e) { | ||||
|                     console.error("Could not parse file ", "./assets/layers/" + path, "due to ", e) | ||||
|                 } | ||||
|             }) | ||||
|     } | ||||
| 
 | ||||
|     public static getThemeFiles() { | ||||
|         return ScriptUtils.readDirRecSync("./assets/themes") | ||||
|             .filter(path => path.endsWith(".json")) | ||||
|             .filter(path => path.indexOf("license_info.json") < 0) | ||||
|             .map(path => { | ||||
|                 try { | ||||
|                     return JSON.parse(readFileSync(path, "UTF8")); | ||||
|                 } catch (e) { | ||||
|                     console.error("Could not read file ", path, "due to ", e) | ||||
|                     throw e | ||||
|                 } | ||||
|             }); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -17,43 +17,13 @@ interface LayersAndThemes { | |||
| } | ||||
| 
 | ||||
| 
 | ||||
| export default class LayerOverviewUtils { | ||||
| 
 | ||||
|     getLayerFiles(): {parsed: LayerConfigJson, path: string}[] { | ||||
|         return ScriptUtils.readDirRecSync("./assets/layers") | ||||
|             .filter(path => path.indexOf(".json") > 0) | ||||
|             .filter(path => path.indexOf("license_info.json") < 0) | ||||
|             .map(path => { | ||||
|                 try { | ||||
|                     const parsed = JSON.parse(readFileSync(path, "UTF8")); | ||||
|                     return {parsed: parsed, path: path} | ||||
|                 } catch (e) { | ||||
|                     console.error("Could not parse file ", "./assets/layers/" + path, "due to ", e) | ||||
|                 } | ||||
|             }) | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     getThemeFiles() { | ||||
|         return ScriptUtils.readDirRecSync("./assets/themes") | ||||
|             .filter(path => path.endsWith(".json")) | ||||
|             .filter(path => path.indexOf("license_info.json") < 0) | ||||
|             .map(path => { | ||||
|                 try { | ||||
|                     return JSON.parse(readFileSync(path, "UTF8")); | ||||
|                 } catch (e) { | ||||
|                     console.error("Could not read file ", path, "due to ", e) | ||||
|                     throw e | ||||
|                 } | ||||
|             }); | ||||
|     } | ||||
| 
 | ||||
| class LayerOverviewUtils { | ||||
| 
 | ||||
|     loadThemesAndLayers(): LayersAndThemes { | ||||
| 
 | ||||
|         const layerFiles = this.getLayerFiles(); | ||||
|         const layerFiles = ScriptUtils.getLayerFiles(); | ||||
| 
 | ||||
|         const themeFiles: any[] = this.getThemeFiles(); | ||||
|         const themeFiles: any[] = ScriptUtils.getThemeFiles(); | ||||
| 
 | ||||
|         console.log("Discovered", layerFiles.length, "layers and", themeFiles.length, "themes\n") | ||||
|         return { | ||||
|  |  | |||
|  | @ -1,12 +1,10 @@ | |||
| import * as fs from "fs"; | ||||
| import {readFileSync, writeFileSync} from "fs"; | ||||
| import {Utils} from "../Utils"; | ||||
| import ScriptUtils from "./ScriptUtils"; | ||||
| import {readFileSync, writeFileSync} from "fs"; | ||||
| import LayerConfig from "../Customizations/JSON/LayerConfig"; | ||||
| import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson"; | ||||
| import * as bookcases from "../assets/layers/public_bookcase/public_bookcase.json" | ||||
| import LayerOverviewUtils from "./generateLayerOverview"; | ||||
| import {Script} from "vm"; | ||||
| import LayoutConfig from "../Customizations/JSON/LayoutConfig"; | ||||
| import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson"; | ||||
| 
 | ||||
| const knownLanguages = ["en", "nl", "de", "fr", "es", "gl", "ca"]; | ||||
| 
 | ||||
|  | @ -191,21 +189,14 @@ function compileTranslationsFromWeblate() { | |||
| 
 | ||||
| } | ||||
| 
 | ||||
| function generateTranslationFromLayerConfig(layerConfig: LayerConfigJson): TranslationPart { | ||||
|     const tr = new TranslationPart(); | ||||
|     tr.recursiveAdd(layerConfig) | ||||
|     return tr; | ||||
| } | ||||
| 
 | ||||
| // Get all the string out of the themes
 | ||||
| function generateLayerTranslationsObject() { | ||||
|     const layerFiles = new LayerOverviewUtils().getLayerFiles(); | ||||
| 
 | ||||
| // Get all the strings out of the layers
 | ||||
| function generateTranslationsObjectFrom(objects: {path: string, parsed: {id: string}}[], target: string) { | ||||
|     const tr = new TranslationPart(); | ||||
| 
 | ||||
|     for (const layerFile of layerFiles) { | ||||
|         const config: LayerConfigJson = layerFile.parsed; | ||||
|         const layerTr = generateTranslationFromLayerConfig(config) | ||||
|     for (const layerFile of objects) { | ||||
|         const config: {id: string} = layerFile.parsed; | ||||
|         const layerTr =new TranslationPart(); | ||||
|         layerTr.recursiveAdd(config) | ||||
|         tr.contents.set(config.id, layerTr) | ||||
|     } | ||||
| 
 | ||||
|  | @ -220,7 +211,7 @@ function generateLayerTranslationsObject() { | |||
|             console.error(e) | ||||
|         } | ||||
| 
 | ||||
|         writeFileSync("langs/layers/" + lang + ".json", json) | ||||
|         writeFileSync(`langs/${target}/${lang}.json`, json) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|  | @ -285,13 +276,14 @@ function mergeLayerTranslations() { | |||
|         translationFiles.set(language, JSON.parse(readFileSync(translationFilePath, "utf8"))) | ||||
|     } | ||||
| 
 | ||||
|     const layerFiles = new LayerOverviewUtils().getLayerFiles(); | ||||
|     const layerFiles = ScriptUtils.getLayerFiles(); | ||||
|     for (const layerFile of layerFiles) { | ||||
|         mergeLayerTranslation(layerFile.parsed, layerFile.path, translationFiles) | ||||
|     } | ||||
| } | ||||
| mergeLayerTranslations(); | ||||
| generateLayerTranslationsObject() | ||||
| generateTranslationsObjectFrom(ScriptUtils.getLayerFiles(), "layers") | ||||
| generateTranslationsObjectFrom(ScriptUtils.getThemeFiles(), "themes") | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue