inlang

このページでは、バンドル、メッセージ、バリアント、宣言、セレクター、一致条件、パターンで使われる具体的なJSON構造を紹介します。

project.db や insertBundleNested() でデータを挿入するときは、これらの構造を使ってください。プラグインの importFiles() も同じ構造を返しますが、bundle_id と message_id の代わりにキャメルケースの bundleId と messageId を使います。これらはSDKによってデータベースのカラムにマッピングされます。

最小構成のメッセージ

const messageId = crypto.randomUUID();
const variantId = crypto.randomUUID();

await insertBundleNested(project.db, {
  id: "greeting",
  declarations: [],
  messages: [
    {
      id: messageId,
      bundle_id: "greeting",
      locale: "en",
      selectors: [],
      variants: [
        {
          id: variantId,
          message_id: messageId,
          matches: [],
          pattern: [{ type: "text", value: "Hello world!" }],
        },
      ],
    },
  ],
});

パターン

pattern は配列です。テキスト、式、マークアップを混在させられます。

テキスト

[{ type: "text", value: "Hello world!" }];

補間

呼び出し元から渡される変数には input-variable 宣言を使い、式で参照します。

const messageId = crypto.randomUUID();
const variantId = crypto.randomUUID();

{
  id: "greeting",
  declarations: [{ type: "input-variable", name: "name" }],
  messages: [
    {
      id: messageId,
      bundle_id: "greeting",
      locale: "en",
      selectors: [],
      variants: [
        {
          id: variantId,
          message_id: messageId,
          matches: [],
          pattern: [
            { type: "text", value: "Hello " },
            {
              type: "expression",
              arg: { type: "variable-reference", name: "name" },
            },
            { type: "text", value: "!" },
          ],
        },
      ],
    },
  ],
}

アノテーション付きの式

アノテーションはフォーマット関数を表します。どのアノテーションをインポート・エクスポートできるかは、プラグインが決定します。

{
  type: "expression",
  arg: { type: "variable-reference", name: "count" },
  annotation: {
    type: "function-reference",
    name: "number",
    options: [],
  },
}

オプションにはリテラルまたは変数参照を使用できます:

{
  type: "function-reference",
  name: "number",
  options: [
    {
      name: "style",
      value: { type: "literal", value: "currency" },
    },
    {
      name: "currency",
      value: { type: "variable-reference", name: "currency" },
    },
  ],
}

マークアップ

マークアップはパターンのパーツとして表現されます。この例は Click <link>here</link><icon/> に対応します。

[
  { type: "text", value: "Click " },
  { type: "markup-start", name: "link" },
  { type: "text", value: "here" },
  { type: "markup-end", name: "link" },
  { type: "markup-standalone", name: "icon" },
];

マークアップにはオプションと属性を含めることができます。

{
  type: "markup-start",
  name: "link",
  options: [
    {
      name: "href",
      value: { type: "literal", value: "/pricing" },
    },
  ],
  attributes: [
    {
      name: "external",
      value: true,
    },
  ],
}

セレクターと一致条件

セレクターは、メッセージがバリアントを選択する際にどの変数を使うかを指定します。各バリアントの一致条件では、セレクター名を参照する必要があります。

リテラル一致条件

{
  type: "literal-match",
  key: "platform",
  value: "ios",
}

キャッチオールの一致条件

{
  type: "catchall-match",
  key: "platform",
}

複数形スタイルのセレクター

呼び出し側から渡される値には入力変数を、そこから導出したセレクターの値にはローカル変数を使います。バリアントは、このローカル変数に一致するように定義します。

const messageId = crypto.randomUUID();
const oneVariantId = crypto.randomUUID();
const otherVariantId = crypto.randomUUID();

{
  id: "items_count",
  declarations: [
    { type: "input-variable", name: "count" },
    {
      type: "local-variable",
      name: "countPlural",
      value: {
        type: "expression",
        arg: { type: "variable-reference", name: "count" },
        annotation: {
          type: "function-reference",
          name: "plural",
          options: [],
        },
      },
    },
  ],
  messages: [
    {
      id: messageId,
      bundle_id: "items_count",
      locale: "en",
      selectors: [{ type: "variable-reference", name: "countPlural" }],
      variants: [
        {
          id: oneVariantId,
          message_id: messageId,
          matches: [{ type: "literal-match", key: "countPlural", value: "one" }],
          pattern: [{ type: "text", value: "One item" }],
        },
        {
          id: otherVariantId,
          message_id: messageId,
          matches: [
            { type: "literal-match", key: "countPlural", value: "other" },
          ],
          pattern: [
            {
              type: "expression",
              arg: { type: "variable-reference", name: "count" },
            },
            { type: "text", value: " items" },
          ],
        },
      ],
    },
  ],
}

バリアントの紐付け:CRUD とプラグインインポートの違い

メッセージ行の ID がすでに確定している場合は message_id を使います。CRUD による直接書き込みや insertBundleNested() では、通常この形になります。

const messageId = crypto.randomUUID();

{
  id: "variant_1",
  message_id: messageId,
  matches: [],
  pattern: [{ type: "text", value: "Hello" }],
}

プラグインの importFiles() からバリアントを返す場合は、messageBundleId と messageLocale を使用します。このフローではメッセージ ID を省略することが多く、インポート時に SDK が ID を生成または再利用します。

{
  messageBundleId: "greeting",
  messageLocale: "en",
  matches: [],
  pattern: [{ type: "text", value: "Hello" }],
}

SDK は messageBundleId と messageLocale の組み合わせから対応するメッセージを特定し、必要に応じて id を生成します。

目安:

  • データベースへの直接書き込み: message_id を使います。
  • insertBundleNested(): message_id を使い、メッセージオブジェクトと同じ id を再利用します。
  • プラグインの importFiles(): messageBundleId と messageLocale を使います。ただし、プラグイン自身で安定したメッセージ id を意図的に管理している場合は messageId を使います。

次のステップ