2021-09-23 08:49:17 +00:00
|
|
|
import parser from "accept-language-parser";
|
|
|
|
import { IncomingMessage } from "http";
|
|
|
|
|
|
|
|
import { getSession } from "@lib/auth";
|
|
|
|
import prisma from "@lib/prisma";
|
|
|
|
|
|
|
|
import { i18n } from "../../../next-i18next.config";
|
|
|
|
|
2021-10-08 11:43:48 +00:00
|
|
|
export const getOrSetUserLocaleFromHeaders = async (req: IncomingMessage) => {
|
|
|
|
const session = await getSession({ req });
|
2021-09-23 08:49:17 +00:00
|
|
|
const preferredLocale = parser.pick(i18n.locales, req.headers["accept-language"]);
|
|
|
|
|
|
|
|
if (session?.user?.id) {
|
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: {
|
|
|
|
id: session.user.id,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
locale: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user?.locale) {
|
|
|
|
return user.locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preferredLocale) {
|
|
|
|
await prisma.user.update({
|
|
|
|
where: {
|
|
|
|
id: session.user.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
locale: preferredLocale,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await prisma.user.update({
|
|
|
|
where: {
|
|
|
|
id: session.user.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
locale: i18n.defaultLocale,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preferredLocale) {
|
|
|
|
return preferredLocale;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i18n.defaultLocale;
|
|
|
|
};
|
|
|
|
|
|
|
|
interface localeType {
|
|
|
|
[locale: string]: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const localeLabels: localeType = {
|
|
|
|
en: "English",
|
2021-10-08 11:43:48 +00:00
|
|
|
fr: "French",
|
|
|
|
it: "Italian",
|
|
|
|
ru: "Russian",
|
|
|
|
es: "Spanish",
|
|
|
|
de: "German",
|
|
|
|
pt: "Portuguese",
|
2021-09-23 08:49:17 +00:00
|
|
|
ro: "Romanian",
|
2021-10-08 11:43:48 +00:00
|
|
|
nl: "Dutch",
|
2021-09-23 08:49:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type OptionType = {
|
|
|
|
value: string;
|
|
|
|
label: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const localeOptions: OptionType[] = i18n.locales.map((locale) => {
|
|
|
|
return { value: locale, label: localeLabels[locale] };
|
|
|
|
});
|