文档

useT hook

有时你需要翻译除纯 HTML 内容以外的其他内容。为此,Magic Translate 提供了 useT hook。例如,你可以用它来翻译 props。

import { useT } from '@magic-translate/react';
const MyComponent = () => {
const t = useT();
return (
<button aria-label={t('Main menu')}>
<HamburgerIcon />
</button>
);
};

你可以在组件内的任何地方使用 t() 函数。如果你愿意,你也可以在标记中使用它而不是 <T> 组件。

const MyComponent = () => {
const t = useT();
return <p>{t('This will be translated')}</p>;
};

选项

您可以向 t() 传递与 <T> 组件相同的选项,例如 formality。

const MyComponent = () => {
const t = useT();
return <button>{t('Please click here', { formality: 'formal' })}</button>;
};