inlang

Questa pagina mostra le strutture JSON concrete usate da bundle, messaggi, varianti, dichiarazioni, selettori, corrispondenze e pattern.

Usa queste strutture quando inserisci dati tramite project.db o insertBundleNested(). La funzione importFiles() di un plugin restituisce le stesse strutture, con bundleId e messageId in camelCase al posto di bundle_id e message_id. L'SDK li mappa sulle colonne del database.

Messaggio minimo

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

Un pattern è un array. Può contenere insieme testo, espressioni e markup.

Testo

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

Interpolazione

Usa una dichiarazione input-variable per le variabili fornite dal chiamante, poi fai riferimento a essa con un'espressione.

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

Espressione con annotazione

Le annotazioni descrivono funzioni di formattazione. Sono i plugin a stabilire quali annotazioni possono importare o esportare.

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

Le opzioni possono usare valori letterali o riferimenti a variabili:

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

Markup

Il markup è rappresentato tramite parti del pattern. Questo esempio corrisponde a 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" },
];

Il markup può includere opzioni e attributi:

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

Selettori e corrispondenze

I selettori stabiliscono quali variabili un messaggio usa per scegliere una variante. Le corrispondenze di ogni variante devono fare riferimento ai nomi dei selettori.

Corrispondenza letterale

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

Corrispondenza catch-all

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

Selettore in stile plurale

Usa una variabile di input per il valore passato dal chiamante, una variabile locale per il valore derivato del selettore e varianti che corrispondono alla variabile locale.

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

Collegamento delle varianti: CRUD o importazione tramite plugin

Usa message_id quando hai già l'id effettivo della riga del messaggio. È la forma tipica delle scritture CRUD dirette e di insertBundleNested().

const messageId = crypto.randomUUID();

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

Usa messageBundleId e messageLocale quando restituisci varianti dall'importFiles() di un plugin. In questo flusso gli ID dei messaggi vengono spesso omessi, così l'SDK può generarli o riutilizzarli durante l'importazione.

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

L'SDK risolve la coppia messageBundleId + messageLocale nel messaggio corrispondente e genera gli id quando necessario.

Regola pratica:

  • Scritture dirette nel database: usa message_id.
  • insertBundleNested(): usa message_id e riutilizza lo stesso id dell'oggetto messaggio.
  • importFiles() del plugin: usa messageBundleId insieme a messageLocale, a meno che il plugin non gestisca volutamente in autonomia id stabili per i messaggi. In quel caso, usa messageId.

Prossimi passi