Setup i18n and locale detection (#712)

* feat: setup translations

* feat: i18n setup

* Update pages/settings/profile.tsx

Co-authored-by: Alex Johansson <[email protected]>

* fix: abstract locale hook

* fix: set default locale if preferred locale is not supported

* Revert "fix: set default locale if preferred locale is not supported"

This reverts commit e2a3d81371ee02a033520058a1d7d61cffeffc94.

* fix: set default locale if preferred locale is not supported

* fix: use 1 namespace and remove unnecessary logs

* fix: yarn.lock

* fix: linting errors

Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: Alex Johansson <[email protected]>
This commit is contained in:
authored and GitHub committed 2021-09-23 09:49:17 +01:00
1 parent 3764a9d462
commit 82e7e51fca
26 files changed
+801 -1685

No files matched your search

+4 -2
View File
@@ -1,9 +1,11 @@
import short from "short-uuid";
import { v5 as uuidv5 } from "uuid";
import { getIntegrationName } from "@lib/integrations";
import { VideoCallData } from "@lib/videoClient";
import { CalendarEvent } from "./calendarClient";
import { stripHtml } from "./emails/helpers";
import { VideoCallData } from "@lib/videoClient";
import { getIntegrationName } from "@lib/integrations";
const translator = short();
+3 -1
View File
@@ -1,13 +1,15 @@
import { Prisma, Credential } from "@prisma/client";
import { EventResult } from "@lib/events/EventManager";
import logger from "@lib/logger";
import { VideoCallData } from "@lib/videoClient";
import CalEventParser from "./CalEventParser";
import EventOrganizerMail from "./emails/EventOrganizerMail";
import EventOrganizerRescheduledMail from "./emails/EventOrganizerRescheduledMail";
import { AppleCalendar } from "./integrations/Apple/AppleCalendarAdapter";
import { CalDavCalendar } from "./integrations/CalDav/CalDavCalendarAdapter";
import prisma from "./prisma";
import { VideoCallData } from "@lib/videoClient";
const log = logger.getChildLogger({ prefix: ["[lib] calendarClient"] });
+71
View File
@@ -0,0 +1,71 @@
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";
export const extractLocaleInfo = async (req: IncomingMessage) => {
const session = await getSession({ req: req });
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",
ro: "Romanian",
};
export type OptionType = {
value: string;
label: string;
};
export const localeOptions: OptionType[] = i18n.locales.map((locale) => {
return { value: locale, label: localeLabels[locale] };
});
+20
View File
@@ -0,0 +1,20 @@
import { useTranslation } from "next-i18next";
import { useEffect } from "react";
type LocaleProps = {
localeProp: string;
};
export const useLocale = (props: LocaleProps) => {
const { i18n, t } = useTranslation("common");
useEffect(() => {
(async () => await i18n.changeLanguage(props.localeProp))();
}, [i18n, props.localeProp]);
return {
i18n,
locale: props.localeProp,
t,
};
};