> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gatojs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Translations and locales

> How GatoJS uses next-intl: locale routing, per-feature translation files, the generated message registry, and how to add strings and locales.

GatoJS translates the UI with [next-intl](https://next-intl.dev). The locale is part of the URL, every feature keeps its own translation files, and the CLI generates the registry that loads them. Polish (`pl`) is the default and required locale; English (`en`) is enabled by default.

## How it fits together

All pages live under the `src/app/[locale]/` segment. For every request, `src/i18n/request.ts` loads the core messages and each feature's messages for the resolved locale and merges them into one object. A **namespace** is a top-level key of that object: a client component asks for one with `useTranslations("users")`, a server component with `await getTranslations("users")`.

| File | Role |
| - | - |
| `src/i18n/config.ts` | Exports `locales`, the `Locale` type and `defaultLocale` (`pl`). |
| `src/i18n/locales.generated.ts` | Generated list of the project's locales, taken from `locales` in `.wa/kit.json`. Do not edit. |
| `src/i18n/routing.ts` | `defineRouting` config plus locale-aware `Link`, `redirect`, `usePathname` and `useRouter` from `createNavigation`. |
| `src/proxy.ts` | The next-intl middleware that resolves the locale for each request. |
| `src/i18n/request.ts` | `getRequestConfig`: loads core and feature messages and merges them with `mergeMessages`. An unknown locale falls back to the default. |
| `src/i18n/merge-messages.ts` | Puts each feature's messages under its own namespace and rejects collisions. |
| `src/app/messages/{pl,en}.json` | Core messages: namespaces `common`, `errors`, `sidebar` and `serviceMode`. |
| `src/features/<name>/i18n/{pl,en}.json` | Messages of one feature. |
| `src/app/[locale]/layout.tsx` | Returns 404 for an unknown locale segment, calls `setRequestLocale` and passes all messages to `NextIntlClientProvider`. |

> [!TIP]
>
> Import `Link`, `redirect`, `usePathname` and `useRouter` from `@/i18n/routing`, not from `next/navigation`. These versions add the locale prefix for you, and `usePathname` returns the path without it. Take only `notFound` and `useSearchParams` from `next/navigation`.

## How the locale is chosen

```ts src/i18n/routing.ts theme={null}
export const routing = defineRouting({
  locales,
  defaultLocale,
  localePrefix: "as-needed",
});
```

With `localePrefix: "as-needed"`, the default locale has no prefix: the Polish sign-in page is `/sign-in`, the English one is `/en/sign-in`. For an unprefixed URL the proxy rewrites internally to `/pl/...`; when the resolved locale is English it answers with a 307 redirect to the `/en/...` URL.

The routing config does not set `localeDetection`, so next-intl's default (`true`) applies. The request locale is picked in this order:

1. The prefix in the URL (`/en/...`, `/pl/...`).
2. The `NEXT_LOCALE` cookie, if it holds a supported locale.
3. The `Accept-Language` header: the first supported language wins, otherwise `pl`.
4. The default locale, `pl`.

In practice this means:

* With a browser set to English, unprefixed URLs redirect to `/en/...`, even right after `npm run dev`.
* Visiting any `/en/...` page sets `NEXT_LOCALE=en`, so later unprefixed URLs redirect to English too.
* Opening any `/pl/...` URL redirects to the unprefixed URL and sets `NEXT_LOCALE=pl`, which brings Polish back.

If you want unprefixed URLs to always render Polish, set `localeDetection: false` in `defineRouting`. next-intl then skips the cookie and the header. See [Known limitations](https://docs.gatojs.com/known-limitations.md).

## Namespaces and `mergeMessages`

The CLI generator writes one loader per feature and locale into `moduleMessages` in `src/modules.generated.ts`, with `key` equal to the feature's directory name. `request.ts` merges them with the core file:

```ts src/i18n/request.ts theme={null}
const core = (await import(`@/app/messages/${locale}.json`)).default;
const modules = await Promise.all(
  moduleMessages[locale].map(async ({ key, load }) => ({
    key,
    messages: await load(),
  })),
);

return {
  locale,
  messages: mergeMessages(core, modules),
};
```

The rules `mergeMessages` enforces:

* **One namespace per feature.** A feature file lands as a whole under its directory name. Its top-level keys are already message keys: `title` in `src/features/users/i18n/pl.json` is `users.title`, not `users.users.title`.
* **Shallow merge.** A feature can't add to `common` or override a core message.
* **Valid names only.** The namespace must match `^[a-z][a-zA-Z0-9-]*$`. `file-storage2` and `myFeature` pass; `my_feature`, `Notes` and `1x` throw `Nieprawidłowy klucz namespace i18n: <name>` ("invalid i18n namespace key").
* **No collisions.** A name already taken by core (`common`, `errors`, `sidebar`, `serviceMode`) or another feature throws `Kolizja namespace i18n: <name>` ("i18n namespace collision").

The merge runs on every page request, so an error here stops every page under `src/app/[locale]/` from rendering. The sidebar looks labels up under `<key>.<labelKey>`, where `key` comes from the feature's `module.ts`, so keep that `key` equal to the directory name. See [Module registry](https://docs.gatojs.com/concepts/module-registry.md).

> [!WARNING]
>
> Every message of the current locale is sent to the browser: `NextIntlClientProvider` is a client component, so its data ships with the page HTML, including on public pages such as `/sign-in`. Never put anything in a translation file that an anonymous visitor must not see.

## Add a string

**1. Add the key to both locale files**

Add the key to `src/features/<name>/i18n/pl.json` and to `en.json` in the same feature. Core keys use kebab-case, for example `toast-invite-mail-failed`. You can group keys into objects and read them with a dotted path, for example ``t(`role.${member.role}`)``.

```json src/features/invoices/i18n/en.json theme={null}
{
  "title": "Invoices",
  "toast-sent": "Invoice {number} sent",
  "status": {
    "draft": "Draft",
    "sent": "Sent"
  }
}
```

**2. Use it in a component**

Pass values through ICU placeholders in curly braces.

```tsx Client component theme={null}
"use client";
import { useTranslations } from "next-intl";

export function SentToast({ number }: { number: string }) {
  const t = useTranslations("invoices");
  return <span>{t("toast-sent", { number })}</span>;
}
```

```tsx Server component theme={null}
import { getTranslations } from "next-intl/server";

export default async function InvoicesPage() {
  const t = await getTranslations("invoices");
  return <h1>{t("title")}</h1>;
}
```

**3. Regenerate only for new files**

Run the generator only when a feature gets a **new** `i18n/<locale>.json` file. The registry imports the file rather than copying its contents, so new keys in an existing file need no regeneration.

```bash theme={null}
npx gato generate
```

Keep feature strings in the feature's namespace, even when they sound generic. Don't add them to `src/app/messages/`: those files belong to the core, and features don't edit the core. If a string would help every project, propose it upstream (see [Working with the kit](https://docs.gatojs.com/guides/working-with-the-kit.md)).

> [!NOTE]
>
> A missing key doesn't break rendering. next-intl logs a `MISSING_MESSAGE` error through `console.error` and shows the key path, for example `invoices.title`. The same happens when a whole namespace is missing, for example when a feature has only `pl.json` and the page is opened in English. TypeScript doesn't check message keys and no test compares `pl.json` with `en.json`, so you notice gaps on the page or in the console.

## Enable or disable a locale

The project's locales live in the `locales` field of `.wa/kit.json`. You choose them when creating the project (`--locales`, see [`gato create`](https://docs.gatojs.com/cli/create)); without the field, the schema assumes `["pl", "en"]`. The CLI accepts only `pl` and `en`, `pl` is required, and duplicates are rejected.

To switch English off or back on:

1. Change `locales` in `.wa/kit.json`, for example to `["pl"]` or back to `["pl", "en"]`.
2. When enabling `en`, make sure `src/app/messages/en.json` and every feature's `i18n/en.json` exist. Kit features ship with both files; add them to your own features.
3. Regenerate and verify:

   ```bash theme={null}
   npx gato generate
   npm run verify
   ```

With `["pl"]`, the generator rewrites `src/i18n/locales.generated.ts` to `["pl"] as const` and drops the `en` loaders from `src/modules.generated.ts`. The `en.json` files stay on disk, unused.

> [!WARNING]
>
> Don't edit `src/i18n/locales.generated.ts` by hand. CI regenerates the files and fails on any diff (see [CI workflows](https://docs.gatojs.com/deployment/ci.md)).

### A third locale

Adding a locale such as `de` needs a change in the CLI itself. With only `.wa/kit.json` changed, `gato generate` exits with code 1 and a Zod error (`Invalid option: expected one of "pl"|"en"`) and writes nothing. The locale list is hardcoded in the CLI's manifest schema, argument parser, registry generator and `kit sync` command, so the change belongs in the kit repository. See [Contributing](https://docs.gatojs.com/contributing/overview.md).

Once the CLI accepts the locale, add it to `locales`, create `src/app/messages/<code>.json` with the `common`, `errors`, `sidebar` and `serviceMode` namespaces, add `i18n/<code>.json` to every feature, then run `gato generate` and `npm run verify`.

## Text outside translation files

Some user-facing text does not go through next-intl. Keep this in mind before you promise a fully English UI:

| Text | Language | Source | What an `en` user sees |
| - | - | - | - |
| Email subjects and bodies | Polish | `src/app/emails/*.tsx`, subjects in `src/shared/lib/mailer.ts` | The same Polish email. Templates don't receive a locale. See [Email](https://docs.gatojs.com/guides/email.md). |
| Command error messages (`result.error`) | Polish | Strings in command code | A Polish message in the toast, shown without translation. |
| Server-side Zod validation messages | English | Zod 4 defaults | Rarely visible, because forms validate the same schema client-side with translated messages first. |
| Environment validation report | Polish and English | `@gatojs/env` | Developers only. See [Environment variables](https://docs.gatojs.com/configuration/environment-variables.md). |
| Log entries | English, with an area prefix such as `[MAILER]` | Strings in code | Not visible to users. |

## Related

> [card] [Email](https://docs.gatojs.com/guides/email.md)
>
> Transports, templates and the mails the app sends.

> [card] [Module registry](https://docs.gatojs.com/concepts/module-registry.md)
>
> How the generator wires feature messages, env schemas and navigation.

> [card] [Build a feature](https://docs.gatojs.com/guides/build-a-feature.md)
>
> Where a feature's `i18n` folder fits in its structure.

> [card] [gato generate](https://docs.gatojs.com/cli/generate.md)
>
> Regenerate the registry and locale files.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.