Mario
Mario

Reputation: 4998

Menu item label translation not performed

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

Answers (2)

Alimdi
Alimdi

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

Mario
Mario

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

Related Questions