Skip to content

Commit

Permalink
Merge pull request #1070 from ORCID/persist-accept-language-header
Browse files Browse the repository at this point in the history
add persisting Accept-Language header
  • Loading branch information
bobcaprice authored Dec 20, 2023
2 parents 78ca64d + 253edc7 commit 9d70368
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions ui/src/app/account/service/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export class AccountService {
if (response && response.body) {
this.authenticated = true
const account: IAccount = response.body
if (account.langKey) {
localStorage.setItem('langCode', account.langKey)
}
this.accountData.next(account)

// After retrieve the account info, the language will be changed to
Expand Down
11 changes: 9 additions & 2 deletions ui/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BrowserModule } from '@angular/platform-browser'

import { AppRoutingModule } from './app-routing.module'
import { AppComponent } from './app.component'
import { HttpClientModule } from '@angular/common/http'
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'
import { AccountModule } from './account/account.module'
import { NgxWebstorageModule } from 'ngx-webstorage'
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome'
Expand All @@ -14,6 +14,7 @@ import { HasAnyAuthorityDirective } from './shared/directive/has-any-authority.d
import { HomeModule } from './home/home.module'
import { FooterComponent } from './layout/footer/footer.component'
import { SharedModule } from './shared/shared.module'
import { HeaderInterceptor } from './shared/interceptor/header.interceptor'

@NgModule({
declarations: [AppComponent, NavbarComponent, HasAnyAuthorityDirective, FooterComponent],
Expand All @@ -29,7 +30,13 @@ import { SharedModule } from './shared/shared.module'
NgbModule,
SharedModule.forRoot(),
],
providers: [],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: HeaderInterceptor,
multi: true,
},
],
bootstrap: [AppComponent],
})
export class AppModule {}
21 changes: 21 additions & 0 deletions ui/src/app/shared/interceptor/header.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@angular/core'
import { HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'

@Injectable()
export class HeaderInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler) {
const langCode = localStorage.getItem('langCode')

if (langCode) {
const request = req.clone({
headers: req.headers.set('Accept-Language', langCode),
})

// send cloned request with header to the next handler.
return next.handle(request)
}

// pass through unaltered request
return next.handle(req)
}
}

0 comments on commit 9d70368

Please sign in to comment.