From e84a7951cd3ee461dea07bdc8671d3aa327dc35c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Scha=CC=88fli?= Date: Wed, 16 May 2018 19:40:38 +0200 Subject: [PATCH 1/2] Add translation fallback handling for unknown keys --- src/app/app.module.ts | 6 ++-- .../language/translation-missing-handler.ts | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/services/language/translation-missing-handler.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 5dc1afb5..65ac768f 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -15,7 +15,7 @@ import {StatusBar} from "@ionic-native/status-bar"; import {StreamingMedia} from "@ionic-native/streaming-media"; import {Toast} from "@ionic-native/toast"; import {IonicApp, IonicErrorHandler, IonicModule, ModalController, NavController, Platform} from "ionic-angular"; -import {TranslateModule, TranslateService} from "ng2-translate/ng2-translate"; +import {TranslateModule, TranslateService, MissingTranslationHandler} from "ng2-translate/ng2-translate"; import {TranslateLoader, TranslateStaticLoader} from "ng2-translate/src/translate.service"; import {OPEN_LEARNPLACE_ACTION_FACTORY, OpenLearnplaceAction, OpenLearnplaceActionFunction} from "../actions/open-learnplace-action"; import {OPEN_OBJECT_IN_ILIAS_ACTION_FACTORY, OpenObjectInILIASAction} from "../actions/open-object-in-ilias-action"; @@ -87,6 +87,7 @@ import {DiagnosticUtil} from "../services/device/hardware-features/diagnostics.u import {Hardware} from "../services/device/hardware-features/hardware-feature.service"; import {FileService} from "../services/file.service"; import {FooterToolbarService} from "../services/footer-toolbar.service"; +import {PegasusMissingTranslationHandler} from "../services/language/translation-missing-handler"; import {DEFAULT_LINK_BUILDER, DefaultLinkBuilder, DefaultLinkBuilderImpl} from "../services/link/default.builder"; import {LINK_BUILDER, LinkBuilderImpl} from "../services/link/link-builder.service"; import { @@ -459,7 +460,8 @@ import {HTTP} from "@ionic-native/http"; IonicErrorHandler, {provide: ErrorHandler, useClass: PegasusErrorHandler}, - {provide: XhrFactory, useClass: PegasusXhrFactory, multi: false} + {provide: XhrFactory, useClass: PegasusXhrFactory, multi: false}, + {provide: MissingTranslationHandler, useClass: PegasusMissingTranslationHandler, multi: false} ], exports: [ TranslateModule diff --git a/src/services/language/translation-missing-handler.ts b/src/services/language/translation-missing-handler.ts new file mode 100644 index 00000000..027aa13d --- /dev/null +++ b/src/services/language/translation-missing-handler.ts @@ -0,0 +1,36 @@ +import {MissingTranslationHandler, MissingTranslationHandlerParams, TranslateService} from "ng2-translate"; +import {Injectable} from "@angular/core"; +import {Logger} from "../logging/logging.api"; +import {Logging} from "../logging/logging.service"; + +/** + * Fallback handler for translation. + * The handler will check if the missing translation is due to a unknown type which may happens with ILIAS plugins. + * If the key do not start with the "object_type" namespace the missing key is return as translation in order to + * indicate which variable is missing. + * + * It is not possible to translate a string within this handler due to the fact that the + * TranslationService is depending on the MissingTranslation handler. + * + * @author Nicolas Schaefli + * @version 1.0.0 + * @since v2.0.1 + */ +@Injectable() +export class PegasusMissingTranslationHandler extends MissingTranslationHandler { + + private static readonly OBJECT_TYPE_KEY: string = "object_type."; + private readonly log: Logger = Logging.getLogger(PegasusMissingTranslationHandler.name); + + handle(params: MissingTranslationHandlerParams): string { + + // check if we got a object_type which indicates that we got a unknown object from ILIAS most likely a plugin + if(params.key.startsWith(PegasusMissingTranslationHandler.OBJECT_TYPE_KEY) === true) { + this.log.warn(() => `No ILIAS object translation found for "${params.key}" fallback to "Object" translation.`); + return "Object"; + } + + this.log.warn(() => `Missing translation falling back to key "${params.key}"`); + return params.key; + } +} From f54a21959b71e3e47e443424c5f606237daa9c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Scha=CC=88fli?= Date: Wed, 16 May 2018 19:43:36 +0200 Subject: [PATCH 2/2] Clean up translation fallback service imports --- src/services/language/translation-missing-handler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/language/translation-missing-handler.ts b/src/services/language/translation-missing-handler.ts index 027aa13d..247bdf0f 100644 --- a/src/services/language/translation-missing-handler.ts +++ b/src/services/language/translation-missing-handler.ts @@ -1,5 +1,5 @@ -import {MissingTranslationHandler, MissingTranslationHandlerParams, TranslateService} from "ng2-translate"; import {Injectable} from "@angular/core"; +import {MissingTranslationHandler, MissingTranslationHandlerParams} from "ng2-translate"; import {Logger} from "../logging/logging.api"; import {Logging} from "../logging/logging.service";