---
title: "Send email in each reader's language"
description: "Pass your app's i18n message files to setupEmail and every email the package sends is written in its reader's language: the account's user.locale, a fallback order for people without an account, localized links, and your own emails translated the same way."
canonical_url: "https://nuxt-backend.dev/docs/build/email/templates/languages"
---
# Send email in each reader's language

> Pass your app's i18n message files to setupEmail and every email the package sends is written in its reader's language: the account's user.locale, a fallback order for people without an account, localized links, and your own emails translated the same way.

Version: nuxt-backend 0.6.x · Tab: Build · Kind: how-to · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

- [API reference](https://nuxt-backend.dev/docs/reference/api/email)
- [Source](https://github.com/qruto/nuxt-backend/blob/main/src/convex/locale.ts)

- **You get:** every email the package sends, and your own, in its reader's language, with links that open your pages in that language.
- **Powered by:** your app's own i18n message files; Resend delivers what is rendered and knows nothing of languages.
- **The package adds:** the reader's language from `user.locale`, a fallback order for people without an account, key-by-key fallback to English, and the same messages for your own emails.

Write every email in the language its reader uses your app in. Pass your i18n message files to `setupEmail`, and each email the package sends picks its reader's language, falling back to English key by key. Your own emails read the same messages. The package's own emails come in English, Russian, German, Spanish and French with no messages of yours ([The package's own translations](https://nuxt-backend.dev/docs/build/ui/customize/translation#the-packages-own-translations)); any other language is English until your messages carry it. Once you pass messages, an email goes only in a language your app serves ([Languages your app serves](#languages-your-app-serves)).

## Translate the emails

Pass your app's i18n message files to `setupEmail` as they are. The package reads its keys under `backend.email`, in the same `backend` object that [translates the packaged UI](https://nuxt-backend.dev/docs/build/ui/customize/translation):

```ts [backend/email.ts]
import de from '../i18n/locales/de.json'
import fr from '../i18n/locales/fr.json'

export const email = setupEmail(components, {
  messages: { de, fr },
})
```

```json [i18n/locales/de.json]
{
  "backend": {
    "email": {
      "welcome": {
        "subject": "Willkommen",
        "greetingNamed": "Willkommen, {name}!",
        "body": "Dein Konto steht bereit, danke fürs Mitmachen."
      },
      "layout": { "sentBy": "Gesendet von {brand}" }
    }
  }
}
```

The keys are the [`email` label group](https://nuxt-backend.dev/docs/reference/configuration/labels). A key a language leaves out reads the package's own translation into that language when it has one, then your `en` messages, then the package's English, so you can translate one email at a time, or reword one email of the package's German and keep the rest. The messages use the labels' syntax: keep each `{param}`, and separate plural forms with `|`.

Key `messages` by your i18n locale codes, and spell `defaultLocale` the same way (`pt-br` for an i18n locale `pt-br`). A recipient whose language your app doesn't serve reads its base language's (`de` for `de-AT`), else `defaultLocale`, which is `'en'` unless you set it:

```ts [backend/email.ts]
setupEmail(components, { messages: { de }, defaultLocale: 'de' })
```

### Words of your own in each language

App config's `backend.labels` is one language's copy, and it wins in every language, in the emails as on your pages: the backend reads it from `labels.generated.ts` ([Labels reach the backend](https://nuxt-backend.dev/docs/build/ui/customize/labels#labels-reach-the-backend)). So an app that writes its notification kinds' words there sends them in that language to everyone. To send them in each reader's language, give them per language in the messages instead, under `backend.notifications`, and leave them out of app config ([One language or many](https://nuxt-backend.dev/docs/build/ui/customize/labels#one-language-or-many)):

```json [i18n/locales/de.json]
{
  "backend": {
    "notifications": {
      "strip_ready": { "title": "Fertig: dein Comicstrip", "body": "Öffne ihn, um ihn zu lesen und zu teilen." }
    }
  }
}
```

The same file words the inbox through `@nuxtjs/i18n`, so the pages and the emails read one copy per language.

### Languages your app serves

An email is written, and its links prefixed, only in a language your pages answer in, so a link never opens a path that 404s. Those are `defaultLocale` and the languages your `messages` carry. In the app above, a reader whose account says `ru-RU` gets German, with a `/de` link under `prefix` and an unprefixed one under `prefix_except_default`, not the package's Russian. A key of your own that a language leaves out reads `defaultLocale`'s messages, then your `en`'s.

To send in one of the package's translations your messages don't carry, name your i18n `locales`:

```ts [backend/email.ts]
setupEmail(components, { messages: { de }, locales: ['en', 'de', 'es'] })
```

An app that passes neither `messages` nor `locales` gets the package's five languages. With a `localePaths` that prefixes links, and no language named, every email stays in `defaultLocale`.

A [template override](https://nuxt-backend.dev/docs/build/email/templates#override-a-template) gets the same words through `t`, and the language through `locale`. A key that is not one of the package's reads from the root of your messages, so an override can carry strings of its own:

```ts [backend/email.ts]
templates: {
  welcome: (data, { byDefault, t }) => ({
    ...byDefault(data),
    subject: t('emails.welcomeSubject', { name: data.name }),
  }),
},
```

## The recipient's language

Every account has a language, `user.locale`, which its emails are written in:

- **At sign-up**, the account takes the language of the request that creates it. `useLoginFlow` and `<AuthForm>` send the app's active `@nuxtjs/i18n` locale as `Accept-Language`; without `@nuxtjs/i18n`, the browser's own.
- **On a language switch**, with `@nuxtjs/i18n`, the new locale is stored on the signed-in account.
- **On the profile page**, `<ProfileSettings>` shows a language picker when the app has two locales or more. A pick stores it on the account, then switches the app.

The account's language is also a field of `useAuth().user`, and the OpenID `locale` claim of its session token: `identity.language` in a Convex function.

### People without an account

An invitation, a gift notification or a sign-in code can go to an address no account has yet. Each email the package sends picks the first language it knows, in this order:

1. The language the caller asked for (`sendTemplate`'s `locale`).
2. The language of the account the address belongs to.
3. The language of the request that asked for the email: a sign-in code to a new address is written in the sign-in page's language.
4. The sender's language: an invitee reads the inviter's, a gift's recipient the purchaser's.
5. `defaultLocale`.

A language that cannot be read, such as a `senderId` that names no account, is skipped: the email still goes, and the deployment logs `email.locale_unread`. The welcome and email-change emails go to an account, and always read its language. Your own sends take the same options:

```ts [backend/invites.ts]
await email.sendTemplate(ctx, 'invite', data, { senderId: inviter._id })
await email.sendTemplate(ctx, 'gift', data, { locale: 'fr' })
```

## Localized links

An invitation and a gift notification link to your app's pages. Under an `@nuxtjs/i18n` strategy that prefixes the path, pass the same strategy as `localePaths`, and the links open in the recipient's language:

```ts [backend/email.ts]
setupEmail(components, {
  messages: { fr },
  defaultLocale: 'en',
  localePaths: 'prefix_except_default', // https://acme.com/fr/accept-invitation
})
```

Only links to `SITE_URL` change. A template override reaches the same rule as `link(url)`.

## Your own emails, translated

`email.compose` builds an email of your own in the package's layout, with the render context of the reader's language. Its `t` reads your messages, and the [blocks](https://nuxt-backend.dev/docs/build/email/templates/theme#your-own-emails-in-the-layout) it returns are themed, escaped and given a text version:

```ts [backend/stories.ts]
import { v } from 'convex/values'
import { internalAction } from './_generated/server'
import { email } from './email'

export const sendStoryReady = internalAction({
  args: { to: v.string(), title: v.string(), locale: v.optional(v.string()) },
  handler: async (ctx, { to, title, locale }) => {
    const message = await email.compose(({ t }) => ({
      to,
      subject: t('stories.readySubject', { title }),
      blocks: [{ heading: title }, { paragraph: t('stories.readyBody', { title }) }],
    }), { locale })
    return await email.send(ctx, message)
  },
})
```

Pass the account's `user.locale` as `locale`. [Email sequences](https://nuxt-backend.dev/docs/build/growth/engage/lifecycle-emails) get the same context for each step, in the recipient's language.

## Check it works

Render one email in a test with `renderEmail` from [`nuxt-backend/test-utils`](https://nuxt-backend.dev/docs/reference/api/test-utils), as your `setupEmail` options would, without a deployment:

```ts [test/emails.test.ts]
import { renderEmail } from 'nuxt-backend/test-utils'
import { expect, it } from 'vitest'
import de from '../i18n/locales/de.json'

it('welcomes in German', async () => {
  const message = await renderEmail('welcome', { email: 'ada@example.com', name: 'Ada' }, { locale: 'de', messages: { de } })
  expect(message.subject).toBe('Willkommen')
})
```

The DevTools **Email** tab shows every email in each language your messages cover ([Previewing](https://nuxt-backend.dev/docs/build/email/templates#previewing)).

## Related

- [Customize email templates](https://nuxt-backend.dev/docs/build/email/templates): the templates and their render context.
- [Brand your emails](https://nuxt-backend.dev/docs/build/email/templates/theme): the layout and the brand each email renders with.
- [Translate the UI](https://nuxt-backend.dev/docs/build/ui/customize/translation): the same `backend` messages for the pages and components.
- [Let users edit their profile](https://nuxt-backend.dev/docs/build/auth/accounts/profile): where a person picks their language.


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
