Skip to content

Commit 972283c

Browse files
authored
docs: add example for providing custom MatPaginatorIntl (#22771)
Adds an example for providing a custom `MatPaginatorIntl`. The example also show-cases the use with `@angular/localize`. Fixes #8239.
1 parent 1305587 commit 972283c

File tree

13 files changed

+182
-5
lines changed

13 files changed

+182
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"@angular/bazel": "^12.0.0",
7878
"@angular/compiler-cli": "^12.0.0",
7979
"@angular/dev-infra-private": "https://github.com/angular/dev-infra-private-builds.git#2e0271af93b020a811323fbc274afcf588dbc91e",
80+
"@angular/localize": "^12.0.0",
8081
"@angular/platform-browser-dynamic": "^12.0.0",
8182
"@angular/platform-server": "^12.0.0",
8283
"@angular/router": "^12.0.0",

packages.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ VERSION_PLACEHOLDER_REPLACEMENTS = {
2525
# List of default Angular library UMD bundles which are not processed by ngcc.
2626
ANGULAR_NO_NGCC_BUNDLES = [
2727
("@angular/compiler", ["compiler.umd.js"]),
28+
("@angular/localize", ["localize.umd.js", "localize-init.umd.js"]),
2829
]
2930

3031
# List of Angular library UMD bundles which will are processed by ngcc.

scripts/check-rollup-globals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const filesToCheckGlob = [
3030
'!src/+(e2e-app|universal-app|dev-app)/**/*.ts',
3131
'!src/**/schematics/**/*.ts',
3232
'!src/**/tests/**/*.ts',
33+
'!src/components-examples/private/localize-types.d.ts',
3334
];
3435

3536
const failures = new Map<string, string[]>();

src/components-examples/material/paginator/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ng_module(
1616
deps = [
1717
"//src/cdk/testing",
1818
"//src/cdk/testing/testbed",
19+
"//src/components-examples/private:localize_types",
1920
"//src/material/input",
2021
"//src/material/paginator",
2122
"//src/material/paginator/testing",

src/components-examples/material/paginator/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@ import {
88
} from './paginator-configurable/paginator-configurable-example';
99
import {PaginatorOverviewExample} from './paginator-overview/paginator-overview-example';
1010
import {PaginatorHarnessExample} from './paginator-harness/paginator-harness-example';
11+
import {
12+
PaginatorIntlExample,
13+
PaginatorIntlExampleModule,
14+
} from './paginator-intl/paginator-intl-example';
1115

1216
export {
1317
PaginatorConfigurableExample,
1418
PaginatorHarnessExample,
19+
PaginatorIntlExample,
1520
PaginatorOverviewExample,
1621
};
1722

1823
const EXAMPLES = [
1924
PaginatorConfigurableExample,
2025
PaginatorHarnessExample,
26+
// PaginatorIntlExample is imported through it's own example module.
2127
PaginatorOverviewExample,
2228
];
2329

@@ -26,6 +32,7 @@ const EXAMPLES = [
2632
CommonModule,
2733
MatInputModule,
2834
MatPaginatorModule,
35+
PaginatorIntlExampleModule,
2936
FormsModule,
3037
],
3138
declarations: EXAMPLES,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<mat-paginator [length]="0" [pageSizeOptions]="[10, 50, 100]">
2+
</mat-paginator>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {Component, Injectable, NgModule} from '@angular/core';
2+
import {MatPaginatorIntl, MatPaginatorModule} from '@angular/material/paginator';
3+
import {Subject} from 'rxjs';
4+
5+
@Injectable()
6+
export class MyCustomPaginatorIntl implements MatPaginatorIntl {
7+
changes = new Subject<void>();
8+
9+
// For internationalization, the `$localize` function from
10+
// the `@angular/localize` package can be used.
11+
firstPageLabel = $localize`First page`;
12+
itemsPerPageLabel = $localize`Items per page:`;
13+
lastPageLabel = $localize`Last page`;
14+
15+
// You can set labels to an arbitrary string too, or dynamically compute
16+
// it through other third-party internationalization libraries.
17+
nextPageLabel = 'Next page';
18+
previousPageLabel = 'Previous page';
19+
20+
getRangeLabel(page: number, pageSize: number, length: number): string {
21+
if (length === 0) {
22+
return $localize`Page 1 of 1`;
23+
}
24+
const amountPages = Math.ceil(length / pageSize);
25+
return $localize`Page ${page + 1} of ${amountPages}`;
26+
}
27+
}
28+
29+
/**
30+
* @title Paginator internationalization
31+
*/
32+
@Component({
33+
selector: 'paginator-intl-example',
34+
templateUrl: 'paginator-intl-example.html',
35+
})
36+
export class PaginatorIntlExample {}
37+
38+
@NgModule({
39+
imports: [MatPaginatorModule],
40+
declarations: [PaginatorIntlExample],
41+
providers: [
42+
{provide: MatPaginatorIntl, useClass: MyCustomPaginatorIntl}
43+
]
44+
})
45+
export class PaginatorIntlExampleModule {}

src/components-examples/private/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ ts_library(
1111
"@npm//@angular/core",
1212
],
1313
)
14+
15+
ts_library(
16+
name = "localize_types",
17+
srcs = ["localize-types.d.ts"],
18+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {LocalizeFn} from '@angular/localize/src/localize';
2+
3+
declare global {
4+
const $localize: LocalizeFn;
5+
}

src/dev-app/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ ng_module(
9696
"//src/dev-app/virtual-scroll",
9797
"//src/dev-app/youtube-player",
9898
"//src/material/core",
99+
"@npm//@angular/localize",
99100
"@npm//@angular/router",
100101
],
101102
)

0 commit comments

Comments
 (0)