From 5e20c8cc4fd1fab1ddd7d4d60831cade7ba38788 Mon Sep 17 00:00:00 2001 From: Carter Mintey Date: Fri, 20 Dec 2024 22:41:27 -0600 Subject: [PATCH] translate email templates --- src/i18n/en.json | 16 +++++++++- src/lib/server/email.ts | 55 ++++++++++++++++++++++++++--------- templates/invite.html | 6 ++-- templates/invite.mjml | 35 +++++++--------------- templates/password-reset.html | 4 +-- templates/password-reset.mjml | 30 +++++-------------- 6 files changed, 80 insertions(+), 66 deletions(-) diff --git a/src/i18n/en.json b/src/i18n/en.json index 618bfab..f5364a1 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -16,7 +16,8 @@ "toggle-manager": "{isManager, select, true {remove} other {add}} {user} as manager", "toggle-password-visibility": "toggle password visibility", "two-people-looking-in-an-empty-box": "Two people looking in an empty box", - "upload-profile-image": "upload profile image" + "upload-profile-image": "upload profile image", + "wishlist-logo": "Wishlist Logo" }, "admin": { "about": "About", @@ -111,6 +112,19 @@ "username": "Username", "your-password-was-reset": "Your password was reset." }, + "email": { + "invite-body": "You've been invited to join Wishlist! To accept the invitation, click on the link below.", + "invite-button": "Join Wishlist", + "invite-fallback-text": "Somebody has invited you to join their Wishlist! Follow the link to signup: {url}", + "invite-footer": "If you do not wish to join or believe this email to be sent in error, then you can ignore this email.", + "invite-subject": "Wishlist Invite", + "invite-title": "Welcome to Wishlist!", + "pw-reset-body": "If you requested a password reset, click the link below to complete the process. If you didn't make this request, then you can safely ignore this email.", + "pw-reset-fallback-text": "Follow the link to reset your password {resetUrl}", + "pw-reset-title": "Reset Password", + "test-body": "If you're reading this, then congratulations! Email configuration seems to have been set up properly!", + "test-subject": "Test from Wishlist" + }, "errors": { "asset-not-found": "Asset not found", "cannot-delete-default-group": "Cannot Delete Default Group", diff --git a/src/lib/server/email.ts b/src/lib/server/email.ts index 5e62d19..7af08a2 100644 --- a/src/lib/server/email.ts +++ b/src/lib/server/email.ts @@ -4,14 +4,23 @@ import { readFile } from "fs"; import type Mail from "nodemailer/lib/mailer"; import { getConfig } from "$lib/server/config"; import { env } from "$env/dynamic/private"; +import { getFormatter } from "$lib/i18n"; -type TemplateData = { +type InviteTemplateData = { url: string; baseUrl: string; + wishlistLogoText: string; + previewText: string; + titleText: string; + bodyText: string; + buttonText: string; + footerText: string; }; -let passResetTempl: HandlebarsTemplateDelegate; -let inviteTempl: HandlebarsTemplateDelegate; +type PasswordResetTemplateData = Omit; + +let passResetTempl: HandlebarsTemplateDelegate; +let inviteTempl: HandlebarsTemplateDelegate; readFile("templates/password-reset.html", "utf-8", (err, data) => { if (err) { @@ -79,30 +88,50 @@ const sendEmail = async (options: Mail.Options) => { }); }; -export const sendSignupLink = async (to: string, url: string) => { - const html = inviteTempl({ url, baseUrl: env.ORIGIN || "http://localhost:5173" }); +export const sendSignupLink = async (to: string, inviteUrl: string) => { + const $t = await getFormatter(); + const html = inviteTempl({ + url: inviteUrl, + baseUrl: env.ORIGIN || "http://localhost:5173", + wishlistLogoText: $t("a11y.wishlist-logo"), + previewText: $t("email.invite-title"), + titleText: $t("email.invite-title"), + bodyText: $t("email.invite-body"), + buttonText: $t("email.invite-button"), + footerText: $t("email.invite-footer") + }); return await sendEmail({ to, - subject: "Wishlist Invite", + subject: $t("email.invite-subject"), html, - text: `Somebody has invited you to join their Wishlist! Follow the link to signup: ${url}` + text: $t("email.invite-fallback-text", { values: { url: inviteUrl } }) }); }; -export const sendPasswordReset = async (to: string, url: string) => { - const html = passResetTempl({ url, baseUrl: env.ORIGIN || "http://localhost:5173" }); +export const sendPasswordReset = async (to: string, resetUrl: string) => { + const $t = await getFormatter(); + const html = passResetTempl({ + url: resetUrl, + baseUrl: env.ORIGIN || "http://localhost:5173", + wishlistLogoText: $t("a11y.wishlist-logo"), + previewText: $t("email.pw-reset-title"), + titleText: $t("email.pw-reset-title"), + bodyText: $t("email.pw-reset-body"), + buttonText: $t("email.pw-reset-title") + }); return await sendEmail({ to, - subject: "Password Reset", + subject: "Wishlist | " + $t("email.pw-reset-title"), html, - text: `Follow the link to reset your password ${url}` + text: $t("email.pw-reset-fallback-text", { values: { url: resetUrl } }) }); }; export const sendTest = async (to: string) => { + const $t = await getFormatter(); return await sendEmail({ to, - subject: "Test from Wishlist", - text: "If you're reading this, then congratulations! Email configuration seems to have been set up properly!" + subject: $t("email.test-subject"), + text: $t("email.test-body") }); }; diff --git a/templates/invite.html b/templates/invite.html index ff1cb27..1342689 100644 --- a/templates/invite.html +++ b/templates/invite.html @@ -20,6 +20,6 @@ }
Welcome to Wishlist!
Wishlist Logo
Wishlist

Welcome to Wishlist!
You've been invited to join Wishlist! To accept the invitation, click on the link below.
Join Wishlist

If you do not wish to join or believe this email to be sent in error, then you can ignore this email.
\ No newline at end of file +}
{{previewText}}
{{wishlistLogoText}}
Wishlist

{{titleText}}
{{bodyText}}
{{buttonText}}

{{footerText}}
\ No newline at end of file diff --git a/templates/invite.mjml b/templates/invite.mjml index fbc3bed..17c024c 100644 --- a/templates/invite.mjml +++ b/templates/invite.mjml @@ -1,46 +1,31 @@ - + - - + + - - Welcome to Wishlist! - + {{previewText}} - - + Wishlist - - - - - Welcome to Wishlist! - - - You've been invited to join Wishlist! To accept the invitation, click on the link below. - - - - - Join Wishlist + {{titleText}} + {{bodyText}} + {{buttonText}} - - - If you do not wish to join or believe this email to be sent in error, then you can ignore this email. - + + {{footerText}} diff --git a/templates/password-reset.html b/templates/password-reset.html index dec0503..05f1d2b 100644 --- a/templates/password-reset.html +++ b/templates/password-reset.html @@ -20,5 +20,5 @@ }
Wishlist -- Password Reset Request
Wishlist Logo
Wishlist

Reset Password
If you requested a password reset, click the link below to complete the process. If you didn't make this request, then you can safely ignore this email.
Reset Password
\ No newline at end of file +}
Wishlist | {{previewText}}
{{wishlistLogoText}}
Wishlist

{{titleText}}
{{bodyText}}
{{buttonText}}
\ No newline at end of file diff --git a/templates/password-reset.mjml b/templates/password-reset.mjml index 66a4557..e66cc56 100644 --- a/templates/password-reset.mjml +++ b/templates/password-reset.mjml @@ -1,39 +1,25 @@ - + - - + + - - Wishlist -- Password Reset Request - + Wishlist | {{previewText}} - - + Wishlist - - - - - Reset Password - - - If you requested a password reset, click the link below to complete the process. If you - didn't make this request, then you can safely ignore this email. - - - - - Reset Password + {{titleText}} + {{bodyText}} + {{buttonText}}