Skip to content

Conversation

@scruffian
Copy link
Contributor

@scruffian scruffian commented Nov 28, 2025

Summary

Extends the localization functionality to support text strings stored in block attributes (HTML comments), in addition to the existing HTML markup localization.

Problem

The plugin previously only localized text content within HTML markup (e.g., <p>Text</p>, <h1>Heading</h1>), but many WordPress blocks store translatable text in their attributes within the block comment markup. These strings were not being made translation-ready when saving themes.

Examples of blocks with translatable attributes that were not being localized:

  • Search block: <!-- wp:search {"label":"Search","placeholder":"Type here..."} /-->
  • Pagination blocks: <!-- wp:query-pagination-next {"label":"Next"} /-->
  • Post excerpt: <!-- wp:post-excerpt {"moreText":"— read more"} /-->

Solution

Added attribute-level localization by:

  1. New method get_localizable_block_attributes() - Maps block types to their translatable attribute names
  2. Extended escape_text_content_of_blocks() - Now processes block attributes in addition to HTML content
  3. Uses existing escape_attribute() method - Wraps attribute values with <?php esc_attr_e('...', 'text-domain');?>

Supported Blocks & Attributes

Block Attributes Localized
core/search label, placeholder, buttonText
core/query-pagination-previous label
core/query-pagination-next label
core/comments-pagination-previous label
core/comments-pagination-next label
core/post-navigation-link label
core/post-excerpt moreText

Examples

Search Block

Before:

<!-- wp:search {"label":"Search","placeholder":"Type here...","buttonText":"Search"} /-->

After:

<!-- wp:search {"label":"<?php esc_attr_e('Search', 'theme-domain');?>","placeholder":"<?php esc_attr_e('Type here...', 'theme-domain');?>","buttonText":"<?php esc_attr_e('Search', 'theme-domain');?>"} /-->

Query Pagination

Before:

<!-- wp:query-pagination-previous {"label":"Previous"} /-->
<!-- wp:query-pagination-numbers /-->
<!-- wp:query-pagination-next {"label":"Next"} /-->

After:

<!-- wp:query-pagination-previous {"label":"<?php esc_attr_e('Previous', 'theme-domain');?>"} /-->
<!-- wp:query-pagination-numbers /-->
<!-- wp:query-pagination-next {"label":"<?php esc_attr_e('Next', 'theme-domain');?>"} /-->

Comments Pagination with Unicode Characters

Before:

<!-- wp:comments-pagination-next {"label":"⪻ newer"} /-->
<!-- wp:comments-pagination-previous {"label":"older ⪼"} /-->

After:

<!-- wp:comments-pagination-next {"label":"<?php esc_attr_e('⪻ newer', 'theme-domain');?>"} /-->
<!-- wp:comments-pagination-previous {"label":"<?php esc_attr_e('older ⪼', 'theme-domain');?>"} /-->

Post Excerpt

Before:

<!-- wp:post-excerpt {"moreText":"— read more","showMoreOnNewLine":false} /-->

After:

<!-- wp:post-excerpt {"moreText":"<?php esc_attr_e('— read more', 'theme-domain');?>","showMoreOnNewLine":false} /-->

Technical Details

Files Changed

  • includes/create-theme/theme-locale.php: Added attribute localization logic
  • tests/CbtThemeLocale/escapeBlockAttributes.php: Comprehensive test suite for new functionality

Implementation Notes

  • Follows existing patterns and coding standards
  • Uses the same escape_attribute() method already used for HTML attributes like alt=""
  • Processes attributes after HTML content to maintain separation of concerns
  • Only processes string-type attributes (skips booleans, numbers, etc.)
  • Gracefully handles blocks without attributes or unsupported block types

Testing

  • ✅ Added comprehensive PHPUnit tests covering all supported blocks
  • ✅ Tests verify correct escaping and preservation of non-localizable attributes
  • ✅ Manual testing confirms attributes are properly localized when themes are saved
  • ✅ PHP syntax validation passes

Related Issues

Fixes user-reported issues where pagination labels, search block text, and post excerpt "read more" text were not being made translation-ready.


🤖 Generated with https://claude.com/claude-code

Co-Authored-By: Claude noreply@anthropic.com

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