DocsIntegrations (experimental)

Storyblok (experimental)

@generic-translation/storyblok - translate Storyblok stories through Generic Translation with a CLI, library and webhook handler. Experimental and unverified against a real Storyblok space.

On this page

Experimental. Unverified against a real Storyblok space. The author had no Storyblok space, so nothing here has been run against Storyblok’s servers. The Management API client was written from Storyblok’s documentation and tested against an in-memory fake that returns the same shapes. Only the Generic Translation half was exercised live. Details are in Status.

Source: generic-translation-integrations/storyblok. Not on npm yet; link it from a checkout. It is fetch-only (no Storyblok SDK) and runs on Node 20+ and Cloudflare Workers.

Quickstart

Use a duplicate or sandbox space first, and start with --dry-run.

export STORYBLOK_TOKEN=...        # personal access token (or STORYBLOK_OAUTH_TOKEN)
export GENERIC_TRANSLATION_API_KEY=gt-api-...   # needs translations:generate

# preview
generic-translation-storyblok translate --space 123456 --story home --locales es,fr --dry-run
# field-level, whole space
generic-translation-storyblok translate --space 123456 --all --locales es,fr --source en
# folder-level, source stories under en/
generic-translation-storyblok translate --space 123456 --all --locales es --mode folder --source-folder en

As a library:

import { GenericTranslation } from "@generic-translation/core";
import { StoryblokClient, genericTranslationTranslator, translateStory, translateSpace } from "@generic-translation/storyblok";

const client = new StoryblokClient({ spaceId: 123456, token: process.env.STORYBLOK_TOKEN!, region: "eu" });
const translator = genericTranslationTranslator(new GenericTranslation({ apiKey: process.env.GENERIC_TRANSLATION_API_KEY }));

await translateStory({ client, translator, story: "home", locales: ["es", "fr"], mode: "field" });

Reference

CLI flags

--story <id|full-slug> or --all, --locales, --source, --mode field|folder, --source-folder, --starts-with, --concurrency, --region eu|us|ca|ap|cn, --oauth, --all-fields, --translate-slugs, --publish, --dry-run.

What gets translated

Fields of type text, textarea, markdown and richtext, found by walking story content with the component schemas (nested bloks, and bloks embedded in rich text, included). By default only fields flagged translatable: true are touched; translatableOnly: false or --all-fields lifts that. Assets, multilinks, options, numbers, booleans, table and custom fields are left alone.

  • text / textarea: one ICU message, with braces and apostrophes escaped.
  • markdown: split on blank lines (code fences skipped) and sent as opaque STRING messages.
  • richtext: one message per block. Runs of marked text become numbered tags such as Read the <0><1>docs</1></0> and <2>learn</2><3/>now, where tag 1 is a link and 3 is a hard break. Marks and attributes (href, target, uuid, heading level) are kept and put back onto whatever order the translator returns. A translation that drops or invents tags is rejected and that block keeps its source text with a warning. Code blocks are skipped.

If Generic Translation cannot translate a field, it is left untouched and a warning is returned.

Modes

  • field (default): writes title__i18n__es and similar keys next to the originals of the same story, in one PUT for all locales. With translateSlugs it also sends translated_slugs_attributes.
  • folder: finds or creates es/... folders and creates or updates the translated copy (same group_id as the source). sourceFolder strips a source-language folder from the target path. Stories inside a target language folder are never used as sources.

Re-runs are idempotent: nothing is written when the result equals what is stored.

Webhook handler

import { createStoryblokWebhookHandler } from "@generic-translation/storyblok";

const handler = createStoryblokWebhookHandler({
  client, translator, locales: ["es", "fr"], secret: process.env.STORYBLOK_WEBHOOK_SECRET!,
  waitUntil: (p) => ctx.waitUntil(p), // Workers; omit on Node to answer after translating
});

Requests without a valid webhook-signature get 401. Translating with publish: true would re-fire the “published” event; unchanged results are not written, so the loop ends after one extra delivery. Prefer saving as draft.

Lower-level exports: extractFields(content, components), encodeInline / decodeInline, the StoryblokClient methods, and your own Translator function if you do not want Generic Translation.

Status

Verified for real Not verified against Storyblok
The Generic Translation half: a live test sent text extracted from a fixture story to the Generic Translation API, reassembled it, and checked that marks, link attributes, hard breaks and code blocks survived. It passed once. Every Management API call: endpoints, query parameters, bodies, auth headers, pagination headers and rate-limit behavior.
Extraction, rich-text tag encoding, reassembly, field versus folder logic, retry, pagination and concurrency, and the webhook signature maths (against Node’s crypto), all against the in-memory fake. That a story written with field__i18n__<lang> keys or translated_slugs_attributes displays correctly in the Visual Editor.
Folder mode: creating folders, group_id linking as alternates, with_slug lookups.
Webhook payload field names and that Storyblok signs the raw body with HMAC-SHA1 in webhook-signature (the header is sent only if a secret is set).
Whether a 429 carries Retry-After (honored if present, otherwise exponential backoff). Plan limits differ; the default throttle is 3 requests per second.
Rich-text node and mark names beyond those in the fixtures. Unknown inline nodes become opaque self-closing tags and unknown marks are carried through untouched.

Something wrong or unclear on this page? Open an issue.

Type to search the documentation.