From 2f51b9f2b8310188fa70115242dd4c6183efad91 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Mon, 4 Jun 2018 09:58:21 +0100 Subject: [PATCH] [ADF-3118] translation support for notification service (snackbars) (#3427) * translation support for snackbar * unit tests --- docs/core/notification.service.md | 18 ++++++++---- .../services/notification.service.spec.ts | 20 +++++++++++-- lib/core/services/notification.service.ts | 28 +++++++++++-------- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/docs/core/notification.service.md b/docs/core/notification.service.md index b6c73a537b..5326adf07d 100644 --- a/docs/core/notification.service.md +++ b/docs/core/notification.service.md @@ -15,12 +15,12 @@ Shows a notification message with optional feedback. - **openSnackMessage**(message: `string` = `null`, millisecondsDuration?: `number` = `null`): `MatSnackBarRef`
Opens a snackbar notification to show a message. - - _message:_ `string` - The message to show + - _message:_ `string` - The message (or resource key) to show. - _millisecondsDuration:_ `number` - (Optional) Time before notification disappears after being shown - **Returns** `MatSnackBarRef` - Information/control object for the snackbar - **openSnackMessageAction**(message: `string` = `null`, action: `string` = `null`, millisecondsDuration?: `number` = `null`): `MatSnackBarRef`
Opens a snackbar notification with a message and a response button. - - _message:_ `string` - The message to show + - _message:_ `string` - The message (or resource key) to show. - _action:_ `string` - Caption for the response button - _millisecondsDuration:_ `number` - (Optional) Time before the notification disappears (unless the button is clicked) - **Returns** `MatSnackBarRef` - Information/control object for the snackbar @@ -39,9 +39,12 @@ export class MyComponent implements OnInit { } ngOnInit() { - this.notificationService.openSnackMessage('test', 200000).afterDismissed().subscribe(() => { - console.log('The snack-bar was dismissed'); - }); + this.notificationService + .openSnackMessage('test', 200000) + .afterDismissed() + .subscribe(() => { + console.log('The snack-bar was dismissed'); + }); } } ``` @@ -55,7 +58,10 @@ export class MyComponent implements OnInit { } ngOnInit() { - this.notificationService.openSnackMessageAction('Do you want to report this issue?', 'send', 200000).afterDismissed().subscribe(() => { + this.notificationService + .openSnackMessageAction('Do you want to report this issue?', 'send', 200000) + .afterDismissed() + .subscribe(() => { console.log('The snack-bar was dismissed'); }); } diff --git a/lib/core/services/notification.service.spec.ts b/lib/core/services/notification.service.spec.ts index a66da91bfa..877efb9ff7 100644 --- a/lib/core/services/notification.service.spec.ts +++ b/lib/core/services/notification.service.spec.ts @@ -23,6 +23,8 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { MatSnackBar, MatSnackBarModule } from '@angular/material'; import { NoopAnimationsModule } from '@angular/platform-browser/animations'; import { NotificationService } from './notification.service'; +import { TranslationMock } from '../mock/translation.service.mock'; +import { TranslationService } from './translation.service'; @Component({ template: '', @@ -47,6 +49,7 @@ class ProvidesNotificationServiceComponent { describe('NotificationService', () => { let fixture: ComponentFixture; + let translationService: TranslationService; beforeEach(async(() => { TestBed.configureTestingModule({ @@ -60,11 +63,12 @@ describe('NotificationService', () => { NotificationService, MatSnackBar, OVERLAY_PROVIDERS, - LiveAnnouncer + LiveAnnouncer, + { provide: TranslationService, useClass: TranslationMock } ] }); - TestBed.compileComponents(); + translationService = TestBed.get(TranslationService); })); beforeEach(() => { @@ -72,6 +76,18 @@ describe('NotificationService', () => { fixture.detectChanges(); }); + it('should translate messages', (done) => { + spyOn(translationService, 'instant').and.callThrough(); + + let promise = fixture.componentInstance.sendMessage(); + promise.afterDismissed().subscribe(() => { + expect(translationService.instant).toHaveBeenCalled(); + done(); + }); + + fixture.detectChanges(); + }); + it('should open a message notification bar', (done) => { let promise = fixture.componentInstance.sendMessage(); promise.afterDismissed().subscribe(() => { diff --git a/lib/core/services/notification.service.ts b/lib/core/services/notification.service.ts index ea665dc3ed..9479a862b0 100644 --- a/lib/core/services/notification.service.ts +++ b/lib/core/services/notification.service.ts @@ -17,36 +17,42 @@ import { Injectable } from '@angular/core'; import { MatSnackBar, MatSnackBarRef } from '@angular/material'; +import { TranslationService } from './translation.service'; @Injectable() export class NotificationService { static DEFAULT_DURATION_MESSAGE: number = 5000; - constructor(public snackbar: MatSnackBar) { + constructor(private snackBar: MatSnackBar, + private translationService: TranslationService) { } /** - * Opens a snackbar notification to show a message. - * @param message The message to show + * Opens a SnackBar notification to show a message. + * @param message The message (or resource key) to show. * @param millisecondsDuration Time before notification disappears after being shown - * @returns Information/control object for the snackbar + * @returns Information/control object for the SnackBar */ - public openSnackMessage(message: string, millisecondsDuration?: number): MatSnackBarRef { - return this.snackbar.open(message, null, { + openSnackMessage(message: string, millisecondsDuration?: number): MatSnackBarRef { + const translatedMessage = this.translationService.instant(message); + + return this.snackBar.open(translatedMessage, null, { duration: millisecondsDuration || NotificationService.DEFAULT_DURATION_MESSAGE }); } /** - * Opens a snackbar notification with a message and a response button. - * @param message The message to show + * Opens a SnackBar notification with a message and a response button. + * @param message The message (or resource key) to show. * @param action Caption for the response button * @param millisecondsDuration Time before the notification disappears (unless the button is clicked) - * @returns Information/control object for the snackbar + * @returns Information/control object for the SnackBar */ - public openSnackMessageAction(message: string, action: string, millisecondsDuration?: number): MatSnackBarRef { - return this.snackbar.open(message, action, { + openSnackMessageAction(message: string, action: string, millisecondsDuration?: number): MatSnackBarRef { + const translatedMessage = this.translationService.instant(message); + + return this.snackBar.open(translatedMessage, action, { duration: millisecondsDuration || NotificationService.DEFAULT_DURATION_MESSAGE }); }