8000 feat(i18n): images and videos can be replaced per language by Klehm · Pull Request #62 · Klehm/content-blocks-project · GitHub
[go: up one dir, main page]

Skip to content

feat(i18n): images and videos can be replaced per language - #62

Merged
Klehm merged 1 commit into
masterfrom
feat/i18n-localized-media
Sep 29, 2026
Merged

Klehm merged 1 commit into
masterfrom
feat/i18n-localized-media

Conversation

@Klehm
@Klehm Klehm commented Sep 29, 2026
Copy link
Copy Markdown
Owner

Why

We were asked how to change an image or a video depending on the language. Until now the answer was that you could not: a file was shared by every language. Only its alt text, caption and link were translated, and for a video, its captions file. A picture with words in it stayed in the source language.

What changes

Kit. The file fields are now tagged cb_translatable:

  • src on image, gallery, card and video;
  • poster on video.

Without klehm/content-blocks-i18n installed, nothing changes.

i18n. A translated path already went through storage, rendering (cb_image(), so srcset and WebP still work), the asset sweep (TranslationAssetReferenceProvider) and export/import (AssetTokenizer/AssetRewriter). What did not fit was the behaviour built for text. File fields are recognised by their widget, image or video, found by walking up the upload type's parent chain. For those fields:

Text field File field
No value missing, counts against progress shared (the source's file), left out of progress
Machine translation sent to the provider never sent, and not counted as skipped
Value checked by the writer no scheme-less path or http(s) URL, else invalid_media
"" stores a deliberate blank clears back to the source's file
Workbench row text box, → ⚡ ✓ ⨯ both files, upload button + drop zone, ✓ ⨯
  • FieldStatus gains no case. It is frozen API and host code may match on it. Shared is only how a media row displays missing, in the template and in workbench.js.
  • Uploads use the core's content_blocks_upload. The size and MIME limits and the canEdit() check on the area apply. The workbench already held the content_blocks CSRF token. The path is saved immediately, with no debounce, and the preview refreshes that one block.
  • Outdated. A localized file becomes outdated when the source file changes, and ✓ re-validates it like text.

No migration, and stored block data is unchanged. scripts/bc-check/run.sh reports no backwards-incompatible change.

Docs

  • docs/guide/translation.md: the "What this package does not do" bullet is updated, and a new section Localized images and videos explains how to tag a file field in a custom block.
  • docs/internals/i18n.md#localized-media: why each rule is what it is.
  • The kit's image and video reference pages (blocks-meta.mjs, regenerated), CHANGELOGs for i18n and kit, and CLAUDE.md.

Tests

  • PHPUnit, i18n: image/video detection through the parent chain; the writer stores a valid path, refuses javascript:, a tab-split java\tscript:, data: and a newline, and clears on blank; shared media is out of progress and outdated media still counts; machine translation sends neither the image nor the video. 191 tests pass.
  • PHPUnit, kit: the tagging test now covers image, video, the gallery item and the card item. 339 pass.
  • Vitest, i18n: picking a file uploads it with the area and the CSRF token, then saves the path; a refused upload shows the server's reason and saves nothing; only a file drag is taken by the drop zone; ⨯ makes the row shared again and removes it from the counters. 34 pass.
  • Playwright: new spec an image is replaced for one language and served in its locale. It uploads an image in the builder, then an EN image in the workbench; the preview switches; after Publish, /en/page/{id} serves the EN file and /page/{id} the source file. Main suite 199 passed, Encore 7 passed.
  • PHPStan and php-cs-fixer are clean, and core PHPUnit passes (1058).

Not in this PR

  • There is no config switch to hide media rows. Shared rows are optional and do not count toward progress, so one did not seem needed; say so if a host wants one.
  • A section's background image stays shared, because it belongs to the styling, which is common to all languages.

🤖 Generated with Claude Code

The kit tags the file fields (`src` on image, gallery, card and video, and
the video's `poster`) with `cb_translatable`. Storage, render, the asset
sweep and export already carried a translated path; what changes is the
text semantics around it, decided on the field's widget (`image` / `video`,
read from the upload type's parent chain):

- a file with no localized version is shared, not missing, and is left out
  of TranslationProgress (FieldStatus gains no case: it is frozen API);
- machine translation never sends a file path to a provider;
- the writer accepts a scheme-less path or an http(s) URL only
  (`invalid_media`), and a blank value clears;
- the workbench shows both files, with an upload button and a drop zone
  that post to the core's upload endpoint, then save at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Klehm
Klehm merged commit 225b0cc into master Sep 29, 2026
36 checks passed
@Klehm
Klehm deleted the feat/i18n-localized-media branch September 29, 2026 08:47
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.

1 participant

0