From d60f09889886eb3a117c7e879eae7676b1f8bd08 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Sat, 20 Jul 2024 19:13:16 +0200 Subject: [PATCH] Add 'catch' to onDestory functie --- src/UI/i18n/Translation.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/UI/i18n/Translation.ts b/src/UI/i18n/Translation.ts index 38425e87d..dc9586719 100644 --- a/src/UI/i18n/Translation.ts +++ b/src/UI/i18n/Translation.ts @@ -22,7 +22,7 @@ export class Translation extends BaseUIElement { constructor( translations: string | Record, context?: string, - strictLanguages?: boolean + strictLanguages?: boolean, ) { super() this._strictLanguages = strictLanguages @@ -62,7 +62,7 @@ export class Translation extends BaseUIElement { `. The offending object is: `, translations[translationsKey], "\n current translations are: ", - translations + translations, ) throw ( "Error in an object depicting a translation: a non-string object was found. (" + @@ -75,7 +75,7 @@ export class Translation extends BaseUIElement { if (count === 0) { console.error( "Constructing a translation, but the object containing translations is empty " + - (context ?? "No context given") + (context ?? "No context given"), ) } } @@ -94,7 +94,7 @@ export class Translation extends BaseUIElement { [], (f) => { this.onDestroy = f - } + }, ) } return this._currentLanguage @@ -113,7 +113,7 @@ export class Translation extends BaseUIElement { static ExtractAllTranslationsFrom( object: any, - context = "" + context = "", ): { context: string; tr: Translation }[] { const allTranslations: { context: string; tr: Translation }[] = [] for (const key in object) { @@ -127,7 +127,7 @@ export class Translation extends BaseUIElement { } if (typeof v === "object") { allTranslations.push( - ...Translation.ExtractAllTranslationsFrom(v, context + "." + key) + ...Translation.ExtractAllTranslationsFrom(v, context + "." + key), ) } } @@ -153,7 +153,11 @@ export class Translation extends BaseUIElement { Destroy() { super.Destroy() - this.onDestroy() + try { + this.onDestroy() + } catch (e) { + console.error("Could not call this.onDestroy", e) + } this.isDestroyed = true } @@ -180,6 +184,7 @@ export class Translation extends BaseUIElement { console.error("Missing language ", Locale.language.data, "for", this.translations) return undefined } + public textFor(language: string): string | undefined { return this.translations[this.actualLanguage(language)] } @@ -249,7 +254,7 @@ export class Translation extends BaseUIElement { */ public OnEveryLanguage( f: (s: string, language: string) => string, - context?: string + context?: string, ): Translation { const newTranslations = {} for (const lang in this.translations) { @@ -332,7 +337,7 @@ export class Translation extends BaseUIElement { const htmlElement = document.createElement("div") htmlElement.innerHTML = render const images = Array.from(htmlElement.getElementsByTagName("img")).map( - (img) => img.src + (img) => img.src, ) allIcons.push(...images) } else { @@ -345,7 +350,7 @@ export class Translation extends BaseUIElement { .map((img) => img.match(/src=("[^"]+"|'[^']+'|[^/ ]+)/)) .filter((match) => match != null) .map((match) => - match[1].trim().replace(/^['"]/, "").replace(/['"]$/, "") + match[1].trim().replace(/^['"]/, "").replace(/['"]$/, ""), ) allIcons.push(...sources) } @@ -393,7 +398,7 @@ export class TypedTranslation> extends Translation } PartialSubs( - text: Partial & Record + text: Partial & Record, ): TypedTranslation> { const newTranslations: Record = {} for (const lang in this.translations) { @@ -410,7 +415,7 @@ export class TypedTranslation> extends Translation PartialSubsTr( key: string, - replaceWith: Translation + replaceWith: Translation, ): TypedTranslation> { const newTranslations: Record = {} const toSearch = "{" + key + "}" @@ -429,7 +434,7 @@ export class TypedTranslation> extends Translation for (const missingLanguage of missingLanguages) { newTranslations[missingLanguage] = baseTemplate.replaceAll( toSearch, - replaceWith.textFor(missingLanguage) + replaceWith.textFor(missingLanguage), ) }