import { Component , OnInit , RendererFactory2 , Renderer2 } from '@angular/core' ; import { Title } from '@angular/platform-browser' ; import { Router , ActivatedRouteSnapshot , NavigationEnd } from '@angular/router' ; import { TranslateService , LangChangeEvent } from '@ngx-translate/core' ; import dayjs from 'dayjs/esm' ; import { AccountService } from 'app/core/auth/account.service' ; @ Component ( { selector : 'jhi-main' , templateUrl : './main.component.html' , } ) export class MainComponent implements OnInit { private renderer : Renderer2 ; constructor ( private accountService , private titleService , private router , private translateService , rootRenderer ) { this . renderer = rootRenderer . createRenderer ( document . querySelector ( 'html' ) , null ) ; } ngOnInit ( ) { this . accountService . identity ( ) . subscribe ( ) ; this . router . events . subscribe ( event => { if ( event instanceof NavigationEnd ) { this . updateTitle ( ) ; } } ) ; this . translateService . onLangChange . subscribe ( ( langChangeEvent ) => { this . updateTitle ( ) ; dayjs . locale ( langChangeEvent . lang ) ; this . renderer . setAttribute ( document . querySelector ( 'html' ) , 'lang' , langChangeEvent . lang ) ; } ) ; } private getPageTitle ( routeSnapshot ) { const title = routeSnapshot . data [ 'pageTitle' ] ?? '' ; if ( routeSnapshot . firstChild ) { return this . getPageTitle ( routeSnapshot . firstChild ) || title ; } return title ; } private updateTitle ( ) { let pageTitle = this . getPageTitle ( this . router . routerState . snapshot . root ) ; if ( ! pageTitle ) { pageTitle = 'global.title' ; } this . translateService . get ( pageTitle ) . subscribe ( title => this . titleService . setTitle ( title ) ) ; } }