From 1005aad1c172ea66bc29e0979ee4d03089798447 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 8 Sep 2022 16:04:31 +0200 Subject: [PATCH] MOBILE-4059 core: Improve Translate.instant types --- .../comments/pages/viewer/viewer.page.ts | 2 +- src/core/features/course/services/sync.ts | 22 ++++++++++++------- src/core/services/utils/dom.ts | 8 +++++-- src/core/singletons/index.ts | 5 ++++- src/testing/utils.ts | 9 +++++--- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/core/features/comments/pages/viewer/viewer.page.ts b/src/core/features/comments/pages/viewer/viewer.page.ts index 4d7ddf9b7..54cf2285f 100644 --- a/src/core/features/comments/pages/viewer/viewer.page.ts +++ b/src/core/features/comments/pages/viewer/viewer.page.ts @@ -128,7 +128,7 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy { this.componentName = CoreNavigator.getRequiredRouteParam('componentName'); this.itemId = CoreNavigator.getRequiredRouteNumberParam('itemId'); this.area = CoreNavigator.getRouteParam('area') || ''; - this.title = CoreNavigator.getRouteNumberParam('title') || + this.title = CoreNavigator.getRouteParam('title') || Translate.instant('core.comments.comments'); this.courseId = CoreNavigator.getRouteNumberParam('courseId'); } catch (error) { diff --git a/src/core/features/course/services/sync.ts b/src/core/features/course/services/sync.ts index 773486202..5ef2145a4 100644 --- a/src/core/features/course/services/sync.ts +++ b/src/core/features/course/services/sync.ts @@ -193,10 +193,13 @@ export class CoreCourseSyncProvider extends CoreSyncBaseProvider { reject(); }, }, { - text: buttons && 'okText' in buttons ? buttons.okText : Translate.instant('core.ok'), + text: buttons && 'okText' in buttons + ? buttons.okText as string + : Translate.instant('core.ok'), handler: resolvePromise, }, ]; diff --git a/src/core/singletons/index.ts b/src/core/singletons/index.ts index faa44981f..ebaf9f791 100644 --- a/src/core/singletons/index.ts +++ b/src/core/singletons/index.ts @@ -217,7 +217,10 @@ export const Router = makeSingleton(RouterService); export const DomSanitizer = makeSingleton(DomSanitizerService); // Convert external libraries injectables. -export const Translate = makeSingleton(TranslateService); +export const Translate: Omit, 'instant'> & { + instant(keys: string[]): string[]; + instant(key: string, interpolateParams?: Record): string; +} = makeSingleton(TranslateService); // Async singletons. export const AngularFrameworkDelegate = asyncInstance(async () => { diff --git a/src/testing/utils.ts b/src/testing/utils.ts index 38b091cb8..b4bad0c31 100644 --- a/src/testing/utils.ts +++ b/src/testing/utils.ts @@ -29,6 +29,7 @@ import { CorePlatform } from '@services/platform'; import { CoreDB } from '@services/db'; import { CoreNavigator } from '@services/navigator'; import { CoreDomUtils } from '@services/utils/dom'; +import { TranslateService } from '@ngx-translate/core'; abstract class WrapperComponent { @@ -293,8 +294,10 @@ export function wait(time: number): Promise { * * @param translations List of translations. */ -export function mockTranslate(translations: Record): void { - mockSingleton(Translate, { - instant: (key) => translations[key] ?? key, +export function mockTranslate(translations: Record = {}): void { + mockSingleton(Translate as CoreSingletonProxy, { + instant: (key) => Array.isArray(key) + ? key.map(k => translations[k] ?? k) + : translations[key] ?? key, }); }