Skip to content

Instantly share code, notes, and snippets.

@pmioduszewski
Last active January 13, 2024 13:36
Show Gist options
  • Save pmioduszewski/eea40b2564b30e038db08a4b73472fb6 to your computer and use it in GitHub Desktop.
Save pmioduszewski/eea40b2564b30e038db08a4b73472fb6 to your computer and use it in GitHub Desktop.
Send emails with react-email using next-i18next internationalization and Nextjs (pages router) on Vercel
import i18next from "i18next";
import { render } from "@react-email/render";
import {
Head,
Html,
// ...
} from "@react-email/components";
type MyEmailTemplate {
i18nResources: Record<string, unknown>;
locale: string;
// ...
}
export const MyEmailTemplate = ({
i18nResources,
locale,
// ..
}: MyEmailTemplate) => {
i18next.init({
// @ts-expect-error: Bro, I have to eat
resources: i18nResources,
lng: locale,
});
const t = i18next.t;
return (
<Html>
<Head />
{/* ... */}
{t("my_beautiful_key", { ns: "common" })}
{/* ... */}
</Html>
);
};
export const renderMyEmailTemplate = ({
i18nResources,
locale,
// ..
}) =>
render(
<MyEmailTemplate
i18nResources={i18nResources}
locale={locale}
{/* ... */}
/>,
{
pretty: true,
},
);
import type { NextApiRequest, NextApiResponse } from "next";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import { renderMyEmailTemplate } from "../somewhere-over-the-rainbow/my-email-temp";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const locale = "de"
const i18n = {
...(await serverSideTranslations(locale || "en", ["common"], i18nConfig, [
"en",
"de",
])),
};
const i18nResources = i18n._nextI18Next?.initialI18nStore;
const html = renderMyEmailTemplate({
i18nResources,
locale
})
// ... sendEmail
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment