fix: Revert deletion of getLocaleFromRequest in #11534 (#11835)

pull/11804/head
Alex van Andel 2023-10-11 18:36:32 +01:00 committed by GitHub
parent d72a1ee328
commit df29376b0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 0 deletions

View File

@ -0,0 +1,22 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import parser from "accept-language-parser";
import type { GetServerSidePropsContext, NextApiRequest } from "next";
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import type { Maybe } from "@calcom/trpc/server";
const { i18n } = require("@calcom/config/next-i18next.config");
export async function getLocaleFromRequest(
req: NextApiRequest | GetServerSidePropsContext["req"]
): Promise<string> {
const session = await getServerSession({ req });
if (session?.user?.locale) return session.user.locale;
let preferredLocale: string | null | undefined;
if (req.headers["accept-language"]) {
preferredLocale = parser.pick(i18n.locales, req.headers["accept-language"], {
loose: true,
}) as Maybe<string>;
}
return preferredLocale ?? i18n.defaultLocale;
}