| 
									
										
										
										
											2020-07-21 00:07:04 +02:00
										 |  |  | import {FixedUiElement} from "../Base/FixedUiElement"; | 
					
						
							| 
									
										
										
										
											2022-04-13 01:19:28 +02:00
										 |  |  | import {Translation, TypedTranslation} from "./Translation"; | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  | import BaseUIElement from "../BaseUIElement"; | 
					
						
							| 
									
										
										
										
											2022-01-29 02:45:59 +01:00
										 |  |  | import * as known_languages from "../../assets/generated/used_languages.json" | 
					
						
							| 
									
										
										
										
											2022-02-11 04:28:11 +01:00
										 |  |  | import CompiledTranslations from "../../assets/generated/CompiledTranslations"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-20 12:39:43 +02:00
										 |  |  | export default class Translations { | 
					
						
							| 
									
										
										
										
											2020-07-21 23:31:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 04:28:11 +01:00
										 |  |  |     static t = CompiledTranslations.t; | 
					
						
							| 
									
										
										
										
											2022-01-29 02:45:59 +01:00
										 |  |  |     private static knownLanguages = new Set(known_languages.languages) | 
					
						
							| 
									
										
										
										
											2020-07-21 23:31:41 +02:00
										 |  |  |     constructor() { | 
					
						
							|  |  |  |         throw "Translations is static. If you want to intitialize a new translation, use the singular form" | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |     public static W(s: string | BaseUIElement): BaseUIElement { | 
					
						
							| 
									
										
										
										
											2020-08-22 02:12:46 +02:00
										 |  |  |         if (typeof (s) === "string") { | 
					
						
							|  |  |  |             return new FixedUiElement(s); | 
					
						
							| 
									
										
										
										
											2020-07-20 12:39:43 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         if (typeof s === "number") { | 
					
						
							|  |  |  |             return new FixedUiElement("" + s) | 
					
						
							| 
									
										
										
										
											2021-08-23 15:48:42 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-08-22 02:12:46 +02:00
										 |  |  |         return s; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-21 12:39:28 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Converts a string or an object into a typed translation. | 
					
						
							|  |  |  |      * Translation objects ('Translation' and 'TypedTranslation') are converted/returned | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * Translations.T("some text") // => new TypedTranslation({"*": "some text"})
 | 
					
						
							|  |  |  |      * Translations.T("some text").txt // => "some text"
 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * const t = new Translation({"nl": "vertaling", "en": "translation"}) | 
					
						
							|  |  |  |      * Translations.T(t) // => new TypedTranslation<object>({"nl": "vertaling", "en": "translation"})
 | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * const t = new TypedTranslation({"nl": "vertaling", "en": "translation"}) | 
					
						
							|  |  |  |      * Translations.T(t) // => t
 | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * const json: any = {"en": "English", "nl": "Nederlands"}; | 
					
						
							|  |  |  |      * const translation = Translations.T(new Translation(json)); | 
					
						
							|  |  |  |      * translation.textFor("en") // => "English"
 | 
					
						
							|  |  |  |      * translation.textFor("nl") // => "Nederlands"
 | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-04-13 01:19:28 +02:00
										 |  |  |     static T(t: string | any, context = undefined): TypedTranslation<object> { | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         if (t === undefined || t === null) { | 
					
						
							| 
									
										
										
										
											2020-10-27 01:01:34 +01:00
										 |  |  |             return undefined; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         if (typeof t === "number") { | 
					
						
							|  |  |  |             t = "" + t | 
					
						
							| 
									
										
										
										
											2021-10-22 02:16:07 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         if (typeof t === "string") { | 
					
						
							| 
									
										
										
										
											2022-04-21 12:39:28 +02:00
										 |  |  |             return new TypedTranslation<object>({"*": t}, context); | 
					
						
							| 
									
										
										
										
											2020-10-27 01:01:34 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         if (t.render !== undefined) { | 
					
						
							| 
									
										
										
										
											2020-10-27 01:01:34 +01:00
										 |  |  |             const msg = "Creating a translation, but this object contains a 'render'-field. Use the translation directly" | 
					
						
							|  |  |  |             console.error(msg, t); | 
					
						
							|  |  |  |             throw msg | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-13 01:19:28 +02:00
										 |  |  |         if (t instanceof TypedTranslation) { | 
					
						
							| 
									
										
										
										
											2021-07-01 02:26:45 +02:00
										 |  |  |             return t; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-21 12:39:28 +02:00
										 |  |  |         if(t instanceof Translation){ | 
					
						
							|  |  |  |             return new TypedTranslation<object>(t.translations) | 
					
						
							| 
									
										
										
										
											2020-08-22 02:12:46 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-21 12:39:28 +02:00
										 |  |  |         return new TypedTranslation<object>(t, context); | 
					
						
							| 
									
										
										
										
											2020-07-20 12:39:43 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-21 00:07:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-21 12:39:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 02:01:34 +02:00
										 |  |  |     public static CountTranslations() { | 
					
						
							|  |  |  |         const queue: any = [Translations.t]; | 
					
						
							|  |  |  |         const tr: Translation[] = []; | 
					
						
							|  |  |  |         while (queue.length > 0) { | 
					
						
							|  |  |  |             const item = queue.pop(); | 
					
						
							|  |  |  |             if (item instanceof Translation || item.translations !== undefined) { | 
					
						
							|  |  |  |                 tr.push(item); | 
					
						
							| 
									
										
										
										
											2020-07-31 17:38:03 +02:00
										 |  |  |             } else if (typeof (item) === "string") { | 
					
						
							|  |  |  |                 console.warn("Got single string in translationgs file: ", item); | 
					
						
							| 
									
										
										
										
											2020-07-26 02:01:34 +02:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 for (const t in item) { | 
					
						
							|  |  |  |                     const x = item[t]; | 
					
						
							|  |  |  |                     queue.push(x) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const langaugeCounts = {}; | 
					
						
							|  |  |  |         for (const translation of tr) { | 
					
						
							|  |  |  |             for (const language in translation.translations) { | 
					
						
							|  |  |  |                 if (langaugeCounts[language] === undefined) { | 
					
						
							|  |  |  |                     langaugeCounts[language] = 1 | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     langaugeCounts[language]++; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         for (const language in langaugeCounts) { | 
					
						
							|  |  |  |             console.log("Total translations in ", language, langaugeCounts[language], "/", tr.length) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-29 02:45:59 +01:00
										 |  |  |     static isProbablyATranslation(transl: any) { | 
					
						
							|  |  |  |         if(typeof transl !== "object"){ | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-08 01:05:54 +01:00
										 |  |  |         if(Object.keys(transl).length == 0){ | 
					
						
							|  |  |  |             // No translations found; not a translation
 | 
					
						
							|  |  |  |             return false | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-29 02:45:59 +01:00
										 |  |  |         // is a weird key found?
 | 
					
						
							| 
									
										
										
										
											2022-04-06 17:28:51 +02:00
										 |  |  |         if(Object.keys(transl).some(key => key !== '_context' && !this.knownLanguages.has(key))){ | 
					
						
							| 
									
										
										
										
											2022-03-08 01:05:54 +01:00
										 |  |  |             return false | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2022-01-29 02:45:59 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-09-15 18:44:58 +02:00
										 |  |  | } |