inlang

本页展示 bundle、消息、变体、声明、选择器、匹配 和 pattern 所使用的具体 JSON 结构。

通过 project.db 或 insertBundleNested() 插入数据时,请使用这些结构。插件的 importFiles() 返回的结构相同,只是改用驼峰命名的 bundleId 和 messageId,而非 bundle_id 和 message_id。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

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" },
    },
  ],
}

标记

标记以 pattern 片段 (pattern parts) 的形式表示。此示例对应 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" },
];

标记 可以包含选项 (options) 和属性 (attributes) :

{
  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 在导入时生成或复用。

{
  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。

后续步骤