Search documentation

Search all pages, headings and text, in the current package or topic or in all documentation.

Server

Load your dictionaries on the server in Next, translate in Server Components, and translate page metadata.

@workspace/active-i18n 1.1.0Developmentunreleased, in development in Foundationbaseline ce081dc3

The server example below runs on this site's server while the page renders: a React Server Component calls the server integration for five requested languages against a host set of three.

Server translations for the requested languageServer: Calls a real route of this site, which does the work on the server.

RequestedLoadedshop.welcome
nlnlWelkom, Ada
en-GBenWelcome, Ada
pt-BRpt-BRBem-vindo, Ada
ptenWelcome, Ada
frenWelcome, Ada

Bind once, at module scope#

TypeScript
import {createServerTranslations} from '@workspace/active-i18n/server';
export const translations = createServerTranslations(  {    namespaces: ['common', 'shop'],    importNamespace: (locale, namespace) =>      import(`./resources/${locale}/${namespace}.json`),  },  {supportedLocales: ['en', 'nl', 'pt-BR'], fallbackLocale: 'en'},);

The language set must be canonical BCP 47 tags, listed once, with the fallback among them; an invalid set is refused when the instance is created. Give onMissingKey and a logger to report misses and failures.

Translate in a Server Component#

TSX
export default async function Page({locale}: {locale: string}) {  const {t} = await translations.getServerTranslator(locale);  return <h1>{t('shop.welcome', {name: 'Ada'})}</h1>;}

The requested locale is matched exactly first, then with subtags removed from the end (en-GB loads en), never a sibling (pt does not load pt-BR). A miss loads the fallback with a warning.

Hand the tree to the client#

getServerTranslations(locale) returns {translations, locale}; pass both to <I18nProvider> in your layout, so client components translate with the same tree.

Page metadata#

getTranslatedMetadata(titleKey, {preferredLocale, descriptionKey?}) returns Next Metadata with a translated title and description, for generateMetadata.

Where the server entries run#

@workspace/active-i18n/server is marked server-only: it loads in a React Server Components server and throws in plain Node and in a browser bundle. A worker or another server that is not Next uses the root entry instead; see Without Next.

This page as Markdown