Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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