From e98d9043630d16a79541180de284dfca0e1669a3 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Fri, 5 Jan 2018 21:26:09 +0000 Subject: [PATCH] [ADF-1999] i18n support for file size pipe (#2803) * i18n support for fileSize pipe * remove unused declaration * update unit test --- .../datatable/filesize-cell.component.ts | 3 +-- lib/core/i18n/en.json | 11 +++++++++++ lib/core/i18n/ru.json | 13 ++++++++++++- lib/core/pipes/file-size.pipe.spec.ts | 18 +++++++++++++++++- lib/core/pipes/file-size.pipe.ts | 13 ++++++++++--- lib/core/services/translation.service.ts | 4 ++++ 6 files changed, 55 insertions(+), 7 deletions(-) diff --git a/lib/core/datatable/components/datatable/filesize-cell.component.ts b/lib/core/datatable/components/datatable/filesize-cell.component.ts index 4413ace72e..350be11fe9 100644 --- a/lib/core/datatable/components/datatable/filesize-cell.component.ts +++ b/lib/core/datatable/components/datatable/filesize-cell.component.ts @@ -15,12 +15,11 @@ * limitations under the License. */ -import { ChangeDetectionStrategy, Component, ViewEncapsulation } from '@angular/core'; +import { Component, ViewEncapsulation } from '@angular/core'; import { DataTableCellComponent } from './datatable-cell.component'; @Component({ selector: 'adf-filesize-cell', - changeDetection: ChangeDetectionStrategy.OnPush, template: ` {{ value | adfFileSize }} diff --git a/lib/core/i18n/en.json b/lib/core/i18n/en.json index 7cd077d03f..8f1aa31427 100644 --- a/lib/core/i18n/en.json +++ b/lib/core/i18n/en.json @@ -1,5 +1,16 @@ { "CORE": { + "FILE_SIZE": { + "BYTES": "Bytes", + "KB": "KB", + "MB": "MB", + "GB": "GB", + "TB": "TB", + "PB": "PB", + "EB": "EB", + "ZB": "ZB", + "YB": "YB" + }, "PAGINATION": { "ITEMS_RANGE": "Showing {{ range }} of {{ total }}", "ITEMS_PER_PAGE": "Items per page", diff --git a/lib/core/i18n/ru.json b/lib/core/i18n/ru.json index 1de39c030a..b2ff415533 100644 --- a/lib/core/i18n/ru.json +++ b/lib/core/i18n/ru.json @@ -1,5 +1,16 @@ { "CORE": { + "FILE_SIZE": { + "BYTES": "Б", + "KB": "кБ", + "MB": "МБ", + "GB": "ГБ", + "TB": "ТБ", + "PB": "ПБ", + "EB": "ЭБ", + "ZB": "ЗБ", + "YB": "ЙБ" + }, "PAGINATION": { "ITEMS_RANGE": "Отображается {{ range }} из {{ total }}", "ITEMS_PER_PAGE": "Элементов на странице", @@ -148,4 +159,4 @@ "LOADING": "Загружается", "UNKNOWN_FORMAT": "Не удалось загрузить предварительный просмотр" } -} \ No newline at end of file +} diff --git a/lib/core/pipes/file-size.pipe.spec.ts b/lib/core/pipes/file-size.pipe.spec.ts index 0e177ad691..e2b0a2f0a8 100644 --- a/lib/core/pipes/file-size.pipe.spec.ts +++ b/lib/core/pipes/file-size.pipe.spec.ts @@ -22,7 +22,23 @@ describe('FileSizePipe', () => { let pipe: FileSizePipe; beforeEach(() => { - pipe = new FileSizePipe(); + const translation: any = { + instant(key) { + const enUs = { + 'CORE.FILE_SIZE.BYTES': 'Bytes', + 'CORE.FILE_SIZE.KB': 'KB', + 'CORE.FILE_SIZE.MB': 'MB', + 'CORE.FILE_SIZE.GB': 'GB', + 'CORE.FILE_SIZE.TB': 'TB', + 'CORE.FILE_SIZE.PB': 'PB', + 'CORE.FILE_SIZE.EB': 'EB', + 'CORE.FILE_SIZE.ZB': 'ZB', + 'CORE.FILE_SIZE.YB': 'YB' + }; + return enUs[key]; + } + }; + pipe = new FileSizePipe(translation); }); it('returns empty string with invalid input', () => { diff --git a/lib/core/pipes/file-size.pipe.ts b/lib/core/pipes/file-size.pipe.ts index ac28ba22fb..9a8d52a285 100644 --- a/lib/core/pipes/file-size.pipe.ts +++ b/lib/core/pipes/file-size.pipe.ts @@ -16,12 +16,17 @@ */ import { Pipe, PipeTransform } from '@angular/core'; +import { TranslationService } from '../services/translation.service'; @Pipe({ - name: 'adfFileSize' + name: 'adfFileSize', + pure: false }) export class FileSizePipe implements PipeTransform { + constructor(private translation: TranslationService) { + } + transform(bytes: number, decimals: number = 2): string { if (bytes == null || bytes === undefined) { return ''; @@ -33,10 +38,12 @@ export class FileSizePipe implements PipeTransform { const k = 1024, dm = decimals || 2, - sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], + sizes = ['BYTES', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], i = Math.floor(Math.log(bytes) / Math.log(k)); - return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; + const i18nSize = this.translation.instant(`CORE.FILE_SIZE.${sizes[i]}`); + + return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + i18nSize; } } diff --git a/lib/core/services/translation.service.ts b/lib/core/services/translation.service.ts index 99cd087744..5cba275066 100644 --- a/lib/core/services/translation.service.ts +++ b/lib/core/services/translation.service.ts @@ -91,4 +91,8 @@ export class TranslationService { get(key: string|Array, interpolateParams?: Object): Observable { return this.translate.get(key, interpolateParams); } + + instant(key: string | Array, interpolateParams?: Object): string | any { + return this.translate.instant(key, interpolateParams); + } }