79201162

Date: 2024-11-18 19:22:09
Score: 0.5
Natty:
Report link

This code works; you can try it. However, there is no need for the title to be readonly

CustomTitleStrategy

@Injectable({providedIn: 'root'})

export class CustomTitleStrategy extends TitleStrategy {

constructor(private title: Title, private translate:TranslateService) {
    super();
}

updateTitle(routerSnapshot: RouterStateSnapshot) {
    const title = this.buildTitle(routerSnapshot);

    if (title) {
        const translatedTitle = this.translate.instant('titles.site-name') + ' | ' + this.translate.instant(`titles.${title}`);
        this.title.setTitle(translatedTitle);
    } else {
        const translatedTitle = this.translate.instant('titles.site-name');
        this.title.setTitle(translatedTitle);
    }
}

}

In Angular, define a provider to override the default TitleStrategy. Use CustomTitleStrategy as the implementation by specifying it with the provide and useClass properties in the provider configuration

export class CoreModule {
   
    static forRoot(): ModuleWithProviders<CoreModule> {
        return {
            ngModule: CoreModule,
            providers: [
                {
                    provide: TitleStrategy,
                    useClass: CustomTitleStrategy,
                },
            ]
        };
    }
}

language json

"titles": {
     "site-name": "stackoverflow",
     "other": "Other Code"
}
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Önder Yekebağ