| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  | import * as fs from "fs"; | 
					
						
							|  |  |  | import {Utils} from "../Utils"; | 
					
						
							| 
									
										
										
										
											2021-05-19 16:15:12 +02:00
										 |  |  | import ScriptUtils from "./ScriptUtils"; | 
					
						
							|  |  |  | import {readFileSync, writeFileSync} from "fs"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TranslationPart { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     contents: Map<string, TranslationPart | string> = new Map<string, TranslationPart | string>() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     add(language: string, obj: any){ | 
					
						
							|  |  |  |         for (const key in obj) { | 
					
						
							|  |  |  |             const v = obj[key] | 
					
						
							|  |  |  |             if(!this.contents.has(key)){ | 
					
						
							|  |  |  |                 this.contents.set(key, new TranslationPart()) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             const subpart = this.contents.get(key) as TranslationPart | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if(typeof v === "string"){ | 
					
						
							|  |  |  |                 subpart.contents.set(language, v) | 
					
						
							|  |  |  |             }else{ | 
					
						
							|  |  |  |                 subpart.add(language, v) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     toJson(): string{ | 
					
						
							|  |  |  |         const parts = [] | 
					
						
							|  |  |  |         for (let key of Array.from(this.contents.keys()) ){ | 
					
						
							|  |  |  |             const value = this.contents.get(key); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if(typeof  value === "string"){ | 
					
						
							|  |  |  |                 parts.push(`\"${key}\": \"${value}\"`) | 
					
						
							|  |  |  |             }else{ | 
					
						
							|  |  |  |                 parts.push(`\"${key}\": ${(value as TranslationPart).toJson()}`); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return JSON.stringify(JSON.parse(`{${parts.join(",")}}`), null, "    "); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | function isTranslation(tr: any): boolean { | 
					
						
							|  |  |  |     for (const key in tr) { | 
					
						
							|  |  |  |         if (typeof tr[key] !== "string") { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function transformTranslation(obj: any, depth = 1) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (isTranslation(obj)) { | 
					
						
							|  |  |  |         return `new Translation( ${JSON.stringify(obj)} )` | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let values = "" | 
					
						
							|  |  |  |     for (const key in obj) { | 
					
						
							| 
									
										
										
										
											2021-01-18 02:51:42 +01:00
										 |  |  |         if(key === "#"){ | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if(key.match("^[a-zA-Z0-9_]*$") === null){ | 
					
						
							|  |  |  |             throw "Invalid character in key: "+key | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  |         values += (Utils.Times((_) => "  ", depth)) + key + ": " + transformTranslation(obj[key], depth + 1) + ",\n" | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return `{${values}}`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function genTranslations() { | 
					
						
							| 
									
										
										
										
											2021-05-19 16:15:12 +02:00
										 |  |  |     const translations = JSON.parse(fs.readFileSync("./assets/generated/translations.json", "utf-8")) | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  |     const transformed = transformTranslation(translations); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 23:43:30 +02:00
										 |  |  |     let module = `import {Translation} from "../../UI/i18n/Translation"\n\nexport default class CompiledTranslations {\n\n`; | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  |     module += " public static t = " + transformed; | 
					
						
							|  |  |  |     module += "}" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 23:43:30 +02:00
										 |  |  |     fs.writeFileSync("./assets/generated/CompiledTranslations.ts", module); | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-19 16:15:12 +02:00
										 |  |  | // Read 'lang/*.json', writes to 'assets/generated/translations.json'
 | 
					
						
							|  |  |  | function compileTranslationsFromWeblate(){ | 
					
						
							|  |  |  |     const translations = ScriptUtils.readDirRecSync("./langs") | 
					
						
							|  |  |  |         .filter(path => path.indexOf(".json") > 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const allTranslations = new TranslationPart() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const translationFile of translations) { | 
					
						
							|  |  |  |         const contents = JSON.parse(readFileSync(translationFile, "utf-8")); | 
					
						
							|  |  |  |         let language = translationFile.substring(translationFile.lastIndexOf("/") + 1) | 
					
						
							|  |  |  |         language = language.substring(0, language.length-5) | 
					
						
							|  |  |  |         allTranslations.add(language, contents) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     writeFileSync("./assets/generated/translations.json", allTranslations.toJson()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | compileTranslationsFromWeblate(); | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  | genTranslations() |