From 112edb2889bf02a26f233ea42c956f0a45d3de79 Mon Sep 17 00:00:00 2001 From: _Bastler <_Bastler@bstly.de> Date: Fri, 5 Nov 2021 18:40:18 +0100 Subject: [PATCH] add label paginator --- src/app/app.module.ts | 13 +++++++--- src/app/services/i18n.service.ts | 42 +++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 435ba12..a09d070 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -55,13 +55,13 @@ import { BorrowProvingComponent, BorrowProvingResultDialog } from './pages/borro import { DividerComponent } from './ui/divider/divider.component'; import { DividertestComponent } from './pages/dividertest/dividertest.component'; - -import { I18nService } from './services/i18n.service'; +import { I18nService, I18nPaginatorIntl } from './services/i18n.service'; import { MinetestAccountsComponent } from './pages/minetest/accounts/accounts.component'; import { BorrowComponent } from './pages/borrow/borrow.component'; import { DurationpickerComponent } from './ui/durationpicker/durationpicker.component'; import { InviteCodeComponent } from './pages/invites/code/code.component'; import { InviteEditComponent } from './pages/invites/edit/invite.edit'; +import { MatPaginatorIntl } from '@angular/material/paginator'; export function init_app(i18n: I18nService) { @@ -134,7 +134,14 @@ export class XhrInterceptor implements HttpInterceptor { QrCodeModule, ], exports: [ MaterialModule ], - providers: [ { provide: APP_INITIALIZER, useFactory: init_app, deps: [ I18nService ], multi: true }, { provide: HTTP_INTERCEPTORS, useClass: XhrInterceptor, multi: true }, DatePipe ], + providers: [ { provide: APP_INITIALIZER, useFactory: init_app, deps: [ I18nService ], multi: true }, { provide: HTTP_INTERCEPTORS, useClass: XhrInterceptor, multi: true }, DatePipe, + { + provide: MatPaginatorIntl, useFactory: (i18n) => { + const service = new I18nPaginatorIntl(); + service.injectI18n(i18n) + return service; + }, deps: [ I18nService ] + } ], bootstrap: [ AppComponent ], }) export class AppModule { diff --git a/src/app/services/i18n.service.ts b/src/app/services/i18n.service.ts index 1b88be1..3e844b7 100644 --- a/src/app/services/i18n.service.ts +++ b/src/app/services/i18n.service.ts @@ -1,6 +1,8 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { environment } from '../../environments/environment'; +import { MatPaginatorIntl } from '@angular/material/paginator'; +import { Subject } from 'rxjs'; @Injectable({ providedIn: 'root', @@ -96,10 +98,48 @@ export class I18nService { insertArguments(label: string, args: string[]) { if (args) { for (let index in args) { - label = label.replace(`{${index}}`, this.get(args[ index ], [])); + label = label.replace(`{${index}}`, this.get(args[ index ], null)); } } return label; } +} + + +@Injectable() +export class I18nPaginatorIntl implements MatPaginatorIntl { + + changes = new Subject(); + + i18n: I18nService; + + itemsPerPageLabel: string; + nextPageLabel: string; + previousPageLabel: string; + firstPageLabel: string; + lastPageLabel: string; + + + injectI18n(i18n: I18nService) { + this.i18n = i18n; + + this.firstPageLabel = this.i18n.get('paginator.firstPage', []); + this.itemsPerPageLabel = this.i18n.get('paginator.itemsPerPage', []); + this.lastPageLabel = this.i18n.get('paginator.lastPage', []); + + this.nextPageLabel = this.i18n.get('paginator.nextPage', []); + this.previousPageLabel = this.i18n.get('paginator.previousPage', []); + + } + + + getRangeLabel(page: number, pageSize: number, length: number): string { + if (length === 0) { + return this.i18n.get('paginator.empty', []); + } + + const amountPages = Math.ceil(length / pageSize); + return this.i18n.get('paginator.range', [ page + 1 + "", amountPages + "" ]); + } } \ No newline at end of file