mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-09-17 14:21:29 +00:00
[ADF-1999] i18n support for file size pipe (#2803)
* i18n support for fileSize pipe * remove unused declaration * update unit test
This commit is contained in:
committed by
Eugenio Romano
parent
f9fb7dc7dd
commit
e98d904363
@@ -15,12 +15,11 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { ChangeDetectionStrategy, Component, ViewEncapsulation } from '@angular/core';
|
import { Component, ViewEncapsulation } from '@angular/core';
|
||||||
import { DataTableCellComponent } from './datatable-cell.component';
|
import { DataTableCellComponent } from './datatable-cell.component';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'adf-filesize-cell',
|
selector: 'adf-filesize-cell',
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
||||||
template: `
|
template: `
|
||||||
<ng-container>
|
<ng-container>
|
||||||
<span [title]="tooltip">{{ value | adfFileSize }}</span>
|
<span [title]="tooltip">{{ value | adfFileSize }}</span>
|
||||||
|
@@ -1,5 +1,16 @@
|
|||||||
{
|
{
|
||||||
"CORE": {
|
"CORE": {
|
||||||
|
"FILE_SIZE": {
|
||||||
|
"BYTES": "Bytes",
|
||||||
|
"KB": "KB",
|
||||||
|
"MB": "MB",
|
||||||
|
"GB": "GB",
|
||||||
|
"TB": "TB",
|
||||||
|
"PB": "PB",
|
||||||
|
"EB": "EB",
|
||||||
|
"ZB": "ZB",
|
||||||
|
"YB": "YB"
|
||||||
|
},
|
||||||
"PAGINATION": {
|
"PAGINATION": {
|
||||||
"ITEMS_RANGE": "Showing {{ range }} of {{ total }}",
|
"ITEMS_RANGE": "Showing {{ range }} of {{ total }}",
|
||||||
"ITEMS_PER_PAGE": "Items per page",
|
"ITEMS_PER_PAGE": "Items per page",
|
||||||
|
@@ -1,5 +1,16 @@
|
|||||||
{
|
{
|
||||||
"CORE": {
|
"CORE": {
|
||||||
|
"FILE_SIZE": {
|
||||||
|
"BYTES": "Б",
|
||||||
|
"KB": "кБ",
|
||||||
|
"MB": "МБ",
|
||||||
|
"GB": "ГБ",
|
||||||
|
"TB": "ТБ",
|
||||||
|
"PB": "ПБ",
|
||||||
|
"EB": "ЭБ",
|
||||||
|
"ZB": "ЗБ",
|
||||||
|
"YB": "ЙБ"
|
||||||
|
},
|
||||||
"PAGINATION": {
|
"PAGINATION": {
|
||||||
"ITEMS_RANGE": "Отображается {{ range }} из {{ total }}",
|
"ITEMS_RANGE": "Отображается {{ range }} из {{ total }}",
|
||||||
"ITEMS_PER_PAGE": "Элементов на странице",
|
"ITEMS_PER_PAGE": "Элементов на странице",
|
||||||
|
@@ -22,7 +22,23 @@ describe('FileSizePipe', () => {
|
|||||||
let pipe: FileSizePipe;
|
let pipe: FileSizePipe;
|
||||||
|
|
||||||
beforeEach(() => {
|
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', () => {
|
it('returns empty string with invalid input', () => {
|
||||||
|
@@ -16,12 +16,17 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Pipe, PipeTransform } from '@angular/core';
|
import { Pipe, PipeTransform } from '@angular/core';
|
||||||
|
import { TranslationService } from '../services/translation.service';
|
||||||
|
|
||||||
@Pipe({
|
@Pipe({
|
||||||
name: 'adfFileSize'
|
name: 'adfFileSize',
|
||||||
|
pure: false
|
||||||
})
|
})
|
||||||
export class FileSizePipe implements PipeTransform {
|
export class FileSizePipe implements PipeTransform {
|
||||||
|
|
||||||
|
constructor(private translation: TranslationService) {
|
||||||
|
}
|
||||||
|
|
||||||
transform(bytes: number, decimals: number = 2): string {
|
transform(bytes: number, decimals: number = 2): string {
|
||||||
if (bytes == null || bytes === undefined) {
|
if (bytes == null || bytes === undefined) {
|
||||||
return '';
|
return '';
|
||||||
@@ -33,10 +38,12 @@ export class FileSizePipe implements PipeTransform {
|
|||||||
|
|
||||||
const k = 1024,
|
const k = 1024,
|
||||||
dm = decimals || 2,
|
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));
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -91,4 +91,8 @@ export class TranslationService {
|
|||||||
get(key: string|Array<string>, interpolateParams?: Object): Observable<string|any> {
|
get(key: string|Array<string>, interpolateParams?: Object): Observable<string|any> {
|
||||||
return this.translate.get(key, interpolateParams);
|
return this.translate.get(key, interpolateParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
instant(key: string | Array<string>, interpolateParams?: Object): string | any {
|
||||||
|
return this.translate.instant(key, interpolateParams);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user