Skip to content

Post Translation: New plugin for translating post/page content via GlotPress#602

Open
dd32 wants to merge 16 commits intoWordPress:trunkfrom
dd32:create/claude/post-translation-plugin
Open

Post Translation: New plugin for translating post/page content via GlotPress#602
dd32 wants to merge 16 commits intoWordPress:trunkfrom
dd32:create/claude/post-translation-plugin

Conversation

@dd32
Copy link
Copy Markdown
Member

@dd32 dd32 commented Apr 13, 2026

Summary

  • New wporg-post-translation plugin that translates WordPress post/page content through GlotPress
  • Block parser system extracts translatable strings from Gutenberg blocks (paragraph, heading, image, list, quote, button, shortcode) with a fallback parser for unknown blocks
  • Editor sidebar panel with a toggle to enable translation per-post
  • Admin hook schedules GlotPress import when a translated post is saved
  • Frontend filters (the_content, the_title, render_block) apply translations with transient caching using a compound invalidation key (posts + GlotPress last_changed)
  • Adds $found parameter to GlotPress Translate Bridge for distinguishing untranslated strings
  • gettext filter hooks for future live translator UI integration (informed by the add/translator branch)
  • PHPUnit test suite: 29 unit/integration tests + 6 end-to-end tests through GlotPress
  • wp-env local development environment with GlotPress, in environments/post-translation/

Context

Currently, translatable page content on WordPress.org lives in theme template files (wporg-main-2022) so it can be wrapped in translation functions. This means content changes require theme code changes rather than using the WordPress editor. This plugin enables page content to live in the database as normal posts/pages while still being translatable through GlotPress.

Informed by prior work on the feature/post-translation and add/translator branches, but is a fresh implementation.

Test plan

  • cd environments && npm run post-translation:env start
  • Verify editor sidebar toggle appears for posts/pages
  • Enable translation on a post, save, verify strings appear in GlotPress project
  • Add a translation in GlotPress, verify it renders on the frontend for that locale
  • Run tests: npm run post-translation:env -- run cli --env-cwd=wp-content/plugins/wporg-post-translation phpunit

🤖 Generated with Claude Code

dd32 and others added 3 commits April 13, 2026 16:20
New plugin that translates WordPress post/page content via GlotPress.
Extracts translatable strings from block content, imports them into
GlotPress projects, and applies translations on the frontend.

- Block parser system with specialized parsers for core blocks
  (paragraph, heading, image, list, quote, button, shortcode) and
  a BasicText fallback for unknown blocks
- Editor sidebar panel with toggle to enable translation per-post
- Admin hook schedules GlotPress import on post save
- Frontend filters (the_content, the_title, render_block) apply
  translations with transient caching using compound invalidation key
- Adds $found parameter to GlotPress Translate Bridge for distinguishing
  untranslated strings from identity translations
- PHPUnit test suite (29 tests) with wp-env environment
- gettext filter hooks for future live translator UI integration

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Add GlotPress plugin to the wp-env environment with lifecycle
  scripts that set up permalinks and install GlotPress DB tables
- Fix wp-env plugin paths (relative to environments/ working dir)
- Update bootstrap to load GlotPress with case-insensitive path
  lookup and install its schema for the test database
- Add 6 end-to-end tests covering the full translation pipeline:
  import strings into GlotPress, add translations, verify frontend
  serves translated content, verify multi-post string preservation,
  verify auto-creation of projects with translation sets

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The wp-env container ships PHPUnit 10.5 which is incompatible with the
bundled WordPress test suite (uses removed parseTestMethodAnnotations).
Add a conditional step to downgrade to PHPUnit 9 and use the vendored
binary instead of the system one. Only applies to entries that set
phpunit-bin in the matrix.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props dd32.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

dd32 and others added 12 commits April 13, 2026 17:20
- Fix all PHPCS errors across plugin files (multi-line function calls,
  Yoda conditions, alignment, trait/class-per-file ignores for
  DOMDocument properties and related types)
- Add PHPDoc for $found parameter on GlotPress Translate Bridge
  translate() and translate_plural() methods
- Fix wp-env lifecycle script to use wp-env run cli (runs in container,
  not on host)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Use after-start.sh script for wp-env lifecycle (matching
  plugin-directory pattern) instead of inline wp-env commands
  that fail during the start process
- Exclude */build/* from PHPCS - wp-scripts generated asset files
  are not hand-written PHP

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add missing parameter types to PHPDoc and visibility declarations
to translate() and translate_plural() methods.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…nment.

- Map mocks/mu-plugins for the dev login button on the login screen
- Add wporg-main theme and wporg-parent-2021 parent theme
- Activate wporg-main in after-start script

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
switch_to_blog() and restore_current_blog() do not exist on single-site
WordPress installs. Guard all calls with is_multisite() checks.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ctory.

The mocks/mu-plugins directory includes a loader that requires
wporg-mu-plugins which is not available in this environment.
Map just the dev-login file instead.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Installs Spanish language pack and switches the site language so
the frontend always attempts to translate post content.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
In cron context, get_permalink() resolves to the wrong URL because
the site URL is not available from the request. Pass the permalink
as a cron argument so it uses the correct URL from save time.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Rename GlotPress project hierarchy from disabled/posttranslation to
  post-content (top-level "Post Content" project with site name as
  child project)
- Importer auto-creates parent project if it does not exist
- Child projects use the site name from get_bloginfo() instead of slug
- After-start script creates parent project with Spanish translation
  set, sample page with various block types and translation enabled,
  and triggers cron to import strings into GlotPress
- Update tests for new project path

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
`wp post-translation import` finds all published posts with translation
enabled and imports their strings into GlotPress. Operates per-site
(use --url= on multisite). Supports --post_id, --post_type, and
--dry-run flags.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 8, 2026 04:26
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces a new wporg-post-translation plugin to make WordPress post/page content translatable via GlotPress (string extraction from block content, import into GlotPress, and frontend rendering of translations), plus a local wp-env environment and CI coverage to exercise the pipeline.

Changes:

  • Adds core plugin bootstrap and APIs for enabling per-post translation and resolving GlotPress project paths.
  • Implements block-based string extraction/replacement, GlotPress importing with on-demand project creation, and frontend translation hooks with caching.
  • Adds editor sidebar toggle, WP-CLI import command, PHPUnit coverage (including GlotPress e2e tests), and a dedicated wp-env environment + CI job.

Reviewed changes

Copilot reviewed 27 out of 29 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/wporg-post-translation.php Plugin bootstrap and public helper functions/constants.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/inc/class-admin.php Schedules and runs imports on post save / cron.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/inc/class-frontend.php Applies translations to title/excerpt/content/meta and caches results.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/inc/class-importer.php Imports extracted strings into GlotPress; creates projects/sets as needed.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/inc/class-post-parser.php Block parsing system to extract/replace translatable strings.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/inc/class-editor.php Registers REST-exposed meta and enqueues editor assets.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/inc/class-cli.php WP-CLI command to import strings for enabled posts.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/inc/parsers/interface-block-parser.php Parser interface plus shared DOM/tag utility traits.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/inc/parsers/class-html-parser.php Tag/attribute-based extraction and replacement for known blocks.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/inc/parsers/class-basic-text.php DOM-based fallback extraction for unknown blocks.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/inc/parsers/class-noop.php No-op parser for container/structural blocks.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/inc/parsers/class-shortcode-block.php Shortcode block parser.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/src/editor/index.js Gutenberg document settings panel toggle for enabling translations.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/build/editor/index.js Built editor bundle for the sidebar panel.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/build/editor/index.asset.php Asset metadata for enqueueing the built editor bundle.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/package.json Build/start scripts for editor bundle.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/phpunit/bootstrap.php Test bootstrap (loads plugins, ensures permalinks, installs GP schema).
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/phpunit.xml.dist PHPUnit configuration for the plugin test suite.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/phpunit/tests/plugin.php Unit tests for main plugin helpers/meta registration.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/phpunit/tests/post-parser.php Unit tests for block parsing and translation replacement.
wordpress.org/public_html/wp-content/plugins/wporg-post-translation/phpunit/tests/end-to-end.php GlotPress-backed e2e tests for import/translate/render.
wordpress.org/public_html/wp-content/plugins/glotpress-translate-bridge/glotpress-translate-bridge.php Adds optional by-ref $found to detect missing translations.
phpcs.xml.dist Excludes generated build/ artifacts from PHPCS scanning.
environments/README.md Documents how to start and test the post-translation wp-env environment.
environments/post-translation/.wp-env.json Defines wp-env config including GlotPress + bridge dependency.
environments/post-translation/bin/after-start.sh Post-start provisioning (permalinks, GP tables/project/set, sample page).
environments/package.json Adds post-translation:env script for the new environment.
.github/workflows/unit-tests.yml Adds CI job entry for wporg-post-translation PHPUnit run.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +30 to +40
if ( wp_next_scheduled( 'post_translation_import', $args ) ) {
return;
}

// On WordPress.org multisite, schedule the import on translate.w.org.
if ( defined( 'WPORG_TRANSLATE_BLOGID' ) && is_multisite() ) {
switch_to_blog( WPORG_TRANSLATE_BLOGID );
wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'post_translation_import', $args );
restore_current_blog();
} else {
// Local/dev or single-site: run on the same site.
Comment on lines +183 to +191
protected static function translate_string( string $string, string $project ): string {
if ( '' === $string ) {
return $string;
}

$translated = GlotPress_Translate_Bridge::translate( $string, $project, null, $found );

if ( $found ) {
return apply_filters( 'gettext', $translated, $string, TEXTDOMAIN_PREFIX . $project );
Comment on lines +214 to +225
/**
* Generate a cache key for translated content.
*
* The key includes both the posts last_changed and a GlotPress translations
* last_changed token, so the cache invalidates when either changes.
*/
protected static function cache_key( int $post_id, string $locale ): string {
$posts_changed = wp_cache_get_last_changed( 'posts' );
$gp_changed = wp_cache_get_last_changed( self::CACHE_GROUP );

return sprintf( 'pt_%d_%s_%s_%s', $post_id, $locale, $posts_changed, $gp_changed );
}
* @return string[] Unique translatable strings.
*/
public static function post_to_strings( $post ) {
$post = get_post( $post );
Comment on lines +9 to +16
const postType = useSelect(
( select ) => select( 'core/editor' ).getCurrentPostType(),
[]
);

const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );

const isEnabled = meta?.[ '_post_translation_enabled' ] || false;
Comment on lines +25 to +26
'auth_callback' => function () {
return current_user_can( 'edit_posts' );
Comment on lines +176 to +183
// 3. Verify the bridge can fetch the translation.
// Switch locale to Spanish.
switch_to_locale( 'es_ES' );

$translated = \GlotPress_Translate_Bridge::translate( 'This is a test.', $project, null, $found );

$this->assertTrue( $found, 'Bridge should find the translation.' );
$this->assertEquals( 'Esto es una prueba.', $translated );
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