DocsNext.js

Server components

Use T, getGenericTranslation, getDictionary, tx and Tx in Next.js server components, route handlers and server actions.

On this page

Server components cannot use hooks or the client context. Import from @generic-translation/react/next instead (or @generic-translation/react/server outside Next.js). The translation functions are the same shape as on the client.

<T> and the variable components

import { T, Var, Num, Currency, DateTime, Plural, Branch } from "@generic-translation/react/next";

export default async function Page() {
  return (
    <p>
      <T>Welcome back, <Var name="name">{user.name}</Var>.</T>
    </p>
  );
}

On the server T is an async component with props children, context, id and locale (to override the request locale for one element). Inside it, use Var, Num, Currency, DateTime, Plural, Branch from the same entry point, not from the client one. They have the same props as in Components.

In client components, import everything from @generic-translation/react. A client component rendered under the server GenericTranslationProvider receives the right locale and translations automatically.

getGenericTranslation

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

const t = await getGenericTranslation();          // current request locale
const es = await getGenericTranslation("es");     // a specific locale
t("Hello {name}", { name, $context: "greeting" });

Use it for metadata, attributes and route handlers:

export async function generateMetadata() {
  const t = await getGenericTranslation();
  return { title: t("Pricing") };
}

getDictionary

const d = await getDictionary();
d("nav.home");

See Dictionaries.

Locale helpers

Function Description
setRequestLocale(locale) Pin the locale for the current request. Call at the top of a layout or page.
getLocale() Async; pinned value, then x-generic-translation-locale header, then generic-translation-locale cookie, then default.
getRequestLocale() Sync view of the pinned locale, else the default. Used by the variable components.
getTranslations(locale?) The translation map. Never rejects.
loadDictionary() The source dictionary.
registerLocaleResolver(fn) Plug in your own way to find the locale (done for you by /next).
getGenericTranslationConfig(), configureGenericTranslation(cfg) Effective and explicit configuration.

Runtime translation

For content that does not exist at build time (user-generated text, CMS content), translate during the request through the API. This needs GENERIC_TRANSLATION_API_KEY, on the server only, with the translations:generate permission.

import { tx, Tx } from "@generic-translation/react/next";

const summary = await tx(post.body, { dataFormat: "STRING", context: "blog post" });

<Tx>Free shipping on orders over <Currency currency="USD">{50}</Currency></Tx>

tx(content, options) options: locale, context, dataFormat ("ICU" by default; use "STRING" for opaque text that must not be parsed), values. It first checks the published translations for ICU content, then asks the API; results are cached in memory. On any failure it resolves to the source text. <Tx> behaves like <T> but translates missing messages on demand while rendering. If GENERIC_TRANSLATION_API_KEY is missing, both show source content and warn in development.

The same strings are not shared with @generic-translation/node’s tx() unless the data format and context match, because the hash covers them (see Hashing).

Static rendering

Reading getLocale() without a pinned locale reads headers and makes the route dynamic. For static pages, use an app/[locale] segment, generateStaticParams, and setRequestLocale(locale) in each layout and page. The translation maps are then read at build time from public/_generic-translation or the CDN.

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

Type to search the documentation.