From c75063f0eeb823e0792c8df60e09163b1aa62963 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 14:37:32 +0000 Subject: [PATCH] I18N: Use `translation-revision-date` header for PHP translation files. GlotPress generates PHP translation files with a `translation-revision-date` header instead of the `po-revision-date` header used in PO and MO files. `wp_get_l10n_php_file_data()` now reads the revision date from that header, and `WP_Translation_File::transform()` renames the header accordingly when converting between MO and PHP files. See #65809. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5mDonfFaS5ooNyetmH --- src/wp-includes/l10n.php | 2 +- .../l10n/class-wp-translation-file-mo.php | 9 +++- .../l10n/class-wp-translation-file-php.php | 12 ++++- tests/phpunit/data/languages/de_CH.l10n.php | 2 +- .../tests/l10n/wpTranslationsConvert.php | 50 +++++++++++++++++++ 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index e90f97fd2025a..3394c32e83367 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -1830,7 +1830,7 @@ function wp_get_l10n_php_file_data( $php_file ) { unset( $data['messages'] ); $headers = array( 'POT-Creation-Date' => 'pot-creation-date', - 'PO-Revision-Date' => 'po-revision-date', + 'PO-Revision-Date' => 'translation-revision-date', 'Project-Id-Version' => 'project-id-version', 'X-Generator' => 'x-generator', ); diff --git a/src/wp-includes/l10n/class-wp-translation-file-mo.php b/src/wp-includes/l10n/class-wp-translation-file-mo.php index 36d782cd5151d..2fdb4e25fc32d 100644 --- a/src/wp-includes/l10n/class-wp-translation-file-mo.php +++ b/src/wp-includes/l10n/class-wp-translation-file-mo.php @@ -186,9 +186,16 @@ protected function parse_file(): bool { * @return string Translation file contents. */ public function export(): string { + // PHP translation files use 'translation-revision-date' instead of 'po-revision-date'. + $headers = $this->headers; + if ( isset( $headers['translation-revision-date'] ) ) { + $headers['po-revision-date'] = $headers['translation-revision-date']; + unset( $headers['translation-revision-date'] ); + } + // Prefix the headers as the first key. $headers_string = ''; - foreach ( $this->headers as $header => $value ) { + foreach ( $headers as $header => $value ) { $headers_string .= "{$header}: $value\n"; } $entries = array_merge( array( '' => $headers_string ), $this->entries ); diff --git a/src/wp-includes/l10n/class-wp-translation-file-php.php b/src/wp-includes/l10n/class-wp-translation-file-php.php index f93dd0163fddc..a4e29d402a944 100644 --- a/src/wp-includes/l10n/class-wp-translation-file-php.php +++ b/src/wp-includes/l10n/class-wp-translation-file-php.php @@ -45,7 +45,17 @@ protected function parse_file() { * @return string Translation file contents. */ public function export(): string { - $data = array_merge( $this->headers, array( 'messages' => $this->entries ) ); + /* + * PHP translation files use the 'translation-revision-date' header (as generated by GlotPress) + * instead of the 'po-revision-date' header used in PO and MO files. + */ + $headers = $this->headers; + if ( isset( $headers['po-revision-date'] ) ) { + $headers['translation-revision-date'] = $headers['po-revision-date']; + unset( $headers['po-revision-date'] ); + } + + $data = array_merge( $headers, array( 'messages' => $this->entries ) ); return 'var_export( $data ) . ';' . PHP_EOL; } diff --git a/tests/phpunit/data/languages/de_CH.l10n.php b/tests/phpunit/data/languages/de_CH.l10n.php index 1e5285fdf457f..4575dc50a0a32 100644 --- a/tests/phpunit/data/languages/de_CH.l10n.php +++ b/tests/phpunit/data/languages/de_CH.l10n.php @@ -1,2 +1,2 @@ '2024-01-31 19:08:22+0000','mime-version'=>'1.0','content-type'=>'text/plain; charset=UTF-8','content-transfer-encoding'=>'8bit','plural-forms'=>'nplurals=2; plural=n != 1;','x-generator'=>'GlotPress/4.0.0-beta.2','language'=>'de','project-id-version'=>'WordPress - 6.4.x - Development','messages'=>['html_lang_attribute'=>'de-CH','text directionltr'=>'ltr','number_format_decimal_point'=>',','number_format_thousands_sep'=>'.','Update %s now'=>'Jetzt %s aktualisieren','[%1$s] Confirm Action: %2$s'=>'[%1$s] Aktion bestätigen: %2$s','[%s] Erasure Request Fulfilled'=>'[%s] Löschauftrag ausgeführt','[%s] Personal Data Export'=>'[%s] Export personenbezogener Daten']]; +return ['translation-revision-date'=>'2024-01-31 19:08:22+0000','mime-version'=>'1.0','content-type'=>'text/plain; charset=UTF-8','content-transfer-encoding'=>'8bit','plural-forms'=>'nplurals=2; plural=n != 1;','x-generator'=>'GlotPress/4.0.0-beta.2','language'=>'de','project-id-version'=>'WordPress - 6.4.x - Development','messages'=>['html_lang_attribute'=>'de-CH','text directionltr'=>'ltr','number_format_decimal_point'=>',','number_format_thousands_sep'=>'.','Update %s now'=>'Jetzt %s aktualisieren','[%1$s] Confirm Action: %2$s'=>'[%1$s] Aktion bestätigen: %2$s','[%s] Erasure Request Fulfilled'=>'[%s] Löschauftrag ausgeführt','[%s] Personal Data Export'=>'[%s] Export personenbezogener Daten']]; diff --git a/tests/phpunit/tests/l10n/wpTranslationsConvert.php b/tests/phpunit/tests/l10n/wpTranslationsConvert.php index 90c2ae7b4975d..aca758a250b81 100644 --- a/tests/phpunit/tests/l10n/wpTranslationsConvert.php +++ b/tests/phpunit/tests/l10n/wpTranslationsConvert.php @@ -511,6 +511,12 @@ public function test_convert_format( string $source_file, string $destination_fo $source_headers = $source->headers(); $destination_headers = $destination_read->headers(); + // PHP translation files use 'translation-revision-date' instead of 'po-revision-date'. + if ( 'php' === $destination_format && isset( $source_headers['po-revision-date'] ) ) { + $source_headers['translation-revision-date'] = $source_headers['po-revision-date']; + unset( $source_headers['po-revision-date'] ); + } + $this->assertSame( $source_headers, $destination_headers ); foreach ( $source->entries() as $original => $translation ) { @@ -537,6 +543,50 @@ public static function data_export_matrix(): array { return $matrix; } + /** + * @ticket 65809 + * + * @covers WP_Translation_File::transform + * @covers WP_Translation_File_PHP::export + */ + public function test_transform_mo_to_php_uses_translation_revision_date_header() { + $source_file = DIR_TESTDATA . '/l10n/example-simple.mo'; + + $contents = WP_Translation_File::transform( $source_file, 'php' ); + $this->assertIsString( $contents ); + + $destination_file = $this->temp_filename(); + $this->assertNotFalse( $destination_file ); + file_put_contents( $destination_file, $contents ); + + $data = include $destination_file; + + $this->assertSame( '2016-01-05 18:45:32+1000', $data['translation-revision-date'] ); + $this->assertArrayNotHasKey( 'po-revision-date', $data ); + + $this->assertSame( '2016-01-05 18:45:32+1000', wp_get_l10n_php_file_data( $destination_file )['PO-Revision-Date'] ); + } + + /** + * @ticket 65809 + * + * @covers WP_Translation_File::transform + * @covers WP_Translation_File_MO::export + */ + public function test_transform_php_to_mo_uses_po_revision_date_header() { + $contents = WP_Translation_File::transform( DIR_TESTDATA . '/languages/de_CH.l10n.php', 'mo' ); + $this->assertIsString( $contents ); + + $destination_file = $this->temp_filename(); + $this->assertNotFalse( $destination_file ); + file_put_contents( $destination_file, $contents ); + + $headers = WP_Translation_File::create( $destination_file, 'mo' )->headers(); + + $this->assertSame( '2024-01-31 19:08:22+0000', $headers['po-revision-date'] ); + $this->assertArrayNotHasKey( 'translation-revision-date', $headers ); + } + /** * @covers WP_Translation_File::transform */