Documentation

La fonction de traduction

Parfois, tu dois traduire du contenu qui ne fait pas partie de ton JSX, par exemple les métadonnées d'une page Next.js. La fonction t() renvoyée par createLocalizedT traduit n'importe quelle chaîne.

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')
};
}

La fonction translate de la configuration peut également être appelée directement. Il prend une langue, un texte et des options facultatives, et passe à la traduction.

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