著者:
    作成:2026-09-19最終更新:2026-09-19

    Intlayer リクエストコンテキストキー

    Intlayer エクスポートは、Remix 3 におけるリクエストコンテキストストレージ識別子として機能します。ルートハンドラーやカスタムミドルウェア内で、Remix コンテキストオブジェクトから Intlayer の状態を直接取得できます。

    使用方法

    intlayer() ミドルウェアが実行されると、IntlayerState オブジェクトが Intlayer キーの下でリクエストコンテキストに保存されます。任意のルートハンドラー内で取得できます:

    src/server.ts
    import { createRouter } from "remix/router";
    import { intlayer, Intlayer } from "remix-intlayer";
    
    const router = createRouter({
      middleware: [intlayer()],
    });
    
    router.get("/api/profile", (context) => {
      // context.get(Intlayer) 経由でアクセス
      const { locale, defaultLocale, availableLocales } = context.get(Intlayer);
    
      return Response.json({
        locale,
        defaultLocale,
        availableLocales,
      });
    });
    

    直接プロパティのショートハンド context.intlayer を使ってアクセスすることもできます:

    ts
    router.get("/api/status", (context) => {
      const currentLocale = context.intlayer.locale;
      return Response.json({ status: "ok", locale: currentLocale });
    });
    

    IntlayerState の構造

    IntlayerState オブジェクトには以下が含まれます:

    プロパティ型説明
    localeDeclaredLocales現在のリクエストに対して解決されたロケール。
    defaultLocaleDeclaredLocalesintlayer.config.ts で定義されたフォールバックロケール。
    availableLocalesDeclaredLocales[]プロジェクトで設定されたすべてのサポート対象ロケールの一覧。

    関連ドキュメント