@magic-translate/react

中文文档
1.18.0发布于 2026/11/02MITTypeScriptESM + CJS

AI-powered i18n for React. Wrap text in and it is translated automatically, with no translation keys or JSON language files.

原始使用文档
文档目录11 节
  1. Install
  2. Setup
  3. Usage
  4. <T> component
  5. useTranslate
  6. Variables
  7. Options per translation
  8. Switching languages
  9. Configuration
  10. Other frameworks
  11. License

@magic-translate/react

NPM npm

React client for Magic Translate, AI-powered i18n without translation keys or language files. Write your components in one language, wrap the text in <T>, and Magic Translate translates it on the fly.

<button>
  <T>Create chord chart</T>
</button>

No t('editor.toolbar.create'), no en.json / de.json to keep in sync, and adding a language is one line. Translations are cached and can be edited by hand in the dashboard.

Requires React 17 or newer. For server-side rendering with Next.js or React Server Components, use @magic-translate/react-ssr.

Full docs: magictranslate.io/docs/react

Install

npm install --save @magic-translate/react

Or with yarn:

yarn add @magic-translate/react

Setup

Wrap your app in MagicTranslateProvider. You can get an API key for free at magictranslate.io.

// main.tsx
import { createRoot } from 'react-dom/client';
import { MagicTranslateProvider } from '@magic-translate/react';
import App from './App';

createRoot(document.getElementById('root')!).render(
  <MagicTranslateProvider apiKey="YOUR_API_KEY" language="de">
    <App />
  </MagicTranslateProvider>
);

Usage

<T> component

Wrap any text, including plain HTML, in <T>. The original content is shown until its translation has loaded.

import { T } from '@magic-translate/react';

export const Welcome = ({ name }: { name: string }) => (
  <>
    <h1>
      <T>Welcome to our shop</T>
    </h1>
    <p>
      <T>
        Click <a href="/sale">here</a> to see <b>today's</b> deals.
      </T>
    </p>
    <p>
      <T>Hello {name}</T>
    </p>
  </>
);

The content is re-translated whenever it changes, for example when name changes in the example above.

All translations requested at the same time are sent to the API in a single request, and translations are cached, so you can use as many <T> components as you like.

Only text and plain HTML elements are allowed inside <T>. React components are not, because they cannot survive the translation; <T> throws if it finds one. Translate inside the component instead, or pass the translated string down as a prop.

useTranslate

Use useTranslate (or its alias useT) for plain strings, such as attributes, or anywhere a component does not fit:

import { useTranslate } from '@magic-translate/react';

export const Search = () => {
  const t = useTranslate();

  return <input placeholder={t('Search products')} />;
};

t returns the original text until the translation has loaded, and the component re-renders once it has.

Variables

Wrap values that change, such as names or counts, in v(). The text around them is translated once as a template, You have {0} unread messages, and cached, however many different values it is shown with. The values themselves are never sent to the API.

import { T, v } from '@magic-translate/react';

<T>
  Hello {v(user.name)}, you have {v(count)} unread messages
</T>;

The placeholders are numbered in order, and a translation can move them to wherever the language needs them. v() can also hold a React component, such as a link.

With t, use a tagged template. Every ${} value is treated the same way:

const t = useTranslate();

t`You have ${count} unread messages`;

Without v(), a value is translated as part of the text, so every different value is a new translation.

Options per translation

Both the component and t can override the target language and the formality:

<T lang="fr" formality="formal">How are you?</T>

<input placeholder={t('Your name', { lang: 'fr', formality: 'formal' })} />

Switching languages

useLanguageSwitcher reads and changes the current language. Every <T> component and t call updates automatically.

import { useLanguageSwitcher } from '@magic-translate/react';

export const LanguageSwitcher = () => {
  const { currentLanguage, setLanguage } = useLanguageSwitcher();

  return (
    <select
      value={currentLanguage}
      onChange={(e) => setLanguage(e.target.value as typeof currentLanguage)}
    >
      <option value="en">English</option>
      <option value="de">Deutsch</option>
      <option value="fr">Français</option>
    </select>
  );
};

Configuration

MagicTranslateProvider accepts:

PropDescription
apiKeyRequired. Your Magic Translate API key.
languageRequired. The language content is translated to, e.g. 'de'.
sourceLanguageThe language your components are written in. Detected automatically by default; only set it if automatic detection gives wrong results.
formality'formal' or 'informal'. Not supported for all languages. Can be overridden per translation.
debugLogs requests to the console.

Other frameworks

License

MIT © i42 GmbH

版本与发布记录

@magic-translate/react 共发布 25 个版本,首次发布于 2022/12/22;过去 12 个月发布了 5 个版本。

版本发布日期距今
1.18.0latest2026/11/021 天前
1.17.32026/10/294 天前
1.17.22026/10/2410 天前
1.17.12026/09/141 个月前
1.17.02026/09/111 个月前
1.16.02025/03/011.7 年前

关于 @magic-translate/react 的常见问题

@magic-translate/react 是什么?

用于React的ai驱动i18n。将文本包装进去,它就会自动翻译,不需要翻译键或JSON语言文件。

如何安装 @magic-translate/react?

在项目目录运行 npm install @magic-translate/react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=17)、react-dom(>=17)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@magic-translate/react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@magic-translate/react 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@magic-translate/react 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.22.1。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@magic-translate/react 可以商用吗?

@magic-translate/react 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@magic-translate/react 还在维护吗?

最新版本 1.18.0 发布于 2026/11/02(1 天前);至今共发布 25 个版本,其中过去 12 个月发布了 5 个;首次发布于 2022/12/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。