Jet
BuyFollow on X
  • Home
  • Satisfied Requirements
  • License
  • Architecture
    • Overview
    • Internal Dependency Graph
    • External Dependencies
    • Conventions
  • Recipes
    • Get Started
    • Add a Page
    • Customise Fonts
    • Customise Icons
    • Customise Languages
    • Customise Themes
    • Add an Environment Variable
  • Classes
    • Jet Mat Paginator Intl
    • Transloco HTTP Loader
  • Components
    • App Component
    • Footer Component
    • Home Page Component
    • Message Page Component
    • Not Found Page Component
    • Page Component
    • Profile Page Component
    • Reset Password Page Component
    • Settings Page Component
    • Sign In Page Component
    • Sign Out Page Component
    • Sign Up Page Component
    • Update Password Page Component
  • Constants
    • Color Scheme Options
    • Default Color Scheme Option
    • Default Language Option
    • Default Settings
    • Language Options
    • Navigation Menu Items
  • Directives
    • Analytics Directive
  • Enums
    • Bucket
    • LocalStorage Key
    • Query Param
    • SessionStorage Key
    • Table
  • Guards
    • Is Authenticated
    • Is Not Authenticated
  • Interfaces
    • Color Scheme Option
    • Language Option
    • Navigation Menu Item
    • Profile
    • Progress Bar Configuration
    • Settings
  • Services
    • Alert Service
    • Analytics Service
    • Logger Service
    • Profile Service
    • Progress Bar Service
    • Service Worker Service
    • Settings Service
    • Storage Service
    • Supabase Service
    • Toolbar Title Service
    • User Service
  • Types
    • Available Color Scheme
    • Available Font
    • Available Language
    • Available OAuth Provider
Powered by GitBook
On this page

Was this helpful?

  1. Recipes

Add a Page

PreviousGet StartedNextCustomise Fonts

Last updated 8 months ago

Was this helpful?

Let's say you want to add a new page called Account.

Step 1: Generate a new component in the components folder with the page suffix to maintain consistency.

Terminal
ng g c components/account-page

Step 2: Enable routing to this component by adding it to app.routes.ts. Ensure that you import from @jet/components for easy refactoring in the future.

app.routes.ts
export const routes: Routes = [
  { component: HomePageComponent, path: '' },
  { component: SettingsPageComponent, path: 'settings' },
  { component: AccountPageComponent, path: 'account' },
  { path: '**', redirectTo: '/' },
];

Step 3: Show a link to this page in the side navigation and bottom navigation by adding it to this.pages in app.component.ts. The order here will determine the order in which pages appear in both the navigations. Any can be used for the icon property.

app.component.ts
this.pages = [
  {
    icon: 'home',
    nameKey: 'home',
    url: '/',
  },
  { icon: 'account_circle', nameKey: 'account', url: '/account' },
  {
    icon: 'settings',
    nameKey: 'settings',
    url: '/settings',
  },
];

Step 4: On top of this.pages, you'll notice a special comment. It helps notify Transloco Keys Manager of dynamic translation keys. Add the nameKey value to that list, since nameKey is used in the template to dynamically refer to a translation key ({{ t(page.nameKey) }}).

app.component.ts
/**
 * Dynamic keys to include in translations (https://github.com/jsverse/transloco-keys-manager?tab=readme-ov-file#dynamic-keys):
 *
 * t(jet-app.home)
 * t(jet-app.settings)
 * t(jet-app.account)
 */

Step 5: In the template of the new component (account-page.component.html), enclose the generated content in an ng-container element for Transloco to work. We use the value of selector in the @Component decorator for prefix for consistency. This will scope all translation keys detected in this template under jet-account-page.

account-page.component.html
<ng-container *transloco="let t; prefix: 'jet-account-page'">
  <p>account-page works!</p>
</ng-container>

Step 6: Import TranslocoModule and sort the keys alphabetically in the @Component decorator in account-page.component.ts.

account-page.component.ts
@Component({
  imports: [
    TranslocoModule,
  ],
  selector: 'jet-account-page',
  standalone: true,
  styleUrl: './account-page.component.scss',
  templateUrl: './account-page.component.html',
})

Step 7: Add Page Component to the template.

account-page.component.ts
export class AccountPageComponent {
  public constructor(
    private readonly _loggerService: LoggerService,
  ) {
    this._loggerService.logComponentInitialization('AccountPageComponent');
  }
}

Step 8: Update the spec file (account-page.component.spec.ts).

account-page.component.spec.ts
await TestBed.configureTestingModule({
  imports: [TranslocoTestingModule.forRoot({}), AccountPageComponent],
  providers: [
    { provide: LoggerService, useClass: LoggerServiceMock },
    { provide: TitleService, useClass: TitleServiceMock },
  ],
}).compileComponents();

Step 9: Run i18n:extract script in package.json. This will add the missing keys with sample values to your translation files. Update the values in all translation files.

Terminal
npm run i18n:extract
ar.json
"jet-account-page": {
  "title": "Missing value for 'jet-account-page.title'"
},
"jet-app": {
  "account": "Missing value for 'jet-app.account'"
},
en.json
"jet-account-page": {
  "title": "Missing value for 'jet-account-page.title'"
},
"jet-app": {
  "account": "Missing value for 'jet-app.account'"
},

Done!

Material Icon
An Account Page Component in LTR language and font, dark theme and mobile layout
An Account Page Component in LTR language and font, light theme and desktop layout
An Account Page Component in LTR language and font, dark theme and mobile layout
An Account Page Component in LTR language and font, light theme and desktop layout