Przejdź do głównej zawartości

Backend and emails

View .md

Ta treść nie jest jeszcze dostępna w Twoim języku.

A server handles many users at once, so there is no global language. Pass the recipient’s language explicitly:

const mail = createUseLang(user.locale, translations)((all) => all.mails.welcome)
await sendMail({
to: user.email,
subject: mail.subject,
text: mail.greeting({ name: user.name }),
})

For translated Zod errors on the server, let current read the language of the current request, for example from AsyncLocalStorage set in a tRPC or Hono middleware:

import { AsyncLocalStorage } from 'node:async_hooks'
const requestLocale = new AsyncLocalStorage<'en' | 'de'>()
createLang({
base: 'en',
languages: ['en', 'de'],
current: () => requestLocale.getStore() ?? 'en',
})

The schema module stays the same in app and backend, see Zod.