dev*_*054 44 angular-material2 angular
我正在使用MatPaginator组件,我正在试图弄清楚如何翻译这些标签(文档对此不够清楚).
我发现这篇文章显示了如何执行此操作,并按照以下步骤操作:
1 - 我创建了一个名为的文件custom-paginator.ts并在其中放置以下内容:
import { MatPaginator, MatPaginatorIntl } from '@angular/material';
export class CustomPaginator extends MatPaginatorIntl {
constructor() {
super();
this.nextPageLabel = ' My new label for next page';
this.previousPageLabel = ' My new label for previous page';
this.itemsPerPageLabel = 'Task per screen';
}
}
Run Code Online (Sandbox Code Playgroud)
2 - 在app.module.ts我说:
@NgModule({
// ...
providers: [
{
provide: MatPaginatorIntl,
useClass: CustomPaginator
}
]
})
export class AppModule
Run Code Online (Sandbox Code Playgroud)
但是,它根本不会改变任何东西.我错过了什么?
Vin*_*rih 57
你可以这样做:我给你提供克罗地亚标签:
customClass.ts
import {MatPaginatorIntl} from '@angular/material';
export class MatPaginatorIntlCro extends MatPaginatorIntl {
itemsPerPageLabel = 'Stavki po stranici';
nextPageLabel = 'Slijede?a stranica';
previousPageLabel = 'Prethodna stranica';
getRangeLabel = function (page, pageSize, length) {
if (length === 0 || pageSize === 0) {
return '0 od ' + 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 startIndex + 1 + ' - ' + endIndex + ' od ' + length;
};
}
Run Code Online (Sandbox Code Playgroud)
和AppModule.ts:
providers: [{ provide: MatPaginatorIntl, useClass: MatPaginatorIntlCro}],
Run Code Online (Sandbox Code Playgroud)
它很棒.
此外,您必须导入appModule.ts MatPaginatorIntl和MatPaginatorIntlCro
小智 27
基于Vinko Vorih代码,我创建了一个使用ngx-translate的分页器,这里是代码:
import { Injectable } from '@angular/core';
import { MatPaginatorIntl } from '@angular/material';
import { TranslateService } from "@ngx-translate/core";
export class PaginatorIntlService extends MatPaginatorIntl {
translate: TranslateService;
itemsPerPageLabel = 'Items per page';
nextPageLabel = 'Next page';
previousPageLabel = 'Previous page';
getRangeLabel = function (page, pageSize, length) {
const of = this.translate ? this.translate.instant('paginator.of') : 'of';
if (length === 0 || pageSize === 0) {
return '0 ' + of + ' ' + 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 startIndex + 1 + ' - ' + endIndex + ' ' + of + ' ' + length;
};
injectTranslateService(translate: TranslateService) {
this.translate = translate;
this.translate.onLangChange.subscribe(() => {
this.translateLabels();
});
this.translateLabels();
}
translateLabels() {
this.itemsPerPageLabel = this.translate.instant('paginator.items_per_page');
this.nextPageLabel = this.translate.instant('paginator.next_page');
this.previousPageLabel = this.translate.instant('paginator.previous_page');
}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的模块提供者条目中:
{
provide: MatPaginatorIntl,
useFactory: (translate) => {
const service = new PaginatorIntlService();
service.injectTranslateService(translate);
return service;
},
deps: [TranslateService]
}
Run Code Online (Sandbox Code Playgroud)
这样,您可以将翻译存储在常用的i18n文件中,如果您的Web应用程序能够动态更改区域设置,则将根据需要翻译paginator.
Ala*_*lan 11
在文件中:matPaginatorIntlCroClass.ts
import {MatPaginatorIntl} from '@angular/material';
export class MatPaginatorIntlCro extends MatPaginatorIntl {
itemsPerPageLabel = 'Items par page';
nextPageLabel = 'Page Prochaine';
previousPageLabel = 'Page Précedente';
}
Run Code Online (Sandbox Code Playgroud)
在File:AppModule.ts中:
import { MatPaginatorModule, MatPaginatorIntl} from '@angular/material';
import { MatPaginatorIntlCro } from './matPaginatorIntlCroClass'
@NgModule({
imports: [],
providers: [{ provide: MatPaginatorIntl, useClass: MatPaginatorIntlCro}],
..
})
Run Code Online (Sandbox Code Playgroud)
资料来源:https://material.angular.io/components/paginator/api
小智 10
如果您想使用带有ngx-translate的动态语言开关为您工作,这是解决方案,它对我有用。
mat-paginator-i18n.service.ts
import { MatPaginatorIntl } from '@angular/material';
import { TranslateService } from '@ngx-translate/core';
import { Injectable } from '@angular/core';
const ITEMS_PER_PAGE = 'Items per page:';
const NEXT_PAGE = 'Next page';
const PREV_PAGE = 'Previous page';
const FIRST_PAGE = 'First page';
const LAST_PAGE = 'Last page';
@Injectable()
export class MatPaginatorI18nService extends MatPaginatorIntl {
public constructor(private translate: TranslateService) {
super();
this.translate.onLangChange.subscribe((e: Event) => {
this.getAndInitTranslations();
});
this.getAndInitTranslations();
}
public getRangeLabel = (page: number, pageSize: number, length: number): string => {
if (length === 0 || pageSize === 0) {
return `0 / ${length}`;
}
length = Math.max(length, 0);
const startIndex: number = page * pageSize;
const endIndex: number = startIndex < length
? Math.min(startIndex + pageSize, length)
: startIndex + pageSize;
return `${startIndex + 1} - ${endIndex} / ${length}`;
};
public getAndInitTranslations(): void {
this.translate.get([
ITEMS_PER_PAGE,
NEXT_PAGE,
PREV_PAGE,
FIRST_PAGE,
LAST_PAGE,
])
.subscribe((translation: any) => {
this.itemsPerPageLabel = translation[ITEMS_PER_PAGE];
this.nextPageLabel = translation[NEXT_PAGE];
this.previousPageLabel = translation[PREV_PAGE];
this.firstPageLabel = translation[FIRST_PAGE];
this.lastPageLabel = translation[LAST_PAGE];
this.changes.next();
});
}
}
Run Code Online (Sandbox Code Playgroud)
在您的模块AppModule中
@NgModule({
// ...
providers: [
{
provide: MatPaginatorIntl,
useClass: MatPaginatorI18nService,
},
],
})
export class AppModule {
// ...
Run Code Online (Sandbox Code Playgroud)
en.json等
{
"Items per page:": "Items per page:",
"Next page": "Next page",
"Previous page": "Previous page",
"First page": "First page",
"Last page": "Last page",
}
Run Code Online (Sandbox Code Playgroud)
小智 5
当开始索引超过列表长度时,我进行了一些修改来修复结束索引。我还为第一页和最后一页添加了翻译。用于角/材质5.2.4分页组件。
import { Injectable } from '@angular/core';
import { MatPaginatorIntl } from '@angular/material';
import { TranslateService } from '@ngx-translate/core';
@Injectable()
export class MatPaginationIntlService extends MatPaginatorIntl {
translate: TranslateService;
firstPageLabel = 'First page';
itemsPerPageLabel = 'Items per page';
lastPageLabel = 'Last page';
nextPageLabel = 'Next page';
previousPageLabel = 'Previous page';
getRangeLabel = (page: number, pageSize: number, length: number): string => {
const of = this.translate ? this.translate.instant('mat-paginator-intl.of') : 'of';
if (length === 0 || pageSize === 0) {
return '0 ' + of + ' ' + length;
}
length = Math.max(length, 0);
const startIndex = ((page * pageSize) > length) ?
(Math.ceil(length / pageSize) - 1) * pageSize:
page * pageSize;
const endIndex = Math.min(startIndex + pageSize, length);
return startIndex + 1 + ' - ' + endIndex + ' ' + of + ' ' + length;
};
injectTranslateService(translate: TranslateService) {
this.translate = translate;
this.translate.onLangChange.subscribe(() => {
this.translateLabels();
});
this.translateLabels();
}
translateLabels() {
this.firstPageLabel = this.translate.instant('mat-paginator-intl.first_page');
this.itemsPerPageLabel = this.translate.instant('mat-paginator-intl.items_per_page');
this.lastPageLabel = this.translate.instant('mat-paginator-intl.last_page');
this.nextPageLabel = this.translate.instant('mat-paginator-intl.next_page');
this.previousPageLabel = this.translate.instant('mat-paginator-intl.previous_page');
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
为了刷新标签,您可以在更改标签后触发更改事件:
translateLabels() {
this.firstPageLabel = this.translate.instant('mat-paginator-intl.first_page');
...
this.changes.next();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18683 次 |
| 最近记录: |