# gt-react: General Translation React SDK: 辞書 URL: https://generaltranslation.com/ja/docs/react/guides/dictionaries.mdx --- title: 辞書 description: 従来の辞書ベースの翻訳パターンの使用方法 --- {/* 自動生成: 直接編集しないでください。代わりに content/docs-templates/ にある template を編集してください。 */} 辞書は、キーと値のペアからなるネストしたオブジェクトで翻訳を整理する、従来型のアプローチです。[`` コンポーネント](/docs/react/guides/t)が推奨される方法ですが、辞書は他の i18n ライブラリから移行する場合や、翻訳を一元的に管理したい場合に便利です。 **推奨:** 新規プロジェクトでは [`` コンポーネント](/docs/react/guides/t) を使用してください。辞書のサポートは、主に移行や既存の翻訳ワークフローとの互換性を目的としています。 ## 辞書方式とコンポーネント翻訳 ### 辞書パターン ```tsx // dictionary.ts export default { greetings: { hello: 'Hello, world!', welcome: 'Welcome, {name}!' } }; // コンポーネントの使用例 function MyComponent() { const t = useTranslations(); return
{t('greetings.hello')}
; } ``` ### コンポーネントパターン ```tsx // コンポーネントの直接使用 - 推奨 function MyComponent() { return
Hello, world!
; } ``` ## トレードオフ ### 辞書の利点 * **一元管理** - すべての翻訳を1か所で管理 * **業界標準** - 他の i18n ライブラリでも一般的な、なじみのあるパターン * **移行しやすい** - 既存の翻訳を簡単に移行できる ### 辞書のデメリット * **複雑さ** - より多くのセットアップと設定が必要 * **保守性** - コンテンツが使用箇所から分離されるため、更新しにくい * **デバッグ性** - 翻訳をどのコンポーネントで使っているか追跡しにくい * **可読性** - キーだけでは実際の内容がわからない ## クイックスタート ### ステップ 1: 辞書を作成 プロジェクトのルート、または `src` ディレクトリに辞書ファイルを作成します。 ```ts title="dictionary.ts" const dictionary = { greetings: { hello: 'Hello, world!', welcome: 'Welcome to our app!' }, navigation: { home: 'Home', about: 'About', contact: 'Contact' } }; export default dictionary; ``` または、JSON形式を使うこともできます: ```json title="dictionary.json" { "greetings": { "hello": "Hello, world!", "welcome": "Welcome to our app!" }, "navigation": { "home": "Home", "about": "About", "contact": "Contact" } } ``` 次に、これを [``](/docs/react/api/components/gtprovider) コンポーネントに渡します: ```jsx title="index.js" copy import dictionary from "./dictionary.js"; import config from "./gt.config.json"; createRoot(document.getElementById("root")!).render( {/* [!code highlight] */} ); ``` ### ステップ2: コンポーネントで使う [`useTranslations`](/docs/react/api/dictionary/use-translations) Hook を使うと、辞書エントリにアクセスできます。 ```tsx import { useTranslations } from 'gt-react'; function MyComponent() { const t = useTranslations(); return (

{t('greetings.hello')}

{t('greetings.welcome')}

); } ``` ## 変数を使う `{variable}` 構文を使って、辞書エントリに変数を追加します。 ```ts title="dictionary.ts" const dictionary = { user: { greeting: 'Hello, {name}!', itemCount: 'You have {count} items', orderTotal: 'Total: ${amount}' } }; ``` ```tsx function UserDashboard() { const t = useTranslations(); return (

{t('user.greeting', { name: 'Alice' })}

{t('user.itemCount', { count: 5 })}

{t('user.orderTotal', { amount: 99.99 })}

); } ``` ## プレフィックスを使用する プレフィックスを使用して、辞書へのアクセス範囲を特定のセクションに限定します。 ```ts title="dictionary.ts" const dictionary = { dashboard: { header: { welcome: 'Welcome back!', lastLogin: 'Last login: {date}' }, stats: { totalUsers: 'Total Users: {count}', activeUsers: 'Active Users: {count}' } } }; ``` ```tsx function DashboardHeader() { // プレフィックスにより 'dashboard.header' へのアクセスを制限 const t = useTranslations('dashboard.header'); return (

{t('welcome')}

{/* -> dashboard.header.welcome */}

{t('lastLogin', { date: 'Today' })}

{/* -> dashboard.header.lastLogin */}
); } function DashboardStats() { // stats セクション用の別プレフィックス const t = useTranslations('dashboard.stats'); return (

{t('totalUsers', { count: 1000 })}

{/* -> dashboard.stats.totalUsers */}

{t('activeUsers', { count: 150 })}

{/* -> dashboard.stats.activeUsers */}
); } ``` ## 複数言語対応 ### 自動翻訳 (推奨) ほとんどのユーザーは、ベース辞書から翻訳を自動生成するために [`loadTranslations`](/docs/react/api/config/load-translations) を使用してください。 ```ts title="dictionary.ts" const dictionary = { common: { save: 'Save', cancel: 'Cancel', delete: 'Delete' }, forms: { required: 'This field is required', email: 'Please enter a valid email' } }; export default dictionary; ``` 次に、生成された翻訳ファイルを読み込む`loadTranslations`関数を作成します。 ```ts title="src/loadTranslations.ts" export default async function loadTranslations(locale: string) { const translations = await import(`./_gt/${locale}.json`); return translations.default; } ``` ``に渡します: ```tsx title="src/index.tsx" import loadTranslations from './loadTranslations'; import dictionary from './dictionary'; createRoot(document.getElementById("root")!).render( ); ``` GT は、ベース辞書に基づいて他言語の翻訳を自動生成します。設定されているすべての言語の翻訳を生成するには、`npx gt translate` を実行してください。 ### 手動翻訳ファイル (移行) 他の i18n ライブラリから移行する場合や、翻訳を手動で管理する場合は、[`loadDictionary`](/docs/react/api/config/load-dictionary) を使用します。 ```ts title="src/loadDictionary.ts" export default async function loadDictionary(locale: string) { const translations = await import(`../public/locales/${locale}.json`); return translations.default; } ``` この設定では、`public/locales/` ディレクトリから JSON 翻訳ファイルを読み込みます: **適切な方法を選んでください:** 自動翻訳生成を使う新規プロジェクトでは [`loadTranslations`](/docs/react/api/config/load-translations) を、既存の翻訳ファイルを移行する場合は [`loadDictionary`](/docs/react/api/config/load-dictionary) を使用してください。 ## 本番環境のセットアップ ### ビルドプロセス ビルドパイプラインに翻訳を組み込みます: ```json title="package.json" { "scripts": { "build": "npx gt translate && <...YOUR_BUILD_COMMAND...>" } } ``` ### 開発時と本番環境での動作 * **Development**: 辞書エントリは、開発用 API キーを使って on-demand で翻訳 * **Production**: すべての辞書の翻訳をビルド時に事前生成 ## コンポーネントと組み合わせる 辞書と[``コンポーネント](/docs/react/guides/t)は組み合わせて使えます。 ```tsx function MixedApproach() { const t = useTranslations(); return (
{/* 単純な文字列には辞書を使用 */}

{t('page.title')}

{/* 複雑なJSXにはTコンポーネントを使用 */}

This is a complex message with links.

{/* フォームラベルには辞書を使用 */}
); } ``` ## 次のステップ **実際の動作を見る:** 動作するデモとして、[辞書パターン のサンプルアプリ](https://github.com/gt-examples/dictionary-pattern)をご覧ください — [ライブプレビュー](https://dictionary-pattern.generaltranslation.dev)。 * [言語ガイド](/docs/react/guides/languages) - サポートする言語とロケールを設定する * [動的コンテンツガイド](/docs/key-concepts/dynamic-content) - runtime での翻訳に対応する * API リファレンス: * [`useTranslations` Hook](/docs/react/api/dictionary/use-translations)