mirror of
https://github.com/gristlabs/grist-core.git
synced 2024-10-27 20:44:07 +00:00
17 lines
624 B
TypeScript
17 lines
624 B
TypeScript
|
import {parse as languageParser} from "accept-language-parser";
|
||
|
import {Request} from 'express';
|
||
|
import {locales} from 'app/common/Locales';
|
||
|
|
||
|
/**
|
||
|
* Returns the locale from a request, falling back to `defaultLocale`
|
||
|
* if unable to determine the locale.
|
||
|
*/
|
||
|
export function localeFromRequest(req: Request, defaultLocale: string = 'en-US') {
|
||
|
const language = languageParser(req.headers["accept-language"] as string)[0];
|
||
|
if (!language) { return defaultLocale; }
|
||
|
|
||
|
const locale = `${language.code}-${language.region}`;
|
||
|
const supports = locales.some(l => l.code === locale);
|
||
|
return supports ? locale : defaultLocale;
|
||
|
}
|