DocsNext.js

Next.js setup

Entry points, next.config wrapper, environment variables and the server GenericTranslationProvider for the App Router.

On this page

@generic-translation/react ships Next.js support in extra entry points. It needs Next 14 or newer (optional peer dependency) and React 18 or newer. For a step-by-step guide see the Next.js quickstart. Packages are not published to npm yet; install by linking the built packages as shown there.

Entry points

Import Use it in Provides
@generic-translation/react client components GenericTranslationProvider, T, hooks, LocaleSelector, localizePath.
@generic-translation/react/server server code without Next async T, Tx, tx, getGenericTranslation, getDictionary, getTranslations, locale helpers, variable components, configureGenericTranslation.
@generic-translation/react/next server components, layouts, route handlers everything in /server, plus the server GenericTranslationProvider, getLocale wired to Next headers and cookies, LocaleSelector, localizePath, and re-exports of withGenericTranslation and createGenericTranslationMiddleware.
@generic-translation/react/next/config next.config.* withGenericTranslation, genericTranslationEnv, readGenericTranslationConfig.
@generic-translation/react/next/middleware middleware.ts createGenericTranslationMiddleware.

Import next.config and middleware helpers from their lean entry points, not from @generic-translation/react/next.

withGenericTranslation(nextConfig, options?)

// next.config.mjs
import { withGenericTranslation } from "@generic-translation/react/next/config";
export default withGenericTranslation({ reactStrictMode: true });

It accepts a config object or a config function. It reads generic-translation.config.json and:

  • exposes NEXT_PUBLIC_GENERIC_TRANSLATION_DEFAULT_LOCALE, NEXT_PUBLIC_GENERIC_TRANSLATION_LOCALES, NEXT_PUBLIC_GENERIC_TRANSLATION_LOCALE_PREFIX, and (when known) NEXT_PUBLIC_GENERIC_TRANSLATION_PROJECT_ID and NEXT_PUBLIC_GENERIC_TRANSLATION_BASE_URL;
  • sets GENERIC_TRANSLATION_DICTIONARY_PATH for the server when the config names an existing dictionary file;
  • throws if your env block would inline GENERIC_TRANSLATION_API_KEY or any gt-api-... value into browser bundles.
Option Type Description
configPath string Config file path. Default generic-translation.config.json in the working directory.
projectId, baseUrl, defaultLocale, locales Override the file.
dictionary string Dictionary path. Default: the config’s dictionary.
localePrefix "always" | "as-needed" | "never" Must match the middleware. Default "as-needed".

Environment variables

Variable Where Meaning
GENERIC_TRANSLATION_PROJECT_ID server Project id. Also read as NEXT_PUBLIC_GENERIC_TRANSLATION_PROJECT_ID.
GENERIC_TRANSLATION_API_KEY server only Needed only for tx() / <Tx>. Never exposed to the browser.
GENERIC_TRANSLATION_API_URL server API origin; also NEXT_PUBLIC_GENERIC_TRANSLATION_BASE_URL.
NEXT_PUBLIC_GENERIC_TRANSLATION_DEFAULT_LOCALE, NEXT_PUBLIC_GENERIC_TRANSLATION_LOCALES both Set by withGenericTranslation. GENERIC_TRANSLATION_DEFAULT_LOCALE and GENERIC_TRANSLATION_LOCALES are also read.
GENERIC_TRANSLATION_DICTIONARY_PATH server Path to the dictionary JSON file.

You can also call configureGenericTranslation({ ... }) once at startup (exported from @generic-translation/react/server and /next). Its fields: projectId, apiKey, baseUrl, defaultLocale, locales, loadTranslations, translationsDir (default public/_generic-translation), dictionary, cacheTtlMs (default 60000, and 0 in development). getGenericTranslationConfig() returns the effective configuration.

Server GenericTranslationProvider

import { GenericTranslationProvider } from "@generic-translation/react/next";

<GenericTranslationProvider locale={locale}>{children}</GenericTranslationProvider>

An async server component. It resolves the locale, loads that locale’s translations and the dictionary on the server, and renders the client provider with localeNavigation set, so switching language navigates to the localized URL.

Prop Description
locale Defaults to getLocale(). Pass the [locale] route param to keep pages static.
translations Defaults to the server-loaded map.
dictionary Defaults to the dictionary file from the config.
localePrefix Defaults to the withGenericTranslation value.
devApiKey, detectLocale, updateHtmlAttributes Forwarded to the client provider.

Where translations are read from

On the server getTranslations(locale) tries, in order: your loadTranslations, the file public/_generic-translation/{locale}.json (relative to the working directory), then the CDN for projectId. Failures yield an empty map, so pages render source text. In production the maps are cached for a minute; in development they are re-read on every request. No translation is loaded for the default locale. On serverless platforms where the filesystem is not available, publish to the CDN or supply loadTranslations.

Something wrong or unclear on this page? Open an issue.

Type to search the documentation.