著者:
    作成:2026-11-23最終更新:2026-11-23

    Lingui から Intlayer への移行

    Lingui から Intlayer に移行する理由

    Lingui はロケールごとに、アプリの全メッセージを含むコンパイル済みカタログを読み込みます。Intlayer はコンテンツを辞書単位に分割し、各ページが使うものだけを配信するため、バンドルとページのサイズを最大 50% 削減できます。

    Intlayer の辞書は 完全に型付け されており、それを使うコンポーネントの隣に配置できます。extract / compile の手順を覚えておく必要はありません。辞書はバンドラープラグインがビルドします。

    コンポーネントの隣に置かれたコンテンツは、LLM に 必要なコンテキストを減らします。Intlayer には、翻訳漏れをテストする CLI、LSP、MCP サーバー、agent skills が用意されています。

    CI/CD パイプラインで好きな LLM を使って不足しているメッセージを翻訳したり、Web プラットフォーム でバックグラウンドで翻訳したりできます。

    Intlayer はセルフホスト可能な ビジュアルエディター と フル機能の CMS を提供し、翻訳者やコピーライターがリアルタイムでコンテンツを編集できます。

    これらのライブラリの成り立ちを知るには、JavaScript i18n の歴史 をお読みください。

    移行戦略

    1. Compat adapter(既存アプリに推奨): @intlayer/lingui をインストールします。@lingui/core と @lingui/react と同じ API(i18n、setupI18n、I18nProvider、useLingui、Trans)を公開し、メッセージを Intlayer の辞書から提供します。マクロ( t`…` 、<Trans>、msg、plural)はこれまでとまったく同じようにコンパイルされます。
    2. 完全移行: Lingui の呼び出しを、useIntlayer とコンポーネントの隣に置いた .content.ts ファイルに少しずつ置き換えます。

    目次

    クイック移行

    1. 依存関係のインストール

      bash
      npx intlayer init --interactive
      
      --interactive フラグは任意です。AI エージェントの場合は intlayer-cli init を使用してください。

      このコマンドは環境を検出し、必要な package をインストールします。例:

      bash
      npm install intlayer react-intlayer @intlayer/lingui @intlayer/sync-po-plugin
      
      @lingui/core、@lingui/react、マクロプラグイン(@lingui/babel-plugin-lingui-macro または @lingui/swc-plugin)はインストールしたままにしてください。マクロは引き続き実行され、Lingui の package が TypeScript の型を提供します。
    2. Intlayer の設定

      Intlayer は Lingui のカタログを信頼できる唯一の情報源として読み込みます。Lingui のデフォルト構成(src/locales/{locale}/messages.po)の場合は、syncPO プラグインをそれらに向けます。

      intlayer.config.ts
      import { Locales, type IntlayerConfig } from "intlayer";
      import { syncPO } from "@intlayer/sync-po-plugin";
      
      const config: IntlayerConfig = {
        internationalization: {
          locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
          defaultLocale: Locales.ENGLISH,
        },
        plugins: [
          syncPO({
            // ./src/locales/en/messages.po → dictionary `messages`
            source: ({ key, locale }) => `./src/locales/${locale}/${key}.po`,
          }),
        ],
      };
      
      export default config;
      

      @lingui/format-json を使う場合は、ICU 形式で syncJSON を使用します。

      intlayer.config.ts
      import { syncJSON } from "@intlayer/sync-json-plugin";
      
      // ...
      plugins: [
        syncJSON({
          format: "icu",
          source: ({ key, locale }) => `./src/locales/${locale}/${key}.json`,
        }),
      ],
      
      明示的なメッセージ id(t({ id: "hero.title", message: "Welcome" }) または <Trans id="hero.title">)を使用してください。ソーステキストから生成された id はカタログと照合されません。
    3. Lingui の Vite プラグインを置き換える

      @lingui/vite-plugin を @intlayer/lingui/plugin に置き換えます。バインディングは lingui() のままです。Intlayer のビルドを実行し、@lingui/core と @lingui/react を @intlayer/lingui にエイリアスするため、ソースファイルの変更は不要です。

      vite.config.ts
      import { defineConfig } from "vite";
      import react from "@vitejs/plugin-react";
      import { lingui } from "@intlayer/lingui/plugin"; // was "@lingui/vite-plugin"
      
      export default defineConfig({
        plugins: [
          react({
            babel: { plugins: ["@lingui/babel-plugin-lingui-macro"] },
          }),
          lingui(),
        ],
      });
      
      マクロプラグインは React プラグイン(Babel または @lingui/swc-plugin)に残してください。これにより、Intlayer が解決する前にマクロが i18n._() 呼び出しにコンパイルされます。

    コンポーネントはそのまま動作し続けます。

    src/Hero.tsx
    import { Trans, useLingui } from "@lingui/react/macro";
    
    export const Hero = ({ name }: { name: string }) => {
      const { t } = useLingui();
    
      return (
        <header>
          <h1>{t({ id: "hero.title", message: "Welcome" })}</h1>
          <Trans id="hero.greeting">Hello {name}</Trans>
        </header>
      );
    };
    

    ロケールの切り替えは引き続き i18n.activate(locale) で行い、すべての useLingui() の利用箇所が更新されます。

    完全移行

    1. ランタイムでのカタログ読み込みを削除する

      オプション

      メッセージはビルド時にコンパイルされる Intlayer の辞書から提供されるようになりました。i18n.load() / i18n.loadAndActivate() の呼び出しはフォールバックとしてのみ機能し、開発時に警告を表示します。i18n.activate(locale) に置き換え、lingui compile の手順を削除してください。新しいメッセージ id の収集には lingui extract を引き続き使います。

    2. import の明示的なリネーム(任意)

      オプション

      エイリアスはすでに Vite プラグインが処理しています。依存関係を明示したい場合:

      変更前変更後
      import { i18n } from '@lingui/core'import { i18n } from '@intlayer/lingui'
      import { setupI18n } from '@lingui/core'import { setupI18n } from '@intlayer/lingui'
      import { I18nProvider } from '@lingui/react'import { I18nProvider } from '@intlayer/lingui'
      import { useLingui, Trans } from '@lingui/react'import { useLingui, Trans } from '@intlayer/lingui'
    3. コンテンツを .content.ts ファイルに移す

      オプション

      新しいコンポーネントでは、既存の Lingui の呼び出しと並行してネイティブの Intlayer コンテンツを使えます。

      src/hero.content.ts
      import { t, type Dictionary } from "intlayer";
      
      const heroContent = {
        key: "hero",
        content: {
          title: t({ en: "Welcome", fr: "Bienvenue", es: "Bienvenido" }),
        },
      } satisfies Dictionary;
      
      export default heroContent;
      
      src/Hero.tsx
      import { useIntlayer } from "react-intlayer";
      
      export const Hero = () => {
        const { title } = useIntlayer("hero");
      
        return <h1>{title}</h1>;
      };
      
    4. AI による翻訳を有効にする

      オプション
      bash
      # Test for missing translations (add to CI)
      npx intlayer test
      
      # Fill missing translations with AI
      npx intlayer fill
      

      補完された翻訳は Lingui のカタログに書き戻されます。AI の設定については CLI のドキュメント を参照してください。

    移行後に削除できるもの

    ファイル / パターン不要になる理由
    i18n.load() / カタログの動的 import辞書は Intlayer がコンパイルして読み込みます。
    lingui compile とコンパイル済み .js カタログIntlayer がカタログから直接辞書をビルドします。
    @lingui/vite-plugin@intlayer/lingui/plugin の lingui() に置き換えられます。

    TypeScript の設定

    生成された型を tsconfig.json に含めます。

    tsconfig.json
    {
      "include": [
        // ... your existing paths
        ".intlayer/**/*.ts",
      ],
    }
    

    Git の設定

    .gitignore
    # Ignore the files generated by Intlayer
    .intlayer
    

    Go Further