Documentation

The translate function

Sometimes you need to translate content that isn't part of your JSX, for example the metadata of a Next.js page. The t() function returned by createLocalizedT translates any string.

import type { Metadata } from 'next';
import { createLocalizedT } from '@magic-translate/react-ssr';
import { translate } from '@/lib/translate';
export async function generateMetadata({
params
}: {
params: Promise<{ locale: string }>;
}): Promise<Metadata> {
const { locale } = await params;
const { t } = createLocalizedT(translate, locale);
return {
title: await t('This page title will be translated'),
description: await t('And so will this description')
};
}

The translate function from the setup can also be called directly. It takes a language, a text and optional options, and resolves to the translation.

import { Language } from '@magic-translate/react-ssr';
const translation = await translate(
Language.ZH,
'This sentence will be translated to Chinese'
);