Skip to content

Commit 3a8ce70

Browse files
devversionwagnermaciel
authored andcommitted
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. (cherry picked from commit 972283c)
1 parent 17a41ac commit 3a8ce70

File tree

13 files changed

+262
-266
lines changed

13 files changed

+262
-266
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
"@angular-devkit/schematics": "^12.0.0",
7777
"@angular/bazel": "^12.0.0",
7878
"@angular/compiler-cli": "^12.0.0",
79-
"@angular/dev-infra-private": "https://github.com/angular/dev-infra-private-builds.git#3953ba9eea35a2660b6068523bafb92634042be9",
79+
"@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
)

src/dev-app/main.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
/**
10-
* This is the main entry-point for the AOT compilation. File will be used to test AOT support.
11-
*/
9+
// Load `$localize` for examples using it.
10+
import '@angular/localize/init';
1211

1312
import {platformBrowser} from '@angular/platform-browser';
1413
import {MainModuleNgFactory} from './main-module.ngfactory';

tools/system-config-tmpl.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ function setupFrameworkPackages() {
126126
// When running with Ivy, we need to load the ngcc processed UMD bundles.
127127
// These are stored in the `__ivy_ngcc_` folder that has been generated
128128
// since we run ngcc with `--create-ivy-entry-points`. Filter out the compiler
129-
// package because it won't be processed by ngcc.
130-
if (isRunningWithIvy && entryPointName !== '@angular/compiler') {
129+
// and localize package because it won't be processed by ngcc.
130+
if (isRunningWithIvy && entryPointName !== '@angular/compiler' &&
131+
!entryPointName.startsWith('@angular/localize')) {
131132
bundlePath = '__ivy_ngcc__/' + bundlePath;
132133
}
133134
packagesConfig[entryPointName] = {

0 commit comments

Comments
 (0)