著者:
    作成:2026-06-12最終更新:2026-06-12

    動的レコード

    動的レコード(dynamic record)とは、その同一性が連続するインデックスや名前付きバリアントではなく、meta フィールドで宣言された任意のキー・値ペアの集合によって定義されるコンテンツファイルです。Intlayerはランタイムにこれらのフィールドをセレクターとして使用するため、CMSレコードやユーザー固有のコピー、あるいはビルド時にキーが不明なあらゆるコンテンツを指定することが可能になります。

    動적レコードの宣言

    product-copy.abc.content.ts
    import { t, type Dictionary } from "intlayer";
    
    const dictionary = {
      key: "product-copy",
      meta: {
        id: "prod_abc",
        userId: "user_123",
      },
      content: {
        name: t({ en: "Widget Pro", fr: "Widget Pro" }),
        description: t({ en: "The best widget.", fr: "Le meilleur widget." }),
      },
    } satisfies Dictionary;
    
    export default dictionary;
    product-copy.abcd.content.ts
    import { t, type Dictionary } from "intlayer";
    
    const dictionary = {
      key: "product-copy",
      meta: {
        id: "prod_abcd",
        userId: "user_123",
      },
      content: {
        name: t({ en: "Widget Lite", fr: "Widget Lite" }),
        description: t({ en: "A lighter option.", fr: "Une option plus légère." }),
      },
    } satisfies Dictionary;
    
    export default dictionary;

    動的レコードの消費

    セレクターでは、すべての meta フィールドが 必須 です。いずれかのフィールドを省略すると null が返され、TypeScriptのエラーになります。

    ProductCopy.tsx
    import { useIntlayer } from "react-intlayer";
    
    export const ProductCopy = ({
      productId,
      userId,
    }: {
      productId: string;
      userId: string;
    }) => {
      const content = useIntlayer("product-copy", { id: productId, userId });
      // TypeScriptは、`id` と `userId` の両方が提供されていることを強制します。
    
      if (!content) return null;
    
      return <p>{content.description}</p>;
    };

    ロケールを明示的に指定する場合

    tsx
    const content = useIntlayer("product-copy", {  id: "prod_abc",  userId: "user_123",  locale: "ja",});

    metaフィールドの不足 — コンパイルエラー

    ts
    // 型エラー: `userId` が不足していますconst content = useIntlayer("product-copy", { id: "prod_abc" });

    読み込みモード (loading mode)

    動的レコードは通常、遅延読み込み(lazy loading)されます。制御するには、ディクショナリで importMode を設定します:

    ts
    const dictionary = {
      key: "product-copy",
      importMode: "fetch", // または "dynamic"
      meta: { id: "prod_abc", userId: "user_123" },
      content: { … },
    } satisfies Dictionary;
    
    export default dictionary;

    staticdynamicfetch モードの詳細については、バンドルの最適化 を参照してください。

    一般的なユースケース

    • CMSで管理される商品ごとのマーケティングコピー
    • ユーザー固有またはアカウント固有のコンテンツ
    • 不透明なランタイムIDをキーとするあらゆるコンテンツ