Skip to content

Instantly share code, notes, and snippets.

@jgcarmona-com
Last active November 8, 2022 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgcarmona-com/b715587705a6da1c1591a86223940646 to your computer and use it in GitHub Desktop.
Save jgcarmona-com/b715587705a6da1c1591a86223940646 to your computer and use it in GitHub Desktop.
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