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.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) {

View File

@ -193,10 +193,13 @@ export class CoreCourseSyncProvider extends CoreSyncBaseProvider<CoreCourseSyncR
// Completion deleted, add a warning if the completion status doesn't match.
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,
error: Translate.instant('core.course.warningmanualcompletionmodified'),
}));
}),
});
}
return;
@ -220,10 +223,13 @@ export class CoreCourseSyncProvider extends CoreSyncBaseProvider<CoreCourseSyncR
await CoreCourseOffline.deleteManualCompletion(entry.cmid, siteId);
// 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,
error: CoreTextUtils.getErrorMessageFromError(error),
}));
}),
});
}
}));

View File

@ -1553,14 +1553,18 @@ export class CoreDomUtilsProvider {
// Default 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',
handler: () => {
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,
},
];

View File

@ -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<CoreSingletonProxy<TranslateService>, 'instant'> & {
instant(keys: string[]): string[];
instant(key: string, interpolateParams?: Record<string, unknown>): string;
} = makeSingleton(TranslateService);
// Async singletons.
export const AngularFrameworkDelegate = asyncInstance(async () => {

View File

@ -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<U> {
@ -293,8 +294,10 @@ export function wait(time: number): Promise<void> {
*
* @param translations List of translations.
*/
export function mockTranslate(translations: Record<string, string>): void {
mockSingleton(Translate, {
instant: (key) => translations[key] ?? key,
export function mockTranslate(translations: Record<string, string> = {}): void {
mockSingleton(Translate as CoreSingletonProxy<TranslateService>, {
instant: (key) => Array.isArray(key)
? key.map(k => translations[k] ?? k)
: translations[key] ?? key,
});
}