
react-intl 指南:設定 React 國際化
在 React 應用程式中設定 FormatJS react-intl,包括 IntlProvider、FormattedMessage、useIntl、ICU 訊息格式和自動翻譯。
改用 react-i18next? 查看 react-i18next 指南
安裝 react-intl
react-intl 是 FormatJS 項目的一部分,提供 React 元件和 hook,並按 ICU MessageFormat 標準格式化字串、數字、日期和複數。
npm install react-intl設定 IntlProvider
在根元件使用 IntlProvider 包裝應用程式。傳入當前語言和扁平 messages 物件,之後所有下級元件都可通過 FormattedMessage 或 useIntl 取得譯文。
import React from 'react';
import ReactDOM from 'react-dom/client';
import { IntlProvider } from 'react-intl';
import App from './App';
import enMessages from './messages/en.json';
import deMessages from './messages/de.json';
const messages: Record<string, Record<string, string>> = {
en: enMessages,
de: deMessages,
};
// Detect locale from browser or your routing layer
const locale = navigator.language.split('-')[0] || 'en';
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<IntlProvider locale={locale} messages={messages[locale] || messages.en}>
<App />
</IntlProvider>
</React.StrictMode>
);訊息檔案
為每種語言建立一個 JSON 檔案。react-intl 原生使用 ICU MessageFormat 語法,複數、select 和變數都以內嵌方式寫在訊息字串中。
// messages/en.json
{
"app.greeting": "Hello, {name}!",
"nav.home": "Home",
"nav.about": "About",
"nav.settings": "Settings",
"cart.itemCount": "{count, plural, one {# item} other {# items}} in your cart"
}
// messages/de.json
{
"app.greeting": "Hallo, {name}!",
"nav.home": "Startseite",
"nav.about": "Über uns",
"nav.settings": "Einstellungen",
"cart.itemCount": "{count, plural, one {# Artikel} other {# Artikel}} in Ihrem Warenkorb"
}在元件中使用譯文
react-intl 提供兩個主要 API:用於渲染翻譯 JSX 的 FormattedMessage 元件,以及用於命令式訪問的 useIntl hook,例如佔位文字、aria 標籤和編程式格式化。
FormattedMessage 元件
在 JSX 中使用 FormattedMessage 進行聲明式翻譯。傳入訊息 ID 和插值值,它會直接渲染翻譯字串。
import { FormattedMessage } from 'react-intl';
function Greeting({ userName }: { userName: string }) {
return (
<div>
<h1>
<FormattedMessage
id="app.greeting"
values={{ name: userName }}
/>
</h1>
<nav>
<a href="/"><FormattedMessage id="nav.home" /></a>
<a href="/about"><FormattedMessage id="nav.about" /></a>
</nav>
</div>
);
}useIntl Hook
需要將譯文作為普通值使用時,請呼叫 useIntl(),例如輸入框佔位文字、aria-label、document.title,或向非 React API 傳遞字串。它還提供 formatNumber、formatDate 和 formatRelativeTime。
import { useIntl } from 'react-intl';
function SearchBar() {
const intl = useIntl();
return (
<input
type="search"
placeholder={intl.formatMessage({ id: 'search.placeholder' })}
aria-label={intl.formatMessage({ id: 'search.ariaLabel' })}
/>
);
}
// useIntl also gives you formatNumber, formatDate, formatRelativeTime:
function PriceTag({ amount, currency }: { amount: number; currency: string }) {
const intl = useIntl();
return (
<span>{intl.formatNumber(amount, { style: 'currency', currency })}</span>
);
}富文字(譯文中的 HTML)
在訊息字串中使用類似 XML 的標籤,將 JSX 嵌入譯文。通過 values 屬性傳入標籤處理程式,即可在翻譯訊息中渲染連結、粗體文字或任意 React 元件。
import { FormattedMessage } from 'react-intl';
// Message: "By signing up, you agree to our <link>Terms</link>."
// Key: "signup.terms"
// Value: "By signing up, you agree to our <link>Terms</link>."
function SignUp() {
return (
<FormattedMessage
id="signup.terms"
values={{
link: (chunks) => <a href="/terms" className="underline">{chunks}</a>,
}}
/>
);
}使用 @formatjs/cli 提取訊息
FormatJS 提供 CLI,可自動從源程式碼提取訊息 ID 到 JSON 檔案。這樣無需手動記錄,也能讓訊息檔案與元件保持同步。
# Install the CLI
npm install -g @formatjs/cli
# Extract messages from source code into a JSON file
formatjs extract 'src/**/*.tsx' --out-file messages/en.json --id-interpolation-pattern '[sha512:contenthash:base64:6]'
# Or use explicit IDs (recommended):
formatjs extract 'src/**/*.tsx' --out-file messages/en.json
# Compile messages for production (optional, improves perf)
formatjs compile messages/en.json --out-file compiled/en.json
formatjs compile messages/de.json --out-file compiled/de.json複數與 ICU Select
react-intl 原生使用 ICU MessageFormat。複數、基於性別的 select 和嵌套格式都直接寫在訊息字串中,無需後綴約定或單獨鍵。
// ICU MessageFormat syntax — react-intl uses this natively
// English
{
"cart.itemCount": "{count, plural, one {# item} other {# items}} in your cart",
"inbox.unread": "You have {count, plural, =0 {no unread messages} one {# unread message} other {# unread messages}}"
}
// Arabic — 6 plural forms
{
"cart.itemCount": "{count, plural, zero {لا عناصر} one {عنصر واحد} two {عنصران} few {# عناصر} many {# عنصرًا} other {# عنصر}} في سلتك"
}
// Japanese — 1 form (other)
{
"cart.itemCount": "カートに{count}個の商品があります"
}用於性別和角色的 ICU Select
對於性別、用戶角色或狀態值等依賴上下文的譯文,請使用 ICU select 語法。select 表達式會根據提供的值選擇正確變體。
// Gender-dependent messages using ICU select
{
"user.greeting": "{gender, select, male {He} female {She} other {They}} liked your post.",
"user.invitation": "{role, select, admin {You can manage all settings.} editor {You can edit content.} other {You can view content.}}"
}
// Usage:
<FormattedMessage
id="user.greeting"
values={{ gender: user.gender }}
/>自動保證翻譯質素
常見問題
過度依賴 defaultMessage
使用嵌套物件而非扁平鍵
IntlProvider 導致重新渲染
測試中缺少 IntlProvider
推薦的檔案結構
my-react-app/
├── messages/
│ ├── en.json # Source of truth (English)
│ ├── de.json # German
│ ├── ja.json # Japanese
│ └── es.json # Spanish
├── compiled/ # Optional: compiled messages for prod
│ ├── en.json
│ └── ...
├── src/
│ ├── main.tsx # App entry with IntlProvider
│ ├── App.tsx
│ └── components/
│ ├── Greeting.tsx # Uses FormattedMessage
│ └── SearchBar.tsx # Uses useIntl
└── package.json立即試用 i18n Agent
將翻譯檔案拖放到此處
JSON, YAML, PO, XML, CSV, Markdown, Properties
或點擊選擇檔案
目標語言
使用 react-intl-locale-chain 實現語言回退
如果 pt-BR 等區域語言缺少翻譯鍵,react-intl 會直接跳到預設語言,而不會先檢查父語言 pt。
npm install react-intl-locale-chain<LocaleChainProvider
fallbacks={{
'pt-BR': ['pt', 'en'],
'zh-Hant-HK': ['zh-Hant', 'zh', 'en'],
}}
defaultLocale="en"
>
<App />
</LocaleChainProvider>查看語言回退指南,瞭解受支援框架的完整列表和 75 條內置回退鏈。 Learn more →