diff --git a/docs/core/services/notification.service.md b/docs/core/services/notification.service.md index 5fc8204a19..c4304e21ba 100644 --- a/docs/core/services/notification.service.md +++ b/docs/core/services/notification.service.md @@ -21,7 +21,8 @@ Shows a notification message with optional feedback. Opens a SnackBar notification to show a message. - _message:_ `string` - The message (or resource key) to show. - _config:_ `number|MatSnackBarConfig` - (Optional) Time before notification disappears after being shown or MatSnackBarConfig object - - **Returns** [`MatSnackBarRef`](https://material.angular.io/components/snack-bar/overview)`` - Information/control object for the SnackBar + - translationArgs: `any` - The object with the keys for the interpolation of your translation string + - **Returns** [`MatSnackBarRef`](https://material.angular.io/components/snack-bar/overview)`` - Information/control object for the SnackBar - **openSnackMessageAction**(message: `string`, action: `string`, config?: `number|MatSnackBarConfig`): [`MatSnackBarRef`](https://material.angular.io/components/snack-bar/overview)``
Opens a SnackBar notification with a message and a response button. - _message:_ `string` - The message (or resource key) to show. diff --git a/lib/core/services/notification.service.spec.ts b/lib/core/services/notification.service.spec.ts index d0e82a4f9e..f26646baa1 100644 --- a/lib/core/services/notification.service.spec.ts +++ b/lib/core/services/notification.service.spec.ts @@ -45,6 +45,10 @@ class ProvidesNotificationServiceComponent { return this.notificationService.openSnackMessage('Test notification', 1000); } + sendMessageWithArgs() { + return this.notificationService.openSnackMessage('Test notification {{ arg }}', 1000, {arg: 'arg'}); + } + sendCustomMessage() { const matSnackBarConfig = new MatSnackBarConfig(); matSnackBarConfig.duration = 1000; @@ -108,6 +112,18 @@ describe('NotificationService', () => { fixture.detectChanges(); }); + it('should translate messages with args', (done) => { + spyOn(translationService, 'instant').and.callThrough(); + + const promise = fixture.componentInstance.sendMessageWithArgs(); + promise.afterDismissed().subscribe(() => { + expect(translationService.instant).toHaveBeenCalledWith('Test notification {{ arg }}', {arg: 'arg'}); + done(); + }); + + fixture.detectChanges(); + }); + it('should open a message notification bar', (done) => { const promise = fixture.componentInstance.sendMessage(); promise.afterDismissed().subscribe(() => { diff --git a/lib/core/services/notification.service.ts b/lib/core/services/notification.service.ts index 64b01f21f1..a8ba26d12c 100644 --- a/lib/core/services/notification.service.ts +++ b/lib/core/services/notification.service.ts @@ -41,22 +41,25 @@ export class NotificationService { /** * Opens a SnackBar notification to show a message. * @param message The message (or resource key) to show. + * @param translationArgs The interpolation parameters to add for the translation * @param config Time before notification disappears after being shown or MatSnackBarConfig object * @returns Information/control object for the SnackBar */ - openSnackMessage(message: string, config?: number | MatSnackBarConfig): MatSnackBarRef { + openSnackMessage(message: string, config?: number | MatSnackBarConfig, translationArgs?: any): MatSnackBarRef { + const translatedMessage = this.translationService.instant(message, translationArgs); + return this.performOpening(translatedMessage, config); + } + + private performOpening(translatedMessage: string, config?: number | MatSnackBarConfig): MatSnackBarRef { if (!config) { config = this.DEFAULT_DURATION_MESSAGE; } - const translatedMessage = this.translationService.instant(message); - if (typeof config === 'number') { config = { duration: config }; } - this.messages.next({ message: translatedMessage, dateTime: new Date }); return this.snackBar.open(translatedMessage, null, config); @@ -94,9 +97,12 @@ export class NotificationService { return this.snackBar.dismiss(); } - protected showMessage(message: string, panelClass: string, action?: string): MatSnackBarRef { - message = this.translationService.instant(message); + protected showMessage(message: string, panelClass: string, action?: string, interpolateArgs?: any): MatSnackBarRef { + message = this.translationService.instant(message, interpolateArgs); + return this.openMessageBar(message, panelClass, action); + } + private openMessageBar(message: string, panelClass: string, action?: string): MatSnackBarRef { this.messages.next({ message: message, dateTime: new Date }); return this.snackBar.open(message, action, { @@ -119,8 +125,8 @@ export class NotificationService { * @param message Text message or translation key for the message. * @param action Action name */ - showInfo(message: string, action?: string): MatSnackBarRef { - return this.showMessage(message, 'adf-info-snackbar', action); + showInfo(message: string, action?: string, interpolateArgs?: any): MatSnackBarRef { + return this.showMessage(message, 'adf-info-snackbar', action, interpolateArgs); } /**