Reputation: 4998
Given the following component
@Component({
selector: 'sidebar-menu',
templateUrl: './sidebar-menu.component.html',
styleUrls: ['./sidebar-menu.component.scss'],
})
export class SidebarMenuComponent implements OnInit {
userMenuItems: MenuItem[] = [];
constructor(private readonly service: TranslateService) {}
ngOnInit() {
this.userMenuItems = [
{
label: this.service.instant('menu.sidebar.user.global-user-management'),
routerLink: 'admin-users',
},
{
label: this.service.instant('menu.sidebar.user.centre-user-management'),
routerLink: '/',
},
];
}
}
I'm trying to get the labels of the menu items to be translated. But the above fails. What is the correct way for this translation to be applied?
Thanks in advance
Upvotes: 0
Views: 272
Reputation: 11
One option is to use stream
globalusermanagement: string = "";
ngOnInit() {
this.service.stream('global-user-management').subscribe((res: string) => {
this.globalusermanagement = res;
});
this.userMenuItems = [
{
label: this.service.stream('menu.sidebar.user.global-user-management'),
routerLink: 'admin-users',
}
];
Upvotes: 0
Reputation: 4998
The answer to this question is adequately answered in this publication. In summary. instant is synchronous and translations are not available. It is solved with .get
ngOnInit() {
this.service
.get([
'menu.sidebar.user.global-user-management',
'menu.sidebar.user.centre-user-management',
])
.subscribe({
next: () => {
this.userMenuItems = [
{
label: this.service.instant(
'menu.sidebar.user.global-user-management'
),
routerLink: 'admin-users',
},
{
label: this.service.instant(
'menu.sidebar.user.centre-user-management'
),
routerLink: '/',
},
];
},
});
}
Upvotes: 0