Creating multilingual menus with ABP and Nebular

Currently, I am in the process of developing an application using ABP framework version 4.4 and integrating the Nebular theme as opposed to the default basic theme. Amidst various challenges faced during this migration, one particular issue stands out - localizing menu items. In typical HTML, we utilize

{{'::Menu:Home' | abpLocalization }}
for localization purposes. However, this approach proves inefficient when working with Nebular. For instance:

import { NbMenuItem } from '@nebular/theme';

export const MENU_ITEMS: NbMenuItem[] = [
  {
    title: '"::Menu:Home" | abpLocalization',
    icon: 'home-outline',
    link: '/',
    home: true,
  },
];

Despite incorporating the ABP localization pipe into the title property, the UI displays plain text instead of the localized content. It is worth noting that Nebular's menu title property exclusively accepts string values, posing a challenge on how to effectively localize menu items. Any suggestions or workarounds would be greatly appreciated.

Answer №1

To ensure the keys are translated before being passed to the menu component, utilize the localization service:

import {
  LocalizationService
} from '@abp/ng.core';

class MyClass {
  public readonly MENU_ITEMS: NbMenuItem[] = [{
    title: this.localizationService.instant('::Menu:Home'),
    icon: 'home-outline',
    link: '/',
    home: true,
  }];

  constructor(private localizationService: LocalizationService) {}
}

Keep in mind that exporting a const directly from the file's root is not possible due to this approach.

To address this issue, you can implement the following solution:

import { Component. OnInit } from '@angular/core';
import { LocalizationService } from '@abp/ng.core';
import { MENU_ITEMS } from './pages-menu';

@Component({
    selector: 'app-sidebar-menu',
    template:  <nb-menu [items]="menu"></nb-menu> ,
    styleUrls: ['./sidebar-menu.component.scss']
})
export class SidebarMenuComponent implements OnInit {
    menu: NbMenuItem[];

    constructor(private localizationService: LocalizationService) {}

    ngOnInit(): void {
        const menuTranslated = [...MENU_ITEMS];
        menuTranslated.forEach(item => item.title = this.localizationService.instant(item.title));
        this.menu = menuTranslated;
    }
}

In your ./pages_menu file, construct the menu using translation keys.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for customizing components in React-Table by overriding default columns

In a nutshell, I was tasked with developing a table component using react-table. By default, the table uses an input component that allows instant typing when double-clicked. Additionally, I wanted one of the columns in editableCell to use a dropdown. I ...

Utilize the optional chaining feature when accessing properties that may be optional

I recently encountered an issue in my Typescript project where accessing properties or functions of optional properties did not throw any errors. Here is an example: type Example = { bar?: string[] } const foo: Example = {} // Although no error occu ...

When it comes to TypeScript, there is a limitation in assigning a value to an object key with type narrowing through the

I created a function called `hasOwnProperty` with type narrowing: function hasOwnProperty< Obj extends Record<string, any>, Prop extends PropertyKey, >( obj: Obj, prop: Prop, ): obj is Obj & Record<Prop, any> { return Object ...

``Is there a specific scenario where the use of getInitialProps is recommended when automatically redirecting from one

Within my application, I have set up an auto-redirect from the root directory '/' to '/PageOne' with the following code: const Home = () => { const router = useRouter(); useEffect(() => { router.push('/pageone', ...

I encountered an error in my Spring Boot application where I received a TypeError: Cannot read properties of undefined (reading '0') related to the angular.min.js file at line 129

As I work on designing a login page using Angular in my Java Spring Boot application, I encounter an issue. When attempting to log into the panel page, the username and password are successfully sent to the application via the API controller and the user t ...

Exploring the dynamics of parent and child components in Angular

I'm currently working on an Angular2 project and I've hit a roadblock with the parent/child component interaction. My goal is to have a "Producer" entity that can have multiple "Products". Ultimately, I aim to retrieve lists of products associat ...

Limitations of MaterialUI Slider

Looking for a solution to distribute 350 points across 8 sliders, each with a range of 0-100 and 5 marks at 0, 25, 50, 75, and 100. With each step consuming or returning 25 points, the challenge lies in allowing users to adjust the points allocation withou ...

TypeORM is failing to create a table upon initialization

Recently, I delved into the realm of typescript and decided to explore TypeORM for backend development. My current project involves building a CRUD API using typeORM and postgreSQL. After configuring everything initially, I ran the application only to rea ...

How to transfer a value from an observable to a (click) event handler in Angular2

I am currently trying to grasp the concepts of observable programming within Angular 2, but one crucial point eludes me - how can I pass the value from an observable into the (click) event at the template level without subscribing to the observable in the ...

Utilizing ngx-bootstrap Modal for Data Transfer to Component

I am currently dealing with an array called drivers in my component. When a button is clicked in the component, it triggers a ngx-bootstrap modal to display the fullname value from the drivers array. Now, I am looking for a way to retrieve the selected nam ...

Transfer the view encapsulation id selector to a CSS class within an @Input directive passed down from the parent component

Alright, we all know that using ng-deep is not ideal and that ViewEncapsulation.None can lead to messy code. So here's the dilemma I'm facing. Let's say I have a component with the following; @Input() cssClasses: string; Now, when I use t ...

Encountering a Nuxt error where properties of null are being attempted to be read, specifically the 'addEventListener' property. As a result, both the default

Currently, I am utilizing nuxt.js along with vuesax as my UI framework. I made particular adjustments to my default.vue file located in the /layouts directory by incorporating a basic navbar template example from vuesax. Subsequently, I employed @nuxtjs/ ...

What is the method for retrieving array values from an attribute?

I am currently developing an Angular 6 application and I need to pass and retrieve array values dynamically through attributes. Here is the code snippet I have used for this purpose: HTML: <ul class="list-unstyled" id="list" [attr.parent_id]="123"> ...

Guide on how to upload files to a web server using Angular frontend only

My website's frontend is built with Angular and it allows users to upload files. I am looking for a way to store these uploaded files on the web server where my website is currently hosted. Ideally, I would like to accomplish this using just Angular, ...

Exploring TypeScript Generics and the Concept of Function Overloading

How can I create a factory function that returns another function and accepts either one or two generic types (R and an optional P) in TypeScript? If only one generic type is provided, the factory function should return a function with the shape () => ...

Exploring nested contexts in react testing library with renderHook

This is a sample code snippet in TypeScript that uses React and Testing Library: import React, { FC, useEffect, useMemo, useState } from 'react'; import { renderHook, waitFor } from '@testing-library/react'; interface PropsCtx { inpu ...

Utilizing the Teams web app within my customized electron application

I've been developing an electron app and am trying to integrate the MS Teams web app into it. I've successfully displayed MS Word, MS Powerpoint, and other apps using window.loadURL(''), but I'm experiencing issues with MS Teams. D ...

Tips for sending arguments to translations

I am currently implementing vuejs 3 using TS. I have set up my translation files in TypeScript as shown below: index.ts: export default { 'example': 'example', } To use the translations, I simply do: {{ $t('example') }} N ...

Issue with implementing JQuery datepicker within Angular 7 CLI application

I've been working on my application and trying to implement the jQuery datepicker functionality. It's an Angular CLI app, and I have installed jquery-datepicker and jquery using npm. Here is a snippet of the dependencies in my package.json: "@a ...

Using template variable in Angular5 to create a dependant dropdown feature

I have a component where I am subscribing to an observable that contains nested levels of JSON objects. How can I create multiple dropdown selects that depend on the previous selection? I am using template variables to reference the selects. this._exposu ...