Skip to content

Commit

Permalink
add locale to trpc context (calcom#897)
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT authored Oct 11, 2021
1 parent 7488f29 commit 8cbf880
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
11 changes: 9 additions & 2 deletions lib/core/i18n/i18n.utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Maybe } from "@trpc/server";
import parser from "accept-language-parser";
import { IncomingMessage } from "http";

Expand All @@ -6,9 +7,15 @@ import prisma from "@lib/prisma";

import { i18n } from "../../../next-i18next.config";

export const getOrSetUserLocaleFromHeaders = async (req: IncomingMessage) => {
export function getLocaleFromHeaders(req: IncomingMessage): string {
const preferredLocale = parser.pick(i18n.locales, req.headers["accept-language"]) as Maybe<string>;

return preferredLocale ?? i18n.defaultLocale;
}

export const getOrSetUserLocaleFromHeaders = async (req: IncomingMessage): Promise<string> => {
const session = await getSession({ req });
const preferredLocale = parser.pick(i18n.locales, req.headers["accept-language"]);
const preferredLocale = parser.pick(i18n.locales, req.headers["accept-language"]) as Maybe<string>;

if (session?.user?.id) {
const user = await prisma.user.findUnique({
Expand Down
13 changes: 11 additions & 2 deletions server/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import * as trpc from "@trpc/server";
import { Maybe } from "@trpc/server";
import * as trpcNext from "@trpc/server/adapters/next";
import { NextApiRequest } from "next";

import { getSession, Session } from "@lib/auth";
import { getLocaleFromHeaders } from "@lib/core/i18n/i18n.utils";
import prisma from "@lib/prisma";
import { defaultAvatarSrc } from "@lib/profile";

async function getUserFromSession(session: Maybe<Session>) {
async function getUserFromSession({ session, req }: { session: Maybe<Session>; req: NextApiRequest }) {
if (!session?.user?.id) {
return null;
}
Expand All @@ -30,6 +32,7 @@ async function getUserFromSession(session: Maybe<Session>) {
createdDate: true,
hideBranding: true,
avatar: true,
locale: true,
},
});

Expand All @@ -42,11 +45,14 @@ async function getUserFromSession(session: Maybe<Session>) {
return null;
}
const avatar = user.avatar || defaultAvatarSrc({ email });

const locale = user.locale ?? getLocaleFromHeaders(req);
return {
...user,
avatar,
email,
username,
locale,
};
}

Expand All @@ -58,10 +64,13 @@ export const createContext = async ({ req, res }: trpcNext.CreateNextContextOpti
// for API-response caching see https://trpc.io/docs/caching
const session = await getSession({ req });

const user = await getUserFromSession({ session, req });
const locale = user?.locale ?? getLocaleFromHeaders(req);
return {
prisma,
session,
user: await getUserFromSession(session),
user,
locale,
};
};

Expand Down

0 comments on commit 8cbf880

Please sign in to comment.