mirror of https://github.com/ghostfolio/ghostfolio
Browse Source
* Eliminate unresolved page title and RangeNotSatisfiableError * Update changelogpull/7371/head^2
committed by
GitHub
8 changed files with 60 additions and 30 deletions
@ -0,0 +1,37 @@ |
|||||
|
import { environment } from '@ghostfolio/api/environments/environment'; |
||||
|
import { |
||||
|
DEFAULT_LANGUAGE_CODE, |
||||
|
SUPPORTED_LANGUAGE_CODES |
||||
|
} from '@ghostfolio/common/config'; |
||||
|
|
||||
|
import { NextFunction, Request, Response } from 'express'; |
||||
|
import { StatusCodes } from 'http-status-codes'; |
||||
|
|
||||
|
export function languageRedirectMiddleware( |
||||
|
request: Request, |
||||
|
response: Response, |
||||
|
next: NextFunction |
||||
|
) { |
||||
|
if ( |
||||
|
!environment.production || |
||||
|
request.path !== '/' || |
||||
|
!['GET', 'HEAD'].includes(request.method) |
||||
|
) { |
||||
|
return next(); |
||||
|
} |
||||
|
|
||||
|
let languageCode = DEFAULT_LANGUAGE_CODE; |
||||
|
|
||||
|
try { |
||||
|
const code = request.headers['accept-language'].split(',')[0].split('-')[0]; |
||||
|
|
||||
|
if ((SUPPORTED_LANGUAGE_CODES as readonly string[]).includes(code)) { |
||||
|
languageCode = code; |
||||
|
} |
||||
|
} catch {} |
||||
|
|
||||
|
return response.redirect( |
||||
|
StatusCodes.MOVED_PERMANENTLY, |
||||
|
`/${languageCode}${request.url.slice(1)}` |
||||
|
); |
||||
|
} |
||||
Loading…
Reference in new issue