How to translate label and routerlink in Angular2 with i18n inside constructor

I am new in Angular2 and it is my first time working with internazionalitation so I am stuck and I need your help, please.

I ´ve already translated text inside HTML and now I am working with internazionalitation of .ts files.

My question is: ¿How can I translate label and routelink inside a constructor of a .ts file?

I would like to get this translated:

 constructor(private doctypesService: DoctypesService, private categoriasService: CategoriesService, 
          private metadataService: MetadataService, private breadcrumbService: BreadcrumbService, private i18n: I18n) {
this.breadcrumbService.setItems([
    { label: 'Administración' },
    { label: 'Categorías', routerLink: ['/categorias'] }
]);

The two labels and the routerlink.

I ´ve translated values of a table header inside the ts file with this approach and it works, but I don t know how to do it with the labels and routerlink. Thanks for your help

This is what I did in on init to translate headers:

ngOnInit() {

this.refreshCategorias();

this.refreshSubcategorias();

this.refreshMetadatos();

this.cols = [
    { field: 'code', header: this.i18n('código')},
    { field: 'name', header: this.i18n('nombre') },
    { field: 'description', header: this.i18n('descripción')}
];

}

Of course, I had to make this import to make it work

import { I18n } from '@ngx-translate/i18n-polyfill';

Upvotes: 0

Views: 588

Answers (1)

Abhishek Ekaanth
Abhishek Ekaanth

Reputation: 2567

You can translate it by creating an instance of TranslateService and calling the instant()

Example:

     constructor(private doctypesService: DoctypesService, private categoriasService: CategoriesService, 
          private metadataService: MetadataService, private breadcrumbService: BreadcrumbService, public translate: TranslateService) {
this.breadcrumbService.setItems([
    { label: this.translate.instant('Administración') },
    { label: this.translate.instant('Categorías'), routerLink: ['/categorias'] }
]);

Upvotes: 1

Related Questions