# gt: General Translation CLI tool: 自動 JSX インジェクション を使う URL: https://generaltranslation.com/ja/docs/cli/guides/using-auto-jsx.mdx --- title: 自動 JSX インジェクション を使う description: 自動 JSX インジェクション を使って、ビルド時に翻訳可能な JSX テキストを General Translation のコンポーネントでラップする方法。 related: links: - /docs/cli/guides/using-autoderive - /docs/cli/guides/generating-translations - /docs/cli/guides/configuring - /docs/cli/guides/managing-translations --- 自動 JSX インジェクション を使用すると、コンパイラがビルド時に翻訳可能な JSX テキストを [``](/docs/react/reference/components/t) コンポーネントでラップするため、手動で追加する必要がありません。 自動 JSX インジェクション はデフォルトで無効になっています。これは React のシングルページアプリ (SPA) と Next.js の webpack ビルドで動作します。 ## 自動 JSX インジェクションを有効にする [#enable] コンパイラをインストールします。 ```bash npm install @generaltranslation/compiler ``` ```bash yarn add @generaltranslation/compiler ``` ```bash bun add @generaltranslation/compiler ``` ```bash pnpm add @generaltranslation/compiler ``` `gt.config.json` の `files.gt.parsingFlags` で `enableAutoJsxInjection` を設定します。これにより、CLI での抽出とコンパイラによる変換を同期した状態に保てます。 ```json title="gt.config.json" { "files": { "gt": { "parsingFlags": { "enableAutoJsxInjection": true } } } } ``` ## ビルドを設定する [#configure-build] `gt.config.json` フラグは、General Translation コンパイラの実行時に適用されます。 ### React SPAs `@generaltranslation/compiler` を Vite、webpack、Rollup、Rspack、または esbuild の設定に追加します。adapter の例については、[SPA translations での開発](/docs/react/guides/developing-spa-translations#setup)を参照してください。 ### Next.js Babel ベースの webpack コンパイラを使用するように `gt-next` を設定します。 ```ts title="next.config.ts" import { withGTConfig } from 'gt-next/config'; const nextConfig = {}; export default withGTConfig(nextConfig, { experimentalCompilerOptions: { type: 'babel', enableAutoJsxInjection: true, }, }); ``` webpack で開発サーバーと本番ビルドの両方を実行します: ```json title="package.json" { "scripts": { "dev": "next dev --webpack", "build": "next build --webpack" } } ``` Next.js 16 では、デフォルトで Turbopack が使用されます。Turbopack は Babel コンパイラを無効にし、自動インジェクションがスキップされることを警告し、JSX はラップされないままになります。[Next.js compiler options](/docs/react/nextjs/config#compiler-options) を参照してください。 ## 仕組み [#how] インジェクションを使わない場合は、翻訳可能なテキストを自分でラップします。 ```jsx import { T } from 'gt-react'; function Welcome() { return (

Welcome to our app

); } ``` Next.js のプロジェクトでは、代わりに `gt-next` から [``](/docs/react/reference/components/t) をインポートします。 インジェクションを有効にすると、コンパイラが翻訳可能なテキストを検出し、ビルド時に自動的にラップします。 ```jsx function Welcome() { return

Welcome to our app

; } ``` すでに [``](/docs/react/reference/components/t) でラップされているテキストは、[``](/docs/react/reference/components/var)、[``](/docs/react/reference/components/branch)、[``](/docs/react/reference/components/plural)、[``](/docs/react/reference/components/derive) などの他の General Translation コンポーネントと同様に、そのまま保持されます。 ## Next steps - /docs/cli/guides/using-autoderive - /docs/cli/guides/generating-translations - /docs/cli/guides/configuring - /docs/cli/guides/managing-translations