MOBILE-4288 multilang: Implement type safety

main
Noel De Martin 2023-04-20 13:03:46 +02:00
parent b52deca5fe
commit 9180e6e082
14 changed files with 56 additions and 27 deletions

View File

@ -42,6 +42,7 @@ import { AddonCalendarEventsSource } from '@addons/calendar/classes/events-sourc
import { CoreSwipeNavigationItemsManager } from '@classes/items-management/swipe-navigation-items-manager';
import { CoreReminders, CoreRemindersService } from '@features/reminders/services/reminders';
import { CoreRemindersSetReminderMenuComponent } from '@features/reminders/components/set-reminder-menu/set-reminder-menu';
import { CoreLang } from '@services/lang';
/**
* Page that displays a single calendar event.
@ -370,7 +371,7 @@ export class AddonCalendarEventPage implements OnInit, OnDestroy {
const groups = await CoreGroups.getUserGroupsInCourse(courseId);
const group = groups.find((group) => group.id == event.groupid);
this.groupName = group ? group.name : '';
this.groupName = group ? await CoreLang.filterMultilang(group.name) : '';
} catch {
// Error getting groups, just don't show the group name.

View File

@ -33,6 +33,7 @@ import { CoreError } from '@classes/errors/error';
import { AddonMessagesSyncEvents, AddonMessagesSyncProvider } from './messages-sync';
import { CoreWSError } from '@classes/errors/wserror';
import { AddonNotificationsPreferencesNotificationProcessorState } from '@addons/notifications/services/notifications';
import { MultilangString } from '@services/lang';
const ROOT_CACHE_KEY = 'mmaMessages:';
@ -2970,7 +2971,7 @@ export type AddonMessagesConversationMember = {
conversations?: { // Conversations between users.
id: number; // Conversations id.
type: number; // Conversation type: private or public.
name: string; // Multilang compatible conversation name2.
name: MultilangString; // Multilang compatible conversation name2.
timecreated: number; // The timecreated timestamp for the conversation.
}[];
};
@ -3495,7 +3496,7 @@ export type AddonMessagesGetUserContactsWSResponse = {
conversations?: { // Conversations between users.
id: number; // Conversations id.
type: number; // Conversation type: private or public.
name: string; // Multilang compatible conversation name2.
name: MultilangString; // Multilang compatible conversation name2.
timecreated: number; // The timecreated timestamp for the conversation.
}[];
}[];
@ -3535,7 +3536,7 @@ export type AddonMessagesGetContactRequestsWSResponse = {
conversations?: { // Conversations between users.
id: number; // Conversations id.
type: number; // Conversation type: private or public.
name: string; // Multilang compatible conversation name2.
name: MultilangString; // Multilang compatible conversation name2.
timecreated: number; // The timecreated timestamp for the conversation.
}[];
}[];

View File

@ -1147,10 +1147,10 @@ export class AddonModAssignSubmissionComponent implements OnInit, OnDestroy, Can
if (this.assign.teamsubmission) {
if (lastAttempt.submissiongroup) {
// Get the name of the group.
promises.push(CoreGroups.getActivityAllowedGroups(this.assign.cmid).then((result) => {
promises.push(CoreGroups.getActivityAllowedGroups(this.assign.cmid).then(async (result) => {
const group = result.groups.find((group) => group.id === lastAttempt.submissiongroup);
if (group) {
lastAttempt.submissiongroupname = group.name;
lastAttempt.submissiongroupname = await CoreLang.filterMultilang(group.name);
}
return;

View File

@ -35,6 +35,7 @@ import { AddonModAssignSync, AddonModAssignSyncResult } from '../assign-sync';
import { CoreUser } from '@features/user/services/user';
import { CoreGradesHelper } from '@features/grades/services/grades-helper';
import { CoreCourses } from '@features/courses/services/courses';
import { multilangString } from '@services/lang';
/**
* Handler to prefetch assigns.
@ -355,7 +356,7 @@ export class AddonModAssignPrefetchHandlerService extends CoreCourseActivityPref
// Teacher, prefetch all submissions.
if (!groupInfo.groups || groupInfo.groups.length == 0) {
groupInfo.groups = [{ id: 0, name: '' }];
groupInfo.groups = [{ id: 0, name: multilangString() }];
}
const promises = groupInfo.groups.map((group) =>

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { multilangString } from '@services/lang';
import { Injectable } from '@angular/core';
import { CoreComments } from '@features/comments/services/comments';
import { CoreCourseActivityPrefetchHandlerBase } from '@features/course/classes/activity-prefetch-handler';
@ -98,7 +99,7 @@ export class AddonModDataPrefetchHandlerService extends CoreCourseActivityPrefet
const groupInfo = await CoreGroups.getActivityGroupInfo(module.id, false, undefined, options.siteId);
if (!groupInfo.groups || groupInfo.groups.length == 0) {
groupInfo.groups = [{ id: 0, name: '' }];
groupInfo.groups = [{ id: 0, name: multilangString() }];
}
groups = groupInfo.groups || [];

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { multilangString } from '@services/lang';
import { Injectable } from '@angular/core';
import { CoreCourseActivityPrefetchHandlerBase } from '@features/course/classes/activity-prefetch-handler';
import { CoreCourseAnyModuleData, CoreCourseCommonModWSOptions } from '@features/course/services/course';
@ -187,7 +188,7 @@ export class AddonModFeedbackPrefetchHandlerService extends CoreCourseActivityPr
const promises: Promise<unknown>[] = [];
if (!groupInfo.groups || groupInfo.groups.length == 0) {
groupInfo.groups = [{ id: 0, name: '' }];
groupInfo.groups = [{ id: 0, name: multilangString() }];
}
groupInfo.groups.forEach((group) => {

View File

@ -44,6 +44,7 @@ import { AddonModForumDiscussionsSwipeManager } from '../../classes/forum-discus
import { ActivatedRoute, ActivatedRouteSnapshot } from '@angular/router';
import { AddonModForumDiscussionsSource } from '../../classes/forum-discussions-source';
import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/routed-items-manager-sources-tracker';
import { CoreLang, multilangString } from '@services/lang';
type NewDiscussionData = {
subject: string;
@ -190,14 +191,14 @@ export class AddonModForumNewDiscussionPage implements OnInit, OnDestroy, CanLea
}
// eslint-disable-next-line promise/no-nesting
return promise.then((forumGroups) => {
return promise.then(async (forumGroups) => {
if (forumGroups.length > 0) {
this.groups = forumGroups;
this.groupIds = forumGroups.map((group) => group.id).filter((id) => id > 0);
// Do not override group id.
this.newDiscussion.groupId = this.newDiscussion.groupId || this.getInitialGroupId();
this.showGroups = true;
this.calculateGroupName();
await this.calculateGroupName();
if (this.groupIds.length <= 1) {
this.newDiscussion.postToAllGroups = false;
}
@ -271,7 +272,7 @@ export class AddonModForumNewDiscussionPage implements OnInit, OnDestroy, CanLea
this.newDiscussion.subscribe = !!discussion.options.discussionsubscribe;
this.newDiscussion.pin = !!discussion.options.discussionpinned;
this.messageControl.setValue(discussion.message);
this.calculateGroupName();
await this.calculateGroupName();
// Treat offline attachments if any.
if (typeof discussion.options.attachmentsid === 'object' && discussion.options.attachmentsid.offline) {
@ -426,7 +427,7 @@ export class AddonModForumNewDiscussionPage implements OnInit, OnDestroy, CanLea
groups.unshift({
courseid: this.courseId,
id: AddonModForumProvider.ALL_PARTICIPANTS,
name: Translate.instant('core.allparticipants'),
name: multilangString(Translate.instant('core.allparticipants')),
});
}
@ -611,11 +612,13 @@ export class AddonModForumNewDiscussionPage implements OnInit, OnDestroy, CanLea
/**
* Calculate current group's name.
*/
calculateGroupName(): void {
async calculateGroupName(): Promise<void> {
if (this.newDiscussion.groupId <= 0) {
this.groupName = undefined;
} else {
this.groupName = this.groups.find(group => group.id === this.newDiscussion.groupId)?.name;
const groupName = this.groups.find(group => group.id === this.newDiscussion.groupId)?.name;
this.groupName = groupName && await CoreLang.filterMultilang(groupName);
}
}

View File

@ -48,6 +48,7 @@ import {
import { AddonModLessonModuleHandlerService } from '../../services/handlers/module';
import { CoreTime } from '@singletons/time';
import { CoreError } from '@classes/errors/error';
import { CoreLang } from '@services/lang';
/**
* Component that displays a lesson entry page.
@ -491,7 +492,7 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo
// Search the name of the group if it isn't all participants.
if (groupId && this.groupInfo && this.groupInfo.groups) {
const group = this.groupInfo.groups.find(group => groupId == group.id);
this.selectedGroupName = group?.name || '';
this.selectedGroupName = group ? await CoreLang.filterMultilang(group.name) : '';
}
// Get the overview of retakes for the group.

View File

@ -55,6 +55,7 @@ import {
} from '../../services/wiki-sync';
import { AddonModWikiMapModalComponent, AddonModWikiMapModalReturn } from '../map/map';
import { AddonModWikiSubwikiPickerComponent } from '../subwiki-picker/subwiki-picker';
import { CoreLang } from '@services/lang';
/**
* Component that displays a wiki entry page.
@ -901,7 +902,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
if (subwiki.groupid != 0 && userGroups.length > 0) {
// Get groupLabel if it has groupId.
const group = userGroups.find(group => group.id == subwiki.groupid);
groupLabel = group?.name || '';
groupLabel = group ? await CoreLang.filterMultilang(group.name) : '';
} else {
groupLabel = Translate.instant('addon.mod_wiki.notingroup');
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { multilangString } from '@services/lang';
import { AddonModDataSyncResult } from '@addons/mod/data/services/data-sync';
import { Injectable } from '@angular/core';
import { CoreCourseActivityPrefetchHandlerBase } from '@features/course/classes/activity-prefetch-handler';
@ -105,7 +106,7 @@ export class AddonModWorkshopPrefetchHandlerService extends CoreCourseActivityPr
if (access.canviewallsubmissions) {
const groupInfo = await CoreGroups.getActivityGroupInfo(module.id, false, undefined, options.siteId);
if (!groupInfo.groups || groupInfo.groups.length == 0) {
groupInfo.groups = [{ id: 0, name: '' }];
groupInfo.groups = [{ id: 0, name: multilangString() }];
}
groups = groupInfo.groups;
}

View File

@ -14,7 +14,7 @@
import { Component, Input, OnInit, Type } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { CoreLang } from '@services/lang';
import { CoreLang, multilangString } from '@services/lang';
import { AuthEmailSignupProfileField } from '@features/login/services/login-helper';
import { CoreUserProfileField } from '@features/user/services/user';
import { CoreUserProfileFieldDelegate } from '@features/user/services/user-profile-field-delegate';
@ -52,7 +52,7 @@ export class CoreUserProfileFieldComponent implements OnInit {
this.componentClass = await CoreUserProfileFieldDelegate.getComponent(this.field, this.signup);
if ('param1' in this.field && this.field.param1) {
this.field.param1 = await CoreLang.filterMultilang(<string> this.field.param1);
this.field.param1 = await CoreLang.filterMultilang(multilangString(this.field.param1));
}
this.data.field = this.field;

View File

@ -20,6 +20,7 @@ import { CoreError } from '@classes/errors/error';
import { makeSingleton, Translate } from '@singletons';
import { CoreWSExternalWarning } from '@services/ws';
import { CoreCourses } from '@features/courses/services/courses';
import { multilangString, MultilangString } from '@services/lang';
const ROOT_CACHE_KEY = 'mmGroups:';
@ -174,7 +175,7 @@ export class CoreGroupsProvider {
groupInfo.defaultGroupId = 0;
} else {
if (result.canaccessallgroups || groupInfo.visibleGroups) {
groupInfo.groups.push({ id: 0, name: Translate.instant('core.allparticipants') });
groupInfo.groups.push({ id: 0, name: multilangString(Translate.instant('core.allparticipants')) });
groupInfo.defaultGroupId = 0;
} else {
groupInfo.defaultGroupId = result.groups[0].id;
@ -442,7 +443,7 @@ export const CoreGroups = makeSingleton(CoreGroupsProvider);
*/
export type CoreGroup = {
id: number; // Group ID.
name: string; // Multilang compatible name, course unique'.
name: MultilangString; // Multilang compatible name, course unique'.
description?: string; // Group description text.
descriptionformat?: number; // Description format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN).
idnumber?: string; // Id number.
@ -526,7 +527,7 @@ type CoreGroupGetCourseUserGroupsWSParams = {
export type CoreGroupGetCourseUserGroupsWSResponse = {
groups: {
id: number; // Group record id.
name: string; // Multilang compatible name, course unique.
name: MultilangString; // Multilang compatible name, course unique.
description: string; // Group description text.
descriptionformat: number; // Description format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN).
idnumber: string; // Id number.

View File

@ -26,6 +26,7 @@ import { CoreSite } from '../classes/site';
import { CorePlatform } from '@services/platform';
import { AddonFilterMultilangHandler } from '@addons/filter/multilang/services/handlers/multilang';
import { AddonFilterMultilang2Handler } from '@addons/filter/multilang2/services/handlers/multilang2';
import { Brand } from '@/core/utils/types';
/*
* Service to handle language features, like changing the current language.
@ -524,8 +525,8 @@ export class CoreLangProvider {
* @param text Multilang string.
* @returns Filtered string.
*/
async filterMultilang(text: string): Promise<string> {
return Promise.resolve(text)
async filterMultilang(text: MultilangString): Promise<string> {
return Promise.resolve(text as unknown as string)
.then(text => AddonFilterMultilangHandler.filter(text))
.then(text => AddonFilterMultilang2Handler.filter(text));
}
@ -561,11 +562,27 @@ export class CoreLangProvider {
export const CoreLang = makeSingleton(CoreLangProvider);
/**
* Make a multilang string.
*
* @param text String.
* @returns Multilang string.
*/
export function multilangString(text: string = ''): MultilangString {
return text as unknown as MultilangString;
}
/**
* Language code. E.g. 'au', 'es', etc.
*/
export type CoreLangLanguage = string;
/**
* Branded type to mark multilang strings, this is useful to avoid rendering
* multilang strings without filtering.
*/
export type MultilangString = Brand<unknown, 'multilang'>;
/**
* Language object has two leves, first per language and second per string key.
*/

View File

@ -58,7 +58,7 @@ import { CoreUserSupport } from '@features/user/services/support';
import { CoreErrorInfoComponent } from '@components/error-info/error-info';
import { CorePlatform } from '@services/platform';
import { CoreCancellablePromise } from '@classes/cancellable-promise';
import { CoreLang } from '@services/lang';
import { CoreLang, multilangString } from '@services/lang';
/*
* "Utils" service with helper functions for UI, DOM elements and HTML code.
@ -1170,7 +1170,7 @@ export class CoreDomUtilsProvider {
if (hasHTMLTags && !CoreSites.getCurrentSite()?.isVersionGreaterEqualThan('3.7')) {
// Treat multilang.
options.message = await CoreLang.filterMultilang(<string> options.message);
options.message = await CoreLang.filterMultilang(multilangString(<string> options.message));
}
const alertId = <string> Md5.hashAsciiStr((options.header || '') + '#' + (options.message || ''));