Skip to content

Commit

Permalink
Translate emails based on users language
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoenig committed Apr 27, 2022
1 parent ffb72c7 commit 7537653
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 30 deletions.
24 changes: 23 additions & 1 deletion translations/de/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -436,5 +436,27 @@
"post.new.success.button": "Weiteren Beitrag erstellen",
"post.category.picker": "Kategorie auswählen",
"post.new.hint": "Hinweis: Sie müssen sich vor dem Absenden Ihres Beitrags mit Ihrem Benutzerdaten anmelden, wenn Sie den Beitrag zu einem späteren Zeitpunkt bearbeiten möchten.",
"post.update": "Aktivität aktualisieren"
"post.update": "Aktivität aktualisieren",

"email.password.reset": "Passwort zurücksetzen",
"email.password.reset.message": "Bitte klicken Sie auf den Link unten, um ein neues Passwort festzulegen.",
"email.password.reset.button": "Passwort zurücksetzen",
"email.approved": "Sie wurden verifiziert",
"email.approved.message": "Sie können jetzt Unterkünfte auf {{link}} anbieten",
"email.approved.button": "Neue Unterkunft hinzufügen",
"email.stayrequest": "Neue Aufenthaltsanfrage",
"email.stayrequest.message": "Sie haben eine neue Aufenthaltsanfrage für einen Ihrer Unterkünfte",
"email.stayrequest.button": "Anfrage anzeigen",
"email.stayrequest.accepted.guest": "Aufenthaltsanfrage angenommen",
"email.stayrequest.accepted.guest.message": "Ihre Aufenthaltsanfrage wurde angenommen.",
"email.stayrequest.accepted.guest.message.name": "Bitte kontaktieren Sie <b>{{name}}</b>",
"email.stayrequest.accepted.host": "Aufenthaltsanfrage angenommen",
"email.stayrequest.accepted.host.message": "Sie haben eine Aufenthaltsanfrage angenommen.",
"email.stayrequest.accepted.host.message.name": "Bitte kontaktieren Sie <b>{{name}}</b>",
"email.stayrequest.declined": "Aufenthaltsanfrage abgelehnt",
"email.stayrequest.declined.message": "Es tut uns leid, aber Ihre Aufenthaltsanfrage wurde abgelehnt. Dies kann aufgrund der Menge an Anfragen passieren, die ein Gastgeber im Moment erhält.",
"email.stayrequest.declined.message.host": "Nachricht vom Gastgeber",
"email.stayrequest.declined.button": "Nach anderen Orten suchen",
"email.stayrequest.canceled": "Aufenthaltsanfrage storniert",
"email.stayrequest.canceled.message": "Der Gast hat eine Aufenthaltsanfrage für Ihre Unterkunft storniert."
}
24 changes: 23 additions & 1 deletion translations/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -446,5 +446,27 @@
"post.new.success.button": "Create another post",
"post.address.countrywide": "Show post for all of Germany",
"post.new.hint": "Note: You need to log into your account before submitting your post, if you would like to edit it at a later time.",
"post.update": "Update Post"
"post.update": "Update Post",

"email.password.reset": "Password Reset",
"email.password.reset.message": "Please click the link below to set a new password.",
"email.password.reset.button": "Reset Password",
"email.approved": "You have been approved",
"email.approved.message": "You can now offer places on {{link}}",
"email.approved.button": "Add new place",
"email.stayrequest": "New Stay Request",
"email.stayrequest.message": "You have a new stay request for one of your places",
"email.stayrequest.button": "Show Request",
"email.stayrequest.accepted.guest": "Stay Request Accepted",
"email.stayrequest.accepted.guest.message": "Your stay request has been accepted.",
"email.stayrequest.accepted.guest.message.name": "Please get in touch with <b>{{name}}</b>",
"email.stayrequest.accepted.host": "Stay Request Accepted",
"email.stayrequest.accepted.host.message": "You accepted a stay request.",
"email.stayrequest.accepted.host.message.name": "Please get in touch with <b>{{name}}</b>",
"email.stayrequest.declined": "Stay Request Declined",
"email.stayrequest.declined.message": "We are sorry but your stay request has been declined. This can happend due to the amount of requests a host receives at the moment.",
"email.stayrequest.declined.message.host": "Message from the host",
"email.stayrequest.declined.button": "Look for other places",
"email.stayrequest.canceled": "Stay Request Canceled",
"email.stayrequest.canceled.message": "The guest canceled a stay request for your place."
}
24 changes: 23 additions & 1 deletion translations/pl/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -411,5 +411,27 @@
"post.new.success.button": "Utwórz kolejny post",
"post.category.picker": "Wybierz kategorię",
"post.new.hint": "Uwaga: Musisz zalogować się na swoje konto przed przesłaniem swojego posta, jeśli chcesz go później edytować.",
"post.update": "Aktualizuj post"
"post.update": "Aktualizuj post",

"email.password.reset": "Zresetuj hasło",
"email.password.reset.message": "Kliknij poniższy link, aby ustawić nowe hasło.",
"email.password.reset.button": "Zresetuj hasło",
"email.approved": "Zostałeś zweryfikowany",
"email.approved.message": "Możesz teraz oferować zakwaterowanie na {{link}}",
"email.approved.button": "Dodaj nową właściwość",
"email.stayrequest": "Nowa prośba o pobyt",
"email.stayrequest.message": "Masz nowy wniosek o pobyt dla jednej ze swoich nieruchomości",
"email.stayrequest.button": "Pokaż prośbę",
"email.stayrequest.accepted.guest": "Prośba o pobyt zaakceptowana",
"email.stayrequest.accepted.guest.message": "Twoja prośba o pobyt została zaakceptowana.",
"email.stayrequest.accepted.guest.message.name": "Proszę o kontakt z <b>{{name}}</b>",
"email.stayrequest.accepted.host": "Prośba o pozostanie zaakceptowana",
"email.stayrequest.accepted.host.message": "Zaakceptowałeś prośbę o pobyt.",
"email.stayrequest.accepted.host.message.name": "Proszę o kontakt z <b>{{name}}</b>",
"email.stayrequest.declined": "Prośba o pobyt odrzucona",
"email.stayrequest.declined.message": "Przepraszamy, ale twoja prośba o pobyt została odrzucona. Może się to zdarzyć ze względu na ilość próśb otrzymywanych w tej chwili przez gospodarza.",
"email.stayrequest.declined.message.host": "Wiadomość od hosta",
"email.stayrequest.declined.button": "Wyszukaj inne lokalizacje",
"email.stayrequest.canceled": "Prośba o pobyt anulowana",
"email.stayrequest.canceled.message": "Gość anulował prośbę o pobyt dla Twojej oferty."
}
24 changes: 23 additions & 1 deletion translations/ru/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -437,5 +437,27 @@
"post.new.success.button": "Создать другой пост",
"post.category.picker": "Выберите категорию",
"post.new.hint": "Примечание. Вам необходимо войти в свою учетную запись перед отправкой сообщения, если вы хотите отредактировать его позже.",
"post.update": "Обновить сообщение"
"post.update": "Обновить сообщение",

"email.password.reset": "Сбросить пароль",
"email.password.reset.message": "Пожалуйста, нажмите на ссылку ниже, чтобы установить новый пароль.",
"email.password.reset.button": "Сбросить пароль",
"email.approved": "Вы прошли проверку",
"email.approved.message": "Теперь вы можете предлагать размещение на {{link}}",
"email.approved.button": "Добавить новое свойство",
"email.stayrequest": "Новый запрос на пребывание",
"email.stayrequest.message": "У вас есть новый запрос на пребывание для одного из ваших объектов",
"email.stayrequest.button": "Показать запрос",
"email.stayrequest.accepted.guest": "Запрос на пребывание принят",
"email.stayrequest.accepted.guest.message": "Ваш запрос на пребывание принят.",
"email.stayrequest.accepted.guest.message.name": "Пожалуйста, свяжитесь с <b>{{name}}</b>",
"email.stayrequest.accepted.host": "Запрос на пребывание принят",
"email.stayrequest.accepted.host.message": "Вы приняли запрос на пребывание.",
"email.stayrequest.accepted.host.message.name": "Пожалуйста, свяжитесь с <b>{{name}}</b>",
"email.stayrequest.declined": "Запрос на пребывание отклонен",
"email.stayrequest.declined.message": "К сожалению, ваш запрос на пребывание был отклонен. Это может произойти из-за большого количества запросов, которые хост получает в данный момент.",
"email.stayrequest.declined.message.host": "Сообщение от хоста",
"email.stayrequest.declined.button": "Искать другие местоположения",
"email.stayrequest.canceled": "Запрос на пребывание отменен",
"email.stayrequest.canceled.message": "Гость отменил запрос на пребывание для вашего объявления."
}
24 changes: 23 additions & 1 deletion translations/ua/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -437,5 +437,27 @@
"post.new.success.button": "Створіть ще одну публікацію",
"post.category.picker": "Виберіть категорію",
"post.new.hint": "Примітка: вам потрібно увійти у свій обліковий запис, перш ніж надсилати свою публікацію, якщо ви хочете відредагувати її пізніше.",
"post.update": "Оновити допис"
"post.update": "Оновити допис",

"email.password.reset": "Скинути пароль",
"email.password.reset.message": "Щоб встановити новий пароль, натисніть посилання нижче.",
"email.password.reset.button": "Скинути пароль",
"email.approved": "Вас підтверджено",
"email.approved.message": "Тепер ви можете запропонувати проживання на {{link}}",
"email.approved.button": "Додати нову властивість",
"email.stayrequest": "Новий запит на проживання",
"email.stayrequest.message": "У вас є новий запит на проживання для одного з ваших об'єктів",
"email.stayrequest.button": "Показати запит",
"email.stayrequest.accepted.guest": "Запит на перебування прийнято",
"email.stayrequest.accepted.guest.message": "Ваш запит на проживання прийнято.",
"email.stayrequest.accepted.guest.message.name": "Зв'яжіться з <b>{{name}}</b>",
"email.stayrequest.accepted.host": "Запит на перебування прийнято",
"email.stayrequest.accepted.host.message": "Ви прийняли запит на перебування.",
"email.stayrequest.accepted.host.message.name": "Зв'яжіться з <b>{{name}}</b>",
"email.stayrequest.declined": "Запит на перебування відхилено",
"email.stayrequest.declined.message": "Вибачте, але ваш запит на перебування було відхилено. Це може статися через велику кількість запитів, які отримує хост на даний момент.",
"email.stayrequest.declined.message.host": "Повідомлення від хоста",
"email.stayrequest.declined.button": "Шукати інші місця",
"email.stayrequest.canceled": "Запит на перебування скасовано",
"email.stayrequest.canceled.message": "Гість скасував запит на проживання для вашого запису."
}
134 changes: 109 additions & 25 deletions utils/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Place, Request, User } from '@prisma/client'
import { MessageHeaders, SMTPClient } from 'emailjs'
import fs from 'fs'
import path from 'path'
import i18n from 'utils/i18next'

const emailPath = path.resolve('utils/email-template.html')

Expand Down Expand Up @@ -44,11 +45,37 @@ const footer = (): string => {
<br>${process.env.CONTACT_ADDRESS_CITY}, ${process.env.CONTACT_ADDRESS_COUNTRY}`
}

const translation = (user: User, key: string): string => {
let language = 'en'
if (user.languages.includes('pl')) {
language = 'pl'
}
if (user.languages.includes('ru')) {
language = 'ru'
}
if (user.languages.includes('ua')) {
language = 'ua'
}
if (user.languages.includes('en')) {
language = 'en'
}
if (user.languages.includes('de')) {
language = 'de'
}
return i18n.t(key, { ns: 'common', lng: language })
}

export const emailPasswordReset = (user: User, hash: string): MessageHeaders => {
const subject = 'HostRefugees - Password Reset'
const subject = `HostRefugees - ${translation(user, 'email.password.reset')}`
const textHtml = [
titleAndParagraph('Password Reset', 'Please click the link below to set a new password.'),
button('Reset Password', `https://hostrefugees.eu/reset-password?hash=${hash}`),
titleAndParagraph(
translation(user, 'email.password.reset'),
translation(user, 'email.password.message')
),
button(
translation(user, 'email.password.button'),
`https://hostrefugees.eu/reset-password?hash=${hash}`
),
].join('')

let content = fs.readFileSync(emailPath, 'utf-8')
Expand All @@ -65,13 +92,19 @@ export const emailPasswordReset = (user: User, hash: string): MessageHeaders =>
}

export const emailApprovedUser = (user: User): MessageHeaders => {
const subject = 'You have been approved'
const subject = translation(user, 'email.approved')
const textHtml = [
titleAndParagraph(
'You have been approved',
`You can now offer places on ${link('HostRefugees.eu', 'https://hostrefugees.eu')}`
translation(user, 'email.approved'),
translation(user, 'email.approved.message').replace(
'{{link}}',
link('HostRefugees.eu', 'https://hostrefugees.eu')
)
),
button(
translation(user, 'email.approved.button'),
'https://hostrefugees.eu/dashboard/place/new'
),
button('Add new place', 'https://hostrefugees.eu/dashboard/place/new'),
].join('')

let content = fs.readFileSync(emailPath, 'utf-8')
Expand All @@ -90,10 +123,18 @@ export const emailApprovedUser = (user: User): MessageHeaders => {
export const emailNewRequest = (
request: Request & { place: Place & { author: User }; author: User }
): MessageHeaders => {
const subject = `New Stay Request - ${request.place.title}`
const subject = `${translation(request.place.author, 'email.stayrequest')} - ${
request.place.title
}`
const textHtml = [
titleAndParagraph('New Stay Request', 'You have a new stay request for one of your places'),
button('Show Request', 'https://hostrefugees.eu/dashboard'),
titleAndParagraph(
translation(request.place.author, 'email.stayrequest'),
translation(request.place.author, 'email.stayrequest.message')
),
button(
translation(request.place.author, 'email.stayrequest.button'),
'https://hostrefugees.eu/dashboard'
),
].join('')

let content = fs.readFileSync(emailPath, 'utf-8')
Expand All @@ -112,14 +153,28 @@ export const emailNewRequest = (
export const emailAcceptRequestGuest = (
request: Request & { place: Place & { author: User }; author: User }
): MessageHeaders => {
const subject = `Stay Request Accepted - ${request.place.title}`
const subject = `${translation(request.author, 'email.stayrequest.accepted.guest')} - ${
request.place.title
}`
const textHtml = [
titleAndParagraph('Stay Request Accepted', 'Your stay request has been accepted.'),
titleAndParagraph(
translation(request.author, 'email.stayrequest.accepted.guest'),
translation(request.author, 'email.stayrequest.accepted.guest.message')
),
paragraph(
`Please get in touch with <b>${request.place.author.firstname} ${request.place.author.lastname}</b>`
translation(request.author, 'email.stayrequest.accepted.guest.message.name').replace(
'{{name}}',
`${request.place.author.firstname} ${request.place.author.lastname}`
)
),
paragraph(
`Email: ${request.place.author.email}<br>Phone: ${request.place.phoneNumber}<br><br>Arrival Instructions:<br>${request.place.arrivalInstructions}`
`${translation(request.author, 'email')}: ${request.place.author.email}<br>${translation(
request.author,
'phone'
)}: ${request.place.phoneNumber}<br><br>${translation(
request.author,
'dashboard.place.arrival'
)}:<br>${request.place.arrivalInstructions}`
),
].join('')

Expand All @@ -139,13 +194,26 @@ export const emailAcceptRequestGuest = (
export const emailAcceptRequestHost = (
request: Request & { place: Place & { author: User }; author: User }
): MessageHeaders => {
const subject = `Stay Request Accepted - ${request.place.title}`
const subject = `${translation(request.place.author, 'email.stayrequest.accepted.host')} - ${
request.place.title
}`
const textHtml = [
titleAndParagraph('Stay Request Accepted', 'You accepted a stay request.'),
titleAndParagraph(
translation(request.place.author, 'email.stayrequest.accepted.host'),
translation(request.place.author, 'email.stayrequest.accepted.host.message')
),
paragraph(
`Please get in touch with <b>${request.author.firstname} ${request.author.lastname}</b>`
translation(request.place.author, 'email.stayrequest.accepted.host').replace(
'{{name}}',
`${request.author.firstname} ${request.author.lastname}`
)
),
paragraph(
`${translation(request.place.author, 'email')}: ${request.author.email}<br>${translation(
request.place.author,
'phone'
)}: ${request.phoneNumber}`
),
paragraph(`Email: ${request.author.email}<br>Phone: ${request.phoneNumber}`),
].join('')

let content = fs.readFileSync(emailPath, 'utf-8')
Expand All @@ -164,14 +232,25 @@ export const emailAcceptRequestHost = (
export const emailDeclineRequest = (
request: Request & { place: Place & { author: User }; author: User }
): MessageHeaders => {
const subject = `Stay Request Declined - ${request.place.title}`
const subject = `${translation(request.author, 'email.stayrequest.declined')} - ${
request.place.title
}`
const textHtml = [
titleAndParagraph(
'Stay Request Declined',
'We are sorry but your stay request has been declined. This can happend due to the amount of requests a host receives at the moment.'
translation(request.author, 'email.stayrequest.declined'),
translation(request.author, 'email.stayrequest.declined.message')
),
request.message
? paragraph(
`${translation(request.author, 'email.stayrequest.declined.message.host')}:<br>${
request.message
}`
)
: '',
button(
translation(request.author, 'email.stayrequest.declined.button'),
'https://hostrefugees.eu/place'
),
request.message ? paragraph(`Message from the host:<br>${request.message}`) : '',
button('Look for other places', 'https://hostrefugees.eu/place'),
].join('')

let content = fs.readFileSync(emailPath, 'utf-8')
Expand All @@ -190,9 +269,14 @@ export const emailDeclineRequest = (
export const emailCancelRequest = (
request: Request & { place: Place & { author: User }; author: User }
): MessageHeaders => {
const subject = `Stay Request Canceled - ${request.place.title}`
const subject = `${translation(request.place.author, 'email.stayrequest.canceled')} - ${
request.place.title
}`
const textHtml = [
titleAndParagraph('Stay Request Canceled', 'The guest canceled a stay request for your place.'),
titleAndParagraph(
translation(request.place.author, 'email.stayrequest.canceled'),
translation(request.place.author, 'email.stayrequest.canceled.message')
),
].join('')

let content = fs.readFileSync(emailPath, 'utf-8')
Expand Down

0 comments on commit 7537653

Please sign in to comment.