From aca73e2ff2a6498d95df3167d657ba5ef6fc02da Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Sun, 3 Jun 2018 11:49:29 +0100 Subject: [PATCH] use instant translation (#382) * use instant translation * update code and tests * fix tests --- .../common/directives/node-copy.directive.ts | 14 +++-- .../directives/node-delete.directive.ts | 51 +++++++++---------- .../common/directives/node-move.directive.ts | 31 ++++++----- .../node-permanent-delete.directive.spec.ts | 14 ++--- .../node-permanent-delete.directive.ts | 20 ++++---- .../directives/node-restore.directive.spec.ts | 14 ++--- .../directives/node-restore.directive.ts | 24 ++++----- .../directives/node-versions.directive.ts | 4 +- 8 files changed, 80 insertions(+), 92 deletions(-) diff --git a/src/app/common/directives/node-copy.directive.ts b/src/app/common/directives/node-copy.directive.ts index 4256ebc5f..29911cba4 100644 --- a/src/app/common/directives/node-copy.directive.ts +++ b/src/app/common/directives/node-copy.directive.ts @@ -108,11 +108,10 @@ export class NodeCopyDirective { const undo = (numberOfCopiedItems > 0) ? this.translation.instant('APP.ACTIONS.UNDO') : ''; const withUndo = (numberOfCopiedItems > 0) ? '_WITH_UNDO' : ''; - this.translation.get(i18nMessageString, { success: numberOfCopiedItems, failed: failedItems }).subscribe(message => { - this.notification.openSnackMessageAction(message, undo, NodeActionsService[`SNACK_MESSAGE_DURATION${withUndo}`]) - .onAction() - .subscribe(() => this.deleteCopy(newItems)); - }); + const message = this.translation.instant(i18nMessageString, { success: numberOfCopiedItems, failed: failedItems }); + this.notification.openSnackMessageAction(message, undo, NodeActionsService[`SNACK_MESSAGE_DURATION${withUndo}`]) + .onAction() + .subscribe(() => this.deleteCopy(newItems)); } private deleteCopy(nodes: MinimalNodeEntity[]) { @@ -138,9 +137,8 @@ export class NodeCopyDirective { i18nMessageString = 'APP.MESSAGES.ERRORS.PERMISSION'; } - this.translation.get(i18nMessageString).subscribe(message => { - this.notification.openSnackMessageAction(message, '', NodeActionsService.SNACK_MESSAGE_DURATION); - }); + const message = this.translation.instant(i18nMessageString); + this.notification.openSnackMessageAction(message, '', NodeActionsService.SNACK_MESSAGE_DURATION); } ); } diff --git a/src/app/common/directives/node-delete.directive.ts b/src/app/common/directives/node-delete.directive.ts index 291cced24..bce80a8c9 100644 --- a/src/app/common/directives/node-delete.directive.ts +++ b/src/app/common/directives/node-delete.directive.ts @@ -65,24 +65,21 @@ export class NodeDeleteDirective { .subscribe( (data) => { const processedData = this.processStatus(data); + const message = this.getDeleteMessage(processedData); + const withUndo = processedData.someSucceeded ? this.translation.instant('APP.ACTIONS.UNDO') : ''; - this.getDeleteMessage(processedData) - .subscribe((message) => { - const withUndo = processedData.someSucceeded ? this.translation.translate.instant('APP.ACTIONS.UNDO') : ''; + this.notification.openSnackMessageAction(message, withUndo, NodeDeleteDirective.DELETE_MESSAGE_DURATION) + .onAction() + .subscribe(() => this.restore(processedData.success)); - this.notification.openSnackMessageAction(message, withUndo, NodeDeleteDirective.DELETE_MESSAGE_DURATION) - .onAction() - .subscribe(() => this.restore(processedData.success)); - - if (processedData.someSucceeded) { - this.content.nodeDeleted.next(null); - } - }); + if (processedData.someSucceeded) { + this.content.nodeDeleted.next(null); + } } ); } - private restore(items): void { + private restore(items: any[]): void { const batch = []; items.forEach((item) => { @@ -95,12 +92,10 @@ export class NodeDeleteDirective { const processedData = this.processStatus(data); if (processedData.failed.length) { - this.getRestoreMessage(processedData) - .subscribe((message) => { - this.notification.openSnackMessageAction( - message, '' , NodeDeleteDirective.RESTORE_MESSAGE_DURATION - ); - }); + const message = this.getRestoreMessage(processedData); + this.notification.openSnackMessageAction( + message, '' , NodeDeleteDirective.RESTORE_MESSAGE_DURATION + ); } if (processedData.someSucceeded) { @@ -178,39 +173,39 @@ export class NodeDeleteDirective { ); } - private getRestoreMessage(status): Observable { + private getRestoreMessage(status): string { if (status.someFailed && !status.oneFailed) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.ERRORS.NODE_RESTORE_PLURAL', { number: status.failed.length } ); } if (status.oneFailed) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.ERRORS.NODE_RESTORE', { name: status.failed[0].name } ); } } - private getDeleteMessage(status): Observable { + private getDeleteMessage(status): string { if (status.allFailed && !status.oneFailed) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.ERRORS.NODE_DELETION_PLURAL', { number: status.failed.length } ); } if (status.allSucceeded && !status.oneSucceeded) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.INFO.NODE_DELETION.PLURAL', { number: status.success.length } ); } if (status.someFailed && status.someSucceeded && !status.oneSucceeded) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.INFO.NODE_DELETION.PARTIAL_PLURAL', { success: status.success.length, @@ -220,7 +215,7 @@ export class NodeDeleteDirective { } if (status.someFailed && status.oneSucceeded) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.INFO.NODE_DELETION.PARTIAL_SINGULAR', { success: status.success.length, @@ -230,14 +225,14 @@ export class NodeDeleteDirective { } if (status.oneFailed && !status.someSucceeded) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.ERRORS.NODE_DELETION', { name: status.failed[0].name } ); } if (status.oneSucceeded && !status.someFailed) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.INFO.NODE_DELETION.SINGULAR', { name: status.success[0].name } ); diff --git a/src/app/common/directives/node-move.directive.ts b/src/app/common/directives/node-move.directive.ts index d3fab7451..d2d6a91a0 100644 --- a/src/app/common/directives/node-move.directive.ts +++ b/src/app/common/directives/node-move.directive.ts @@ -128,20 +128,21 @@ export class NodeMoveDirective { const initialParentId = this.nodeActionsService.getEntryParentId(this.selection[0].entry); - this.translation.get( + const messages = this.translation.instant( [successMessage, partialSuccessMessage, failedMessage], - { success: succeeded, failed: failures, partially: partiallySucceeded}).subscribe(messages => { + { success: succeeded, failed: failures, partially: partiallySucceeded} + ); - this.notification.openSnackMessageAction( - messages[successMessage] - + beforePartialSuccessMessage + messages[partialSuccessMessage] - + beforeFailedMessage + messages[failedMessage], - undo, - NodeActionsService[`SNACK_MESSAGE_DURATION${withUndo}`] - ) - .onAction() - .subscribe(() => this.revertMoving(moveResponse, initialParentId)); - }); + // TODO: review in terms of i18n + this.notification.openSnackMessageAction( + messages[successMessage] + + beforePartialSuccessMessage + messages[partialSuccessMessage] + + beforeFailedMessage + messages[failedMessage], + undo, + NodeActionsService[`SNACK_MESSAGE_DURATION${withUndo}`] + ) + .onAction() + .subscribe(() => this.revertMoving(moveResponse, initialParentId)); } getErrorMessage(errorObject): string { @@ -207,10 +208,8 @@ export class NodeMoveDirective { i18nMessageString = 'APP.MESSAGES.ERRORS.PERMISSION'; } - this.translation.get(i18nMessageString).subscribe(message => { - this.notification.openSnackMessage( - message, NodeActionsService.SNACK_MESSAGE_DURATION); - }); + const message = this.translation.instant(i18nMessageString); + this.notification.openSnackMessage(message, NodeActionsService.SNACK_MESSAGE_DURATION); } ); } diff --git a/src/app/common/directives/node-permanent-delete.directive.spec.ts b/src/app/common/directives/node-permanent-delete.directive.spec.ts index e5246daaa..755f632b4 100644 --- a/src/app/common/directives/node-permanent-delete.directive.spec.ts +++ b/src/app/common/directives/node-permanent-delete.directive.spec.ts @@ -78,7 +78,7 @@ describe('NodePermanentDeleteDirective', () => { beforeEach(() => { nodesService = alfrescoService.getInstance().nodes; - spyOn(translation, 'get').and.returnValue(Observable.of('message')); + spyOn(translation, 'instant').and.returnValue(Observable.of('message')); spyOn(notificationService, 'openSnackMessage').and.returnValue({}); spyOn(dialog, 'open').and.returnValue({ @@ -138,7 +138,7 @@ describe('NodePermanentDeleteDirective', () => { tick(); expect(notificationService.openSnackMessage).toHaveBeenCalled(); - expect(translation.get).toHaveBeenCalledWith( + expect(translation.instant).toHaveBeenCalledWith( 'APP.MESSAGES.INFO.TRASH.NODES_PURGE.PARTIAL_SINGULAR', { name: 'name1', failed: 2 } ); @@ -175,7 +175,7 @@ describe('NodePermanentDeleteDirective', () => { tick(); expect(notificationService.openSnackMessage).toHaveBeenCalled(); - expect(translation.get).toHaveBeenCalledWith( + expect(translation.instant).toHaveBeenCalledWith( 'APP.MESSAGES.INFO.TRASH.NODES_PURGE.PARTIAL_PLURAL', { number: 2, failed: 2 } ); @@ -193,7 +193,7 @@ describe('NodePermanentDeleteDirective', () => { tick(); expect(notificationService.openSnackMessage).toHaveBeenCalled(); - expect(translation.get).toHaveBeenCalledWith( + expect(translation.instant).toHaveBeenCalledWith( 'APP.MESSAGES.INFO.TRASH.NODES_PURGE.SINGULAR', { name: 'name1' } ); @@ -211,7 +211,7 @@ describe('NodePermanentDeleteDirective', () => { tick(); expect(notificationService.openSnackMessage).toHaveBeenCalled(); - expect(translation.get).toHaveBeenCalledWith( + expect(translation.instant).toHaveBeenCalledWith( 'APP.MESSAGES.ERRORS.TRASH.NODES_PURGE.SINGULAR', { name: 'name1' } ); @@ -238,7 +238,7 @@ describe('NodePermanentDeleteDirective', () => { tick(); expect(notificationService.openSnackMessage).toHaveBeenCalled(); - expect(translation.get).toHaveBeenCalledWith( + expect(translation.instant).toHaveBeenCalledWith( 'APP.MESSAGES.INFO.TRASH.NODES_PURGE.PLURAL', { number: 2 } ); @@ -265,7 +265,7 @@ describe('NodePermanentDeleteDirective', () => { tick(); expect(notificationService.openSnackMessage).toHaveBeenCalled(); - expect(translation.get).toHaveBeenCalledWith( + expect(translation.instant).toHaveBeenCalledWith( 'APP.MESSAGES.ERRORS.TRASH.NODES_PURGE.PLURAL', { number: 2 } ); diff --git a/src/app/common/directives/node-permanent-delete.directive.ts b/src/app/common/directives/node-permanent-delete.directive.ts index a9aa7f90a..501976ae1 100644 --- a/src/app/common/directives/node-permanent-delete.directive.ts +++ b/src/app/common/directives/node-permanent-delete.directive.ts @@ -116,15 +116,13 @@ export class NodePermanentDeleteDirective { } private purgeNotification(status): void { - this.getPurgeMessage(status) - .subscribe((message) => { - this.notification.openSnackMessage(message, 3000); - }); + const message = this.getPurgeMessage(status); + this.notification.openSnackMessage(message, 3000); } - private getPurgeMessage(status): Observable { + private getPurgeMessage(status): string { if (status.oneSucceeded && status.someFailed && !status.oneFailed) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.INFO.TRASH.NODES_PURGE.PARTIAL_SINGULAR', { name: status.success[0].name, @@ -134,7 +132,7 @@ export class NodePermanentDeleteDirective { } if (status.someSucceeded && !status.oneSucceeded && status.someFailed) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.INFO.TRASH.NODES_PURGE.PARTIAL_PLURAL', { number: status.success.length, @@ -144,28 +142,28 @@ export class NodePermanentDeleteDirective { } if (status.oneSucceeded) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.INFO.TRASH.NODES_PURGE.SINGULAR', { name: status.success[0].name } ); } if (status.oneFailed) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.ERRORS.TRASH.NODES_PURGE.SINGULAR', { name: status.fail[0].name } ); } if (status.allSucceeded) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.INFO.TRASH.NODES_PURGE.PLURAL', { number: status.success.length } ); } if (status.allFailed) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.ERRORS.TRASH.NODES_PURGE.PLURAL', { number: status.fail.length } ); diff --git a/src/app/common/directives/node-restore.directive.spec.ts b/src/app/common/directives/node-restore.directive.spec.ts index 8a3958b7a..9a7c55708 100644 --- a/src/app/common/directives/node-restore.directive.spec.ts +++ b/src/app/common/directives/node-restore.directive.spec.ts @@ -84,7 +84,7 @@ describe('NodeRestoreDirective', () => { nodesService = alfrescoService.getInstance().nodes; coreApi = alfrescoService.getInstance().core; - spyOn(translation, 'get').and.returnValue(Observable.of('message')); + spyOn(translation, 'instant').and.returnValue(Observable.of('message')); }); it('does not restore nodes if no selection', () => { @@ -209,7 +209,7 @@ describe('NodeRestoreDirective', () => { element.triggerEventHandler('click', null); tick(); - expect(translation.get).toHaveBeenCalledWith( + expect(translation.instant).toHaveBeenCalledWith( 'APP.MESSAGES.ERRORS.TRASH.NODES_RESTORE.PARTIAL_PLURAL', { number: 2 } ); @@ -229,7 +229,7 @@ describe('NodeRestoreDirective', () => { element.triggerEventHandler('click', null); tick(); - expect(translation.get).toHaveBeenCalledWith( + expect(translation.instant).toHaveBeenCalledWith( 'APP.MESSAGES.ERRORS.TRASH.NODES_RESTORE.NODE_EXISTS', { name: 'name1' } ); @@ -249,7 +249,7 @@ describe('NodeRestoreDirective', () => { element.triggerEventHandler('click', null); tick(); - expect(translation.get).toHaveBeenCalledWith( + expect(translation.instant).toHaveBeenCalledWith( 'APP.MESSAGES.ERRORS.TRASH.NODES_RESTORE.GENERIC', { name: 'name1' } ); @@ -269,7 +269,7 @@ describe('NodeRestoreDirective', () => { element.triggerEventHandler('click', null); tick(); - expect(translation.get).toHaveBeenCalledWith( + expect(translation.instant).toHaveBeenCalledWith( 'APP.MESSAGES.ERRORS.TRASH.NODES_RESTORE.LOCATION_MISSING', { name: 'name1' } ); @@ -296,7 +296,7 @@ describe('NodeRestoreDirective', () => { element.triggerEventHandler('click', null); tick(); - expect(translation.get).toHaveBeenCalledWith( + expect(translation.instant).toHaveBeenCalledWith( 'APP.MESSAGES.INFO.TRASH.NODES_RESTORE.PLURAL' ); })); @@ -313,7 +313,7 @@ describe('NodeRestoreDirective', () => { element.triggerEventHandler('click', null); tick(); - expect(translation.get).toHaveBeenCalledWith( + expect(translation.instant).toHaveBeenCalledWith( 'APP.MESSAGES.INFO.TRASH.NODES_RESTORE.SINGULAR', { name: 'name1' } ); diff --git a/src/app/common/directives/node-restore.directive.ts b/src/app/common/directives/node-restore.directive.ts index dfd480126..cb0ff38b3 100644 --- a/src/app/common/directives/node-restore.directive.ts +++ b/src/app/common/directives/node-restore.directive.ts @@ -190,11 +190,11 @@ export class NodeRestoreDirective { ); } - private getRestoreMessage(): Observable { + private getRestoreMessage(): string { const { restoreProcessStatus: status } = this; if (status.someFailed && !status.oneFailed) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.ERRORS.TRASH.NODES_RESTORE.PARTIAL_PLURAL', { number: status.fail.length @@ -204,14 +204,14 @@ export class NodeRestoreDirective { if (status.oneFailed && status.fail[0].statusCode) { if (status.fail[0].statusCode === 409) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.ERRORS.TRASH.NODES_RESTORE.NODE_EXISTS', { name: status.fail[0].entry.name } ); } else { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.ERRORS.TRASH.NODES_RESTORE.GENERIC', { name: status.fail[0].entry.name @@ -221,7 +221,7 @@ export class NodeRestoreDirective { } if (status.oneFailed && !status.fail[0].statusCode) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.ERRORS.TRASH.NODES_RESTORE.LOCATION_MISSING', { name: status.fail[0].entry.name @@ -230,11 +230,11 @@ export class NodeRestoreDirective { } if (status.allSucceeded && !status.oneSucceeded) { - return this.translation.get('APP.MESSAGES.INFO.TRASH.NODES_RESTORE.PLURAL'); + return this.translation.instant('APP.MESSAGES.INFO.TRASH.NODES_RESTORE.PLURAL'); } if (status.allSucceeded && status.oneSucceeded) { - return this.translation.get( + return this.translation.instant( 'APP.MESSAGES.INFO.TRASH.NODES_RESTORE.SINGULAR', { name: status.success[0].entry.name @@ -246,13 +246,11 @@ export class NodeRestoreDirective { private restoreNotification(): void { const status = Object.assign({}, this.restoreProcessStatus); const action = (status.oneSucceeded && !status.someFailed) ? this.translation.translate.instant('APP.ACTIONS.VIEW') : ''; + const message = this.getRestoreMessage(); - this.getRestoreMessage() - .subscribe((message) => { - this.notification.openSnackMessageAction(message, action, 3000) - .onAction() - .subscribe(() => this.navigateLocation(status.success[0].entry.path)); - }); + this.notification.openSnackMessageAction(message, action, 3000) + .onAction() + .subscribe(() => this.navigateLocation(status.success[0].entry.path)); } private refresh(): void { diff --git a/src/app/common/directives/node-versions.directive.ts b/src/app/common/directives/node-versions.directive.ts index 169e114c1..93c209f35 100644 --- a/src/app/common/directives/node-versions.directive.ts +++ b/src/app/common/directives/node-versions.directive.ts @@ -71,8 +71,8 @@ export class NodeVersionsDirective { VersionManagerDialogAdapterComponent, { data: { contentEntry }, panelClass: 'adf-version-manager-dialog', width: '630px' }); } else { - const translatedErrorMessage: any = this.translation.get('APP.MESSAGES.ERRORS.PERMISSION'); - this.notification.openSnackMessage(translatedErrorMessage.value, 4000); + const translatedErrorMessage = this.translation.instant('APP.MESSAGES.ERRORS.PERMISSION'); + this.notification.openSnackMessage(translatedErrorMessage, 4000); this.nodeVersionError.emit(); }