Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-includes/l10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
Expand Down
9 changes: 8 additions & 1 deletion src/wp-includes/l10n/class-wp-translation-file-mo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
12 changes: 11 additions & 1 deletion src/wp-includes/l10n/class-wp-translation-file-php.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<?php' . PHP_EOL . 'return ' . $this->var_export( $data ) . ';' . PHP_EOL;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/data/languages/de_CH.l10n.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?php
return ['po-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']];
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']];
50 changes: 50 additions & 0 deletions tests/phpunit/tests/l10n/wpTranslationsConvert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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
*/
Expand Down