Skip to content

Instantly share code, notes, and snippets.

@FunnyGhost
Created December 13, 2021 15:27
Show Gist options
  • Save FunnyGhost/58800610338c9a850c175f1669d229a5 to your computer and use it in GitHub Desktop.
Save FunnyGhost/58800610338c9a850c175f1669d229a5 to your computer and use it in GitHub Desktop.
Server implementation of the Language Service
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