Skip to content

fix: write downloaded blocks/json fields and component lists correctly (nested, incremental, structure changes) - #131

Merged
david-vaclavek merged 4 commits into
mainfrom
fix/blocks-json-download-nested-fields
Nov 11, 2026
Merged

david-vaclavek merged 4 commits into
mainfrom
fix/blocks-json-download-nested-fields

Conversation

@david-vaclavek

Copy link
Copy Markdown
Contributor

Summary

Fixes how downloaded translations are written back to Strapi for blocks (rich text) and json fields and for lists of components. It applies to every download path: UI Full Sync, UI "Download changes", and the webhook.

Found while localizing a real Strapi 5 site: Czech source, English via Localazy AI, and an About page built as a dynamic zone of components with blocks fields everywhere. Then generalized and checked against every place Strapi allows these fields.

What 1.7.0 did wrong

  • Two blocks/json fields in one component (hero.title + hero.line): the second field's leaves were written into the first, and the second stayed in the source language.
  • Blocks/json inside components nested in a dynamic zone (timeline.stops[].notes, stops[].quote.text): dropped. The final write replaced the whole stops list, so its plain fields (label, captions) were lost too.
  • Incremental sync ("Download changes", or a webhook with incremental sync): fields were rebuilt from the changed leaves only. Every unchanged leaf fell back to source text. On the site above, a stale-cursor run put Czech paragraphs back into the English story of 11 of 12 sessions, the simplest case (one top-level blocks field).
  • Lists with gaps: items were positioned by their source index and then compacted. Any item without downloaded values shifted every later item onto its neighbour. This happens in Full Sync too, e.g. a gallery item without a caption.
  • Updating blocks/json deep-merged into the old value, so removed marks, paragraphs and keys stayed.
  • Source structure changes: when a dynamic zone item or list item was removed, inserted or replaced in the source, the update merged translations into unrelated items or mixed two components into one item. Strapi rejects such an item, and that entry then fails on every sync.
  • Relations inside dynamic zones and repeatable components on update: Document with id "…", locale "null" not found. The lookups ran in an un-awaited forEach(async …) while Strapi was already resolving the payload.
  • json values: numeric keys ({"2024": …}) became arrays thousands of items long. Array items with an id landed at index id. Top-level arrays were handled as plain fields, and populate added stray properties (links.links).

Changes

  • locateDownloadedField finds the exact field segment of a downloaded key: behind dynamic zone id;component segments and component ids, and for json paths ending in indexes or numeric keys. Leaves are accumulated per full field path.
  • collectDownloadedContent (the extracted, pure download loop) takes an entry with any changed key with all of its keys, so incremental sync rewrites a changed entry exactly as Full Sync would.
  • No array compaction (resetArrayKeysDeep removed). Both callers merge onto a complete entry.
  • json fields are rebuilt like blocks (overlayJsonTranslation): a copy of the source value with string leaves replaced by translations, looked up with the same key segments flattenObject produces. On update, blocks and json fields replace the localized value instead of merging into it.
  • alignLocalizedEntryToSource: when a list that receives downloaded values has a different layout in the source (length, or dynamic zone component sequence), the localized list is rebuilt from the source items (without their component ids) before merging. Lists with the same layout merge as before.
  • populateCreateUpdateEntryWithBaseEntry awaits dynamic zone and repeatable items, and no longer walks into json/blocks values.
  • Pure refactors: parseLocalazyKey → utils/parse-localazy-key.ts (the service delegates to it), and the create payload merge → buildLocalizedCreateEntry. findSetupModelByCollectionUid returns undefined instead of throwing for a model without setup.

Behaviour changes worth noting

  • An incremental download now rewrites each changed entry completely (as Full Sync does), not only the changed keys.
  • For json fields (as already for blocks), leaves without a downloaded translation now show the source value instead of keeping an older localized value. Keys present only in the localization are dropped on update.
  • Relations keep their existing rule: a localized relation is replaced when the source has one and kept when it has none. So after a reorder that keeps a list's length and component types (undetectable by structure), translated text and source-defined relations land correctly, but a non-translatable value the source leaves empty stays with its slot.

Tests

  • transfer-round-trip.test.ts runs upload → translate → download through the plugin's own transforms: projectBlocksFields, flattenObject, pickEntries, getPickPathsWithComponents, collectDownloadedContent, and the create/update builders.
    • The schema has blocks/json at the top level (side by side), in single, repeatable and nested components, in dynamic zones, and in repeatable components inside dynamic zone items, plus relations, rich json and a list item with nothing to translate.
    • Expected values come from an independent schema walk.
    • Cases: create, update, incremental, unchanged entries, two languages in one run, stale keys still active in Localazy, source structure changes (marks, paragraphs, a removed dynamic zone item, an inserted stop, a reorder), and json arrays on an empty localization.
  • overlay-json-translation.test.ts, json-fields-download.test.ts, populate-create-update-entry-with-base-entry.test.ts are focused tests. Each behaviour test was checked to fail on 1.7.0.
  • Whole suite: 28 suites / 274 tests. test:ts:back, test:ts:front, lint (only the existing admin warning), format:check, build and verify pass locally.

Verified on a real Strapi 5.50 instance

On a local mirror of the site above, with the built plugin:

  • An English Full Sync creates/updates the About page correctly: all 25 blocks fields keep the source structure, and relations are kept.
  • Incremental runs with 15 / 40 / 120 random keys marked changed in the sync cursor leave the English About and all sessions byte-identical. The same 120-key run on 1.7.0 fails the About update (relation error) and reverts paragraphs of 11 sessions to Czech.
  • With one dynamic zone section removed from the Czech About, a Full Sync rebuilds the English About to the new 7-section layout, fully translated, with no errors.

Not changed

  • Key event numbers are int64 and arrive as JS numbers (precision ≈ 1 000 at their magnitude). Real gaps between events are far larger, so this is left as is.

🤖 Generated with Claude Code

david-vaclavek and others added 4 commits November 11, 2026 16:15
…components

A json/blocks field is uploaded as one key per leaf. On download the leaves
were grouped by their owning entry/component only, and the key was split at
the wrong segment inside dynamic zone components, so:

- two json/blocks fields of one component overwrote each other and the
  second one stayed in the source language,
- blocks inside components nested in a dynamic zone were dropped, and the
  final write replaced the whole component list, losing its plain fields.

`locateDownloadedField` now finds the exact field segment of a key (also
behind dynamic zone `id;component` segments and component ids, and for json
values whose path ends in array indexes or numeric keys), and leaves are
accumulated per full field path.

Incremental sync now takes an entry with any changed key with all of its
keys, so a changed entry is rewritten exactly as a full sync would, instead
of rebuilding fields from the changed leaves only (which put source text
back into every unchanged leaf).

The per-key download loop moves to `collectDownloadedContent` and key
parsing to `parseLocalazyKey`, both pure and tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ource structure

- Repeatable component and dynamic zone items are positioned by the source
  entry; the arrays were then compacted, so any item without downloaded
  values (an empty caption, or every unchanged item in an incremental sync)
  shifted all later items onto their neighbours. The compaction is removed;
  both callers merge onto a complete entry.
- On update, a blocks or json field is replaced with the value rebuilt from
  the current source value instead of being deep-merged into the localized
  one, which kept marks, paragraphs and keys removed from the source.
- json fields are rebuilt like blocks: a copy of the source value with each
  string leaf replaced by its translation, found by the same key segments
  `flattenObject` produces (ids included). Object/array shapes, numeric keys
  and non-string values come from the source, and leaves no longer in the
  source are ignored.
- When the source changed a list's layout (items added or removed, dynamic
  zone components replaced or reordered), the localized list is rebuilt from
  the source items instead of merging translations into unrelated items or
  mixing two components into one.

The create payload merge moves to `buildLocalizedCreateEntry`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ore updating

Dynamic zone and repeatable component items were populated in an
un-awaited `forEach(async …)`, so their relation lookups mutated the update
payload while Strapi was already resolving it, failing updates with
`Document with id "…", locale "null" not found` (a different document on
each run). Items are now awaited in sequence.

json and blocks values are complete values: populate no longer walks into
them as if they were part of the schema (which added stray properties such
as `links.links`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… placements

Runs the plugin's own upload and download transforms for a schema with
json/blocks fields at the top level (side by side), in single, repeatable
and nested components, in dynamic zones and in repeatable components inside
dynamic zone items, and checks create, update, incremental, multi-language,
stale-key and source-structure-change cases against an independent walk
over the schema.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@david-vaclavek
david-vaclavek merged commit e094d23 into main Nov 11, 2026
6 checks passed
@david-vaclavek
david-vaclavek deleted the fix/blocks-json-download-nested-fields branch November 11, 2026 15:23
@localazy-bot localazy-bot Bot mentioned this pull request Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants