@lingo.dev/react-next
v1.26.0
Published
Lingo.dev for Next.js: @lingo.dev/react on the App Router (locale proxy, server Lingo, catalog provider, metadata, locale switcher) and the Pages Router.
Keywords
Readme
@lingo.dev/react-next
Lingo.dev for Next.js: wires @lingo.dev/react into the App Router and the Pages Router. Translations live in locales/<locale>.jsonc, written by the lingo CLI, and are read on the server.
App Router
Requires Next.js 16 and React 19. On Next.js 16.0 to 16.2, set experimental: { rootParams: true } in next.config; from 16.3 next/root-params needs no setting. Every locale is served under a URL prefix (/en/pricing, /de/pricing), and the pages live under app/[locale]/.
// i18n.ts
export const locales = createLocaleRouter(["en", "de", "ja"] as const, { defaultLocale: "en", exclude: ["api"] });
// proxy.ts: a path without a prefix redirects to the browser's locale
export const proxy = lingoProxy(locales);
export const config = { matcher: ["/", "/((?!api|_next|.*\\..*).*)"] };// app/[locale]/layout.tsx
export const generateStaticParams = () => lingoStaticParams(locales);
export default async function RootLayout({ children }: { children: ReactNode }) {
const l = await getLingo();
return (
<html lang={l.locale} dir={l.direction}>
<body>
<LingoAppProvider locales={locales}>{children}</LingoAppProvider>
</body>
</html>
);
}
// app/[locale]/page.tsx: a server component
export async function generateMetadata() {
return lingoMetadata({ locales, pathname: "/", origin: "https://example.com" });
}
export default async function Home() {
const l = await getLingo();
return <h1>{l.text("Welcome", { context: "Heading of the start page" })}</h1>;
}createLocaleRouter comes from @lingo.dev/locale-router; useLingo for client components from @lingo.dev/react.
| Entry | Exports |
| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| @lingo.dev/react-next/app/server | getLingo(): the request's Lingo, for server components and generateMetadata. LingoAppProvider: gives client components useLingo() with the whole catalog of the locale. lingoMetadata({ locales, pathname, origin }): canonical, hreflang and Open Graph locale. lingoStaticParams(locales). |
| @lingo.dev/react-next/app/client | useLocaleSwitch(locales): one { locale, label, href, current } per locale for a link-based switcher. |
| @lingo.dev/react-next/app/proxy | lingoProxy(locales): a 301 to the canonical address for /DE or /pt-BR, a 307 to the negotiated locale for a bare path, undefined for everything else so it chains with the app's own proxy. |
The locale comes from the [locale] segment through next/root-params, so no page passes it on. That segment is the locale's tag in lowercase (/pt-br for pt-BR), so the router takes no slugs option. Internal links carry the prefix: locales.prefix("/pricing", locale).
In production the source locale ships no catalog to the browser; its text is the fallback. The catalogs are read with fs from the folder next runs from. Prerendered pages work anywhere; a page rendered on demand needs a Node.js runtime, not Cloudflare Workers or the Edge runtime.
Pages Router
Works on Next.js 13 and later. The next.config i18n locales match the .lingo/config.json locales.
// pages/_app.tsx
export default withLingoApp(function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />;
});
// pages/about.tsx
export const getStaticProps = withLingoProps({ route: "pages/about" });| Entry | Exports |
| ------------------------------------ | ----------------------------------------------------------------------------------------------------- |
| @lingo.dev/react-next/pages/client | withLingoApp, LingoHead (hreflang links), useLocaleSwitch. |
| @lingo.dev/react-next/pages/server | withLingoProps for getStaticProps or getServerSideProps, withLingoPaths for getStaticPaths. |
A page gets the strings of its own file and of every file outside pages/. A page without withLingoProps renders the source language.
With an agent
lingo agent-guide react-next-app and lingo agent-guide react-pages-router print the full setup for each router, from the installed package.
