2022-07-22 17:27:06 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
|
|
import parser from "accept-language-parser";
|
2023-08-22 12:34:55 +00:00
|
|
|
import type { GetServerSidePropsContext, NextApiRequest } from "next";
|
2022-07-22 17:27:06 +00:00
|
|
|
|
2023-08-22 12:34:55 +00:00
|
|
|
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { Maybe } from "@calcom/trpc/server";
|
2022-07-22 17:27:06 +00:00
|
|
|
|
|
|
|
const { i18n } = require("@calcom/config/next-i18next.config");
|
|
|
|
|
2023-08-22 12:34:55 +00:00
|
|
|
export async function getLocaleFromRequest(
|
|
|
|
req: NextApiRequest | GetServerSidePropsContext["req"]
|
|
|
|
): Promise<string> {
|
|
|
|
const session = await getServerSession({ req });
|
|
|
|
if (session?.user?.locale) return session.user.locale;
|
2022-07-22 17:27:06 +00:00
|
|
|
let preferredLocale: string | null | undefined;
|
|
|
|
if (req.headers["accept-language"]) {
|
2023-08-22 12:34:55 +00:00
|
|
|
preferredLocale = parser.pick(i18n.locales, req.headers["accept-language"], {
|
|
|
|
loose: true,
|
|
|
|
}) as Maybe<string>;
|
2022-07-22 17:27:06 +00:00
|
|
|
}
|
|
|
|
return preferredLocale ?? i18n.defaultLocale;
|
|
|
|
}
|
2023-08-02 09:35:48 +00:00
|
|
|
|
|
|
|
// Workaround for using router.locales from old router
|
|
|
|
export const locales = i18n.locales as string[];
|
|
|
|
|
|
|
|
export const localeOptions = locales.map((locale) => ({
|
|
|
|
value: locale,
|
|
|
|
label: new Intl.DisplayNames(locale, { type: "language" }).of(locale) || "",
|
|
|
|
}));
|