@@ -18,6 +18,16 @@ import { ModalController, Platform } from 'ionic-angular';
1818import { TranslateService } from '@ngx-translate/core' ;
1919import { CoreLangProvider } from '../lang' ;
2020
21+ /**
22+ * Different type of errors the app can treat.
23+ */
24+ export type CoreTextErrorObject = {
25+ message ?: string ;
26+ error ?: string ;
27+ content ?: string ;
28+ body ?: string ;
29+ } ;
30+
2131/*
2232 * "Utils" service with helper functions for text.
2333*/
@@ -122,6 +132,38 @@ export class CoreTextUtilsProvider {
122132 return result ;
123133 }
124134
135+ /**
136+ * Build a message with several paragraphs.
137+ *
138+ * @param paragraphs List of paragraphs.
139+ * @return Built message.
140+ */
141+ buildSeveralParagraphsMessage ( paragraphs : ( string | CoreTextErrorObject ) [ ] ) : string {
142+ // Filter invalid messages, and convert them to messages in case they're errors.
143+ const messages : string [ ] = [ ] ;
144+
145+ paragraphs . forEach ( ( paragraph ) => {
146+ // If it's an error, get its message.
147+ const message = this . getErrorMessageFromError ( paragraph ) ;
148+
149+ if ( paragraph ) {
150+ messages . push ( message ) ;
151+ }
152+ } ) ;
153+
154+ if ( messages . length < 2 ) {
155+ return messages [ 0 ] || '' ;
156+ }
157+
158+ let builtMessage = messages [ 0 ] ;
159+
160+ for ( let i = 1 ; i < messages . length ; i ++ ) {
161+ builtMessage = this . translate . instant ( 'core.twoparagraphs' , { p1 : builtMessage , p2 : messages [ i ] } ) ;
162+ }
163+
164+ return builtMessage ;
165+ }
166+
125167 /**
126168 * Convert size in bytes into human readable format
127169 *
@@ -449,7 +491,7 @@ export class CoreTextUtilsProvider {
449491 * @param error Error object.
450492 * @return Error message, undefined if not found.
451493 */
452- getErrorMessageFromError ( error : any ) : string {
494+ getErrorMessageFromError ( error : string | CoreTextErrorObject ) : string {
453495 if ( typeof error == 'string' ) {
454496 return error ;
455497 }
0 commit comments