# gt-next: General Translation Next.js SDK: 辞書 URL: https://generaltranslation.com/ja/docs/next/guides/dictionaries.mdx --- title: 辞書 description: 従来の辞書ベースの翻訳パターンの使い方 --- 辞書は、キーと値のペアからなるネストされたオブジェクトで翻訳を整理する、従来型のアプローチです。[`` コンポーネント](/docs/next/guides/t) が推奨される方法ですが、辞書は他の i18n ライブラリから移行する場合や、翻訳を一元的に管理したい場合に便利です。 **推奨:** 新しいプロジェクトでは [`` コンポーネント](/docs/next/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" } } ``` [`withGTConfig`](/docs/next/api/config/with-gt-config) 関数は、プロジェクトのルートまたは `src` ディレクトリ内の辞書ファイルを自動的に検出します。 ### ステップ 2: コンポーネントで使う [`useTranslations`](/docs/next/api/dictionary/use-translations) Hook を使うと、辞書のエントリにアクセスできます。 #### クライアントコンポーネント ```tsx import { useTranslations } from 'gt-next'; function MyComponent() { const t = useTranslations(); return (

{t('greetings.hello')}

{t('greetings.welcome')}

); } ``` #### サーバーコンポーネント ```tsx import { getTranslations } from 'gt-next/server'; async function MyServerComponent() { const d = await getTranslations(); 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/next/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` 関数を作成します。 ```js title="src/loadTranslations.ts" export default async function loadTranslations(locale) { const translations = await import(`../public/locales/${locale}.json`); return translations.default; } ``` [`withGTConfig`](/docs/next/api/config/with-gt-config) は、`src/` ディレクトリまたはプロジェクトルートにある `loadTranslations.[js|ts]` ファイルを自動的に読み込みます。 GT は、ベース辞書に基づいて他の言語向けの翻訳を自動生成します。設定されているすべての言語の翻訳を生成するには、`npx gt translate` を実行します。 ### 手動翻訳ファイル (移行) 他の i18n ライブラリからの移行時や、翻訳を手動で管理している場合は、[`loadDictionary`](/docs/next/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/next/api/config/load-translations) を、既存の翻訳ファイルを移行する場合は [`loadDictionary`](/docs/next/api/config/load-dictionary) を使用します。 ## 本番環境のセットアップ ### ビルドプロセス ビルドパイプラインに翻訳を組み込みます。 ```json title="package.json" { "scripts": { "build": "npx gt translate && <...YOUR_BUILD_COMMAND...>" } } ``` ### 開発時と本番時の動作 * **Development**: 辞書エントリは開発用 API キーを使って on-demand で翻訳されます * **本番環境**: すべての辞書の翻訳はビルド時に事前生成されます ## コンポーネントと組み合わせる 辞書と[`` コンポーネント](/docs/next/guides/t)は組み合わせて使用できます。 ```tsx function MixedApproach() { const t = useTranslations(); return (
{/* 単純な文字列には辞書を使用 */}

{t('page.title')}

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

This is a complex message with links.

{/* フォームラベルには辞書を使用 */}
); } ``` ## 次のステップ **実際の動作を見る:** 動作するデモは、[dictionary pattern example app](https://github.com/gt-examples/dictionary-pattern) を確認してください — [ライブプレビュー](https://dictionary-pattern.generaltranslation.dev)。 * [Languages Guide](/docs/next/guides/languages) - サポートする言語とロケール設定を行います * [Dynamic Content Guide](/docs/next/guides/dynamic-content) - runtime の翻訳ニーズに対応します * API リファレンス: * [`useTranslations` Hook](/docs/next/api/dictionary/use-translations) * [`getTranslations` Function](/docs/next/api/dictionary/get-translations)