Skip to content

i18n: Localise fileName and downloadButtonText of the File block#827

Open
bph wants to merge 1 commit into
trunkfrom
fix/i18n-file-block-rich-text
Open

i18n: Localise fileName and downloadButtonText of the File block#827
bph wants to merge 1 commit into
trunkfrom
fix/i18n-file-block-rich-text

Conversation

@bph
Copy link
Copy Markdown
Contributor

@bph bph commented Apr 30, 2026

Summary

  • Adds core/file to get_text_replacement_patterns_for_html() and the dispatch switch in escape_text_content_of_blocks() so the File block's two rich-text-sourced attributes — fileName and downloadButtonText — are wrapped with esc_html_e() on theme save.
  • The block.json declares "selector": "a:not([download])" for fileName and "selector": "a[download]" for downloadButtonText. Two regexes mirror that: a negative-lookahead pattern for the file-name link, and a \sdownload[\s>] pattern for the download button.
  • Refs i18n: Additional blocks not made translation-ready on theme save #825 — second of the two pieces called out there. JSON-attribute additions shipped in i18n: Localize label attribute of additional blocks #826; this is the rich-text path.

Why a separate path

fileName and downloadButtonText are "source": "rich-text", so the values live as inner-HTML text inside the two <a> tags rather than in the block-comment JSON. escape_block_attribute_strings() (the JSON path) can't see them, so they need handling alongside figcaption, alt, and <summary>.

Test plan

  • npm run lint:php clean
  • npm run test:unit:php — 5 new tests pass (file, file without download button, file with custom button text, file with formatted file name, idempotency); 99/99 overall
  • npm run build succeeds
  • Studio smoke through parse_blocks → escape_text_content_of_blocks → serialize_blocks → escape_block_attribute_strings confirms both attributes wrapped with the active theme's text domain
  • Reviewer to spot-check on a theme with a real File block via the Save Theme flow

Notes

  • Behaviour change: themes saved with "Localize Text" enabled will now have File blocks rewritten — they were untouched before. Desired, but worth flagging.
  • The regex assumes the current core HTML shape (boolean download attribute on the second anchor). If a future Gutenberg release restructures the block (e.g. swaps <a download> for <button>), the download-button regex stops matching. Same fragility as every other entry in get_text_replacement_patterns_for_html() — accepted for the band-aid approach tracked in Consider the ideal approach for text localization #776.

@bph bph requested review from MaggieCabrera and mikachan May 7, 2026 10:59
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