Last active
November 8, 2022 16:54
-
-
Save jgcarmona-com/b715587705a6da1c1591a86223940646 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; | |
import { Inject, Injectable, LOCALE_ID } from '@angular/core'; | |
import { Observable } from 'rxjs'; | |
@Injectable() | |
export class LanguageInterceptor implements HttpInterceptor { | |
currentLocale: string; | |
constructor(@Inject(LOCALE_ID) public locale: string) { | |
this.currentLocale = this.locale.split('-', 1)[0]; | |
} | |
intercept( | |
req: HttpRequest<any>, | |
next: HttpHandler | |
): Observable<HttpEvent<any>> { | |
// Clone the request to add the new header | |
const clonedRequest = req.clone({ | |
headers: req.headers.set('Accept-Language', this.currentLocale) | |
}); | |
// Pass the cloned request instead of the original request to the next handle | |
return next.handle(clonedRequest); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment