Created
December 13, 2021 15:27
-
-
Save FunnyGhost/58800610338c9a850c175f1669d229a5 to your computer and use it in GitHub Desktop.
Server implementation of the Language Service
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 { Inject, Injectable } from '@angular/core'; | |
import { LanguageService, LanguageShortName } from 'somewhere'; | |
import { REQUEST } from '@nguniversal/express-engine/tokens'; | |
import { Request } from 'express'; | |
@Injectable() | |
export class ServerLanguageService extends LanguageService { | |
constructor(@Inject(REQUEST) private request: Request) { | |
super(); | |
} | |
getLanguage(defaultLanguage: LanguageShortName): LanguageShortName { | |
const preferredLanguage = this.request.headers['accept-language'] | |
?.split(',')?.[0] | |
?.substring(0, 2) as LanguageShortName; | |
return this.SUPPORTED_LANGUAGES.includes(preferredLanguage) | |
? preferredLanguage | |
: defaultLanguage; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment