Localize paginator

This commit is contained in:
Florian Dupret 2021-11-12 18:06:41 +01:00
commit a5ec79717b
3 changed files with 50 additions and 0 deletions

View file

@ -65,6 +65,9 @@ import { TooltipModule } from "primeng/tooltip";
import { TranslateHttpLoader } from "@ngx-translate/http-loader";
import { UnauthorizedInterceptor } from "./auth/unauthorized.interceptor";
import { environment } from "../environments/environment";
import { MatPaginatorIntl } from "@angular/material/paginator";
import { TranslateService } from "@ngx-translate/core";
import { MatPaginatorI18n } from "./localization/MatPaginatorI18n";
const routes: Routes = [
{ path: "*", component: PageNotFoundComponent },
@ -212,6 +215,10 @@ export function JwtTokenGetter() {
useClass: UnauthorizedInterceptor,
multi: true
},
{
provide: MatPaginatorIntl, deps: [TranslateService],
useFactory: (translateService: TranslateService) => new MatPaginatorI18n(translateService).getPaginatorIntl()
},
],
bootstrap: [AppComponent],
})

View file

@ -0,0 +1,34 @@
import { MatPaginatorIntl } from '@angular/material/paginator';
import { TranslateService } from '@ngx-translate/core';
export class MatPaginatorI18n {
constructor(private translate: TranslateService) { }
getPaginatorIntl(): MatPaginatorIntl {
const paginatorIntl = new MatPaginatorIntl();
paginatorIntl.itemsPerPageLabel = this.translate.instant('Paginator.itemsPerPageLabel');
paginatorIntl.nextPageLabel = this.translate.instant('Paginator.nextPageLabel');
paginatorIntl.previousPageLabel = this.translate.instant('Paginator.previousPageLabel');
paginatorIntl.firstPageLabel = this.translate.instant('Paginator.firstPageLabel');
paginatorIntl.lastPageLabel = this.translate.instant('Paginator.lastPageLabel');
paginatorIntl.getRangeLabel = this.getRangeLabel.bind(this);
return paginatorIntl;
}
private getRangeLabel(page: number, pageSize: number, length: number): string {
if (length == 0 || pageSize == 0) {
return this.translate.instant('Paginator.rangePageLabel1', { length });
}
length = Math.max(length, 0);
const startIndex = page * pageSize;
// If the start index exceeds the list length, do not try and fix the end index to the end.
const endIndex =
startIndex < length ? Math.min(startIndex + pageSize, length) : startIndex + pageSize;
return this.translate.instant('Paginator.rangePageLabel2', { startIndex: startIndex + 1, endIndex, length });
}
}

View file

@ -408,6 +408,15 @@
"3": "Emby User",
"4": "Emby Connect User",
"5": "Jellyfin User"
},
"Paginator": {
"itemsPerPageLabel": "Items per page:",
"nextPageLabel": "Next page",
"previousPageLabel": "Previous page",
"firstPageLabel": "First page",
"lastPageLabel": "Last page",
"rangePageLabel1": "0 of {{length}}",
"rangePageLabel2": "{{startIndex}} {{endIndex}} of {{length}}"
}
}