A small coffee storefront on the Next.js App Router, localised with
@saykit/react and
babel-plugin-saykit.
This is the example to read if you care about React Server Components: it renders messages on the server and in client islands, from a single catalogue serialised across the boundary exactly once.
| Concern | Where |
|---|---|
withSay, establishing the view for a route segment |
layout.tsx, page.tsx |
getSay() inside a plain server component |
src/app/[locale]/product-card.tsx |
SayProvider at the root, taking its props from the scope |
src/app/[locale]/layout.tsx |
<Say> in a client component |
add-to-cart.tsx, locale-switcher.tsx |
generateStaticParams from iterating a Catalogue, whose values are locale-bound Views |
src/app/[locale]/layout.tsx |
Locale detection: path → cookie → Accept-Language |
src/proxy.ts |
| Babel wiring (Next.js runs Babel, not a bundler plugin) | .babelrc |
@saykit/react publishes its . entry twice and lets the bundler pick:
- In a server environment the
react-serverexport condition resolves to a build where<Say>reads fromgetSay(), the viewwithSayput in React's request cache. - In a client environment it resolves to the
"use client"build, where<Say>reads fromuseSay(), the nearestSayProvider.
The component you write is identical in both cases:
<Say>
Roasted by <strong>{product.roaster}</strong>
</Say>Because both halves are fed from the same view, with the client provider reading its locale and messages straight off it, server output and client hydration cannot disagree about which locale is active. Messages compile to data rather than to functions, so they cross the boundary as the JSON they already are.
src/i18n.ts is marked server-only and imports all three catalogues. The middleware and the
locale <select> only need the list of locale codes, so that lives in src/config.ts on its own.
Importing i18n.ts from either would drag the catalogues into the edge bundle and break the client
build.
Next.js compiles with its own toolchain, so the macros are rewritten by babel-plugin-saykit:
{ "presets": ["next/babel"], "plugins": ["saykit"] }The plugin only rewrites macros. import en from './locales/en.js' is an ordinary import of a
module compiled from en.po, which withSayKit in next.config.mjs takes care of: it compiles
every locale before Next builds anything, and under next dev recompiles whenever a catalogue
changes.
import { withSayKit } from 'babel-plugin-saykit/next';
export default withSayKit({});Adding
.babelrcopts the whole app out of Next.js's default SWC pipeline, which is slower to compile. Projects on a Vite-based framework should preferunplugin-saykit; see the TanStack Start example.
pnpm install
pnpm --filter nextjs-example dev
pnpm --filter nextjs-example extract # after editing any messageVisit / for English (served by rewrite), /fr, or /pl. The chosen locale is persisted in the
x-preferred-locale cookie, so a later visit to / redirects accordingly.
Further reading: the Babel integration docs and the React integration docs.