【问题标题】:Angular Material MatPaginator can not be translatedAngular Material MatPaginator 无法翻译
【发布时间】:2020-05-08 23:25:24
【问题描述】:

我想在 matpaginator 标签中翻译 itemsPerPageLabel、nextPageLabel、previousPageLabel、firstPageLabel、lastPageLabel

但我试图创建这样的类,但它不起作用。我的意思是订阅有效,但是当我更改语言时,标签无法更改

@Injectable()
export class MatPaginatorCustom extends MatPaginatorIntl {
    constructor(public translateService: TranslateService) {
        super();
        this.translateService.stream('vehicle.ubdate.matSnackBar.message')
            .subscribe(labelValue => {
                console.log(labelValue);
                this.itemsPerPageLabel = labelValue;
            });
    }
}

我使用 Angular 8 和 ngx-translate

【问题讨论】:

    标签: angular angular-material translate ngx-translate


    【解决方案1】:

    您可以在构造函数中使用 onLangChange 方法。 例如:

    @Injectable()
    export class MatPaginatorCustom extends MatPaginatorIntl {
    constructor(public translateService: TranslateService) {
        super();
        this.translateService.onLangChange.subscribe(event => {
    
                this.translate.get('vehicle.ubdate.matSnackBar.message')
                   .subscribe(labelValue => {
                console.log(labelValue);
                this.itemsPerPageLabel = labelValue;
               
                // must call this method after change label.
                this.changes.next();
            });
    
         });
    }
    

    }

    【讨论】:

    • 不幸的是仍然不起作用,我必须刷新页面,然后我看到一个不同的标签,例如itemsPerPageLabel.
    • 对不起,我忘了使用“this.changes.next();”内订阅。请再试一次。
    猜你喜欢
    • 1970-01-01
    • 2018-04-30
    • 2020-06-09
    • 2018-04-02
    • 2021-03-13
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    相关资源
    最近更新 更多