vivek
vivek

Reputation: 11

Is it possible to intercept static file like .html with Angular interceptor? I wish to change lang attribute

I found this Angular doc about http intercepting. https://angular.io/guide/http#intercepting-requests-and-responses I want to intercept html in Angular interceptor so that i can change the html file before it's rendered to the browser. i tried with server but it's very complicated since i will have to change whole project structure.
Basically,
I wish to change <html lang=" "> based on language selected by user. I have already implemented this.document.documentElement.lang = lang;, this is changing the lang attribute but when i go to view-source the lang attribute does not change.
I tried to follow solution from the below link but i don't understand this solution and couldn't find the method createServerRenderer() as mentioned in that solution. Angular set html lang atttribute in SSR build


Any help or direction will be appreciated greatly, Thanks

Upvotes: 1

Views: 428

Answers (2)

David
David

Reputation: 34465

Try setting the lang value like this, which will work for both client and server side

constructor(private renderer: Renderer2,@Inject(DOCUMENT) private _document: any)
{
}

ngOnInit()
{
  const langValue = "en";//Assign correct value here
  this.renderer.setAttribute(this._document.body.parentNode, 'lang', langValue);

Upvotes: 1

Ben L
Ben L

Reputation: 744

For using ngrx-translate and doing work before angular bootstraps try using APP_INITIALIZER like this:

export function initApp(translate: TranslateService) {
   const browserLang = translate.getBrowserLang();
   translate.use(browserLang);
}

@NgModule({
declarations: [ ],
imports: [ ],
providers: [
{ 
  provide : APP_INITIALIZER, 
  multi : true,  
  deps : [TranslateService],
  useFactory : initApp
 }
],
bootstrap: [AppComponent]
})
export class AppModule { }

Upvotes: 0

Related Questions