MOBILE-4059 core: Improve Translate.instant types

main
Noel De Martin 2022-09-08 16:04:31 +02:00
parent 9f26620e03
commit 1005aad1c1
5 changed files with 31 additions and 15 deletions

View File

@ -128,7 +128,7 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
this.componentName = CoreNavigator.getRequiredRouteParam<string>('componentName'); this.componentName = CoreNavigator.getRequiredRouteParam<string>('componentName');
this.itemId = CoreNavigator.getRequiredRouteNumberParam('itemId'); this.itemId = CoreNavigator.getRequiredRouteNumberParam('itemId');
this.area = CoreNavigator.getRouteParam('area') || ''; this.area = CoreNavigator.getRouteParam('area') || '';
this.title = CoreNavigator.getRouteNumberParam('title') || this.title = CoreNavigator.getRouteParam('title') ||
Translate.instant('core.comments.comments'); Translate.instant('core.comments.comments');
this.courseId = CoreNavigator.getRouteNumberParam('courseId'); this.courseId = CoreNavigator.getRouteNumberParam('courseId');
} catch (error) { } catch (error) {

View File

@ -193,10 +193,13 @@ export class CoreCourseSyncProvider extends CoreSyncBaseProvider<CoreCourseSyncR
// Completion deleted, add a warning if the completion status doesn't match. // Completion deleted, add a warning if the completion status doesn't match.
if (onlineComp.state != entry.completed) { if (onlineComp.state != entry.completed) {
result.warnings.push(Translate.instant('core.course.warningofflinemanualcompletiondeleted', { result.warnings.push({
warningcode: 'apperror',
message: Translate.instant('core.course.warningofflinemanualcompletiondeleted', {
name: courseName || courseId, name: courseName || courseId,
error: Translate.instant('core.course.warningmanualcompletionmodified'), error: Translate.instant('core.course.warningmanualcompletionmodified'),
})); }),
});
} }
return; return;
@ -220,10 +223,13 @@ export class CoreCourseSyncProvider extends CoreSyncBaseProvider<CoreCourseSyncR
await CoreCourseOffline.deleteManualCompletion(entry.cmid, siteId); await CoreCourseOffline.deleteManualCompletion(entry.cmid, siteId);
// Completion deleted, add a warning. // Completion deleted, add a warning.
result.warnings.push(Translate.instant('core.course.warningofflinemanualcompletiondeleted', { result.warnings.push({
warningcode: 'apperror',
message: Translate.instant('core.course.warningofflinemanualcompletiondeleted', {
name: courseName || courseId, name: courseName || courseId,
error: CoreTextUtils.getErrorMessageFromError(error), error: CoreTextUtils.getErrorMessageFromError(error),
})); }),
});
} }
})); }));

View File

@ -1553,14 +1553,18 @@ export class CoreDomUtilsProvider {
// Default buttons. // Default buttons.
options.buttons = [ options.buttons = [
{ {
text: buttons && 'cancelText' in buttons ? buttons.cancelText : Translate.instant('core.cancel'), text: buttons && 'cancelText' in buttons
? buttons.cancelText as string
: Translate.instant('core.cancel'),
role: 'cancel', role: 'cancel',
handler: () => { handler: () => {
reject(); 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, handler: resolvePromise,
}, },
]; ];

View File

@ -217,7 +217,10 @@ export const Router = makeSingleton(RouterService);
export const DomSanitizer = makeSingleton(DomSanitizerService); export const DomSanitizer = makeSingleton(DomSanitizerService);
// Convert external libraries injectables. // Convert external libraries injectables.
export const Translate = makeSingleton(TranslateService); export const Translate: Omit<CoreSingletonProxy<TranslateService>, 'instant'> & {
instant(keys: string[]): string[];
instant(key: string, interpolateParams?: Record<string, unknown>): string;
} = makeSingleton(TranslateService);
// Async singletons. // Async singletons.
export const AngularFrameworkDelegate = asyncInstance(async () => { export const AngularFrameworkDelegate = asyncInstance(async () => {

View File

@ -29,6 +29,7 @@ import { CorePlatform } from '@services/platform';
import { CoreDB } from '@services/db'; import { CoreDB } from '@services/db';
import { CoreNavigator } from '@services/navigator'; import { CoreNavigator } from '@services/navigator';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { TranslateService } from '@ngx-translate/core';
abstract class WrapperComponent<U> { abstract class WrapperComponent<U> {
@ -293,8 +294,10 @@ export function wait(time: number): Promise<void> {
* *
* @param translations List of translations. * @param translations List of translations.
*/ */
export function mockTranslate(translations: Record<string, string>): void { export function mockTranslate(translations: Record<string, string> = {}): void {
mockSingleton(Translate, { mockSingleton(Translate as CoreSingletonProxy<TranslateService>, {
instant: (key) => translations[key] ?? key, instant: (key) => Array.isArray(key)
? key.map(k => translations[k] ?? k)
: translations[key] ?? key,
}); });
} }