From b463fc0cb55f5b6cbb9352d3152180606184bbbe Mon Sep 17 00:00:00 2001 From: Dominik Schilling Date: Sun, 19 Jul 2020 18:09:56 +0200 Subject: [PATCH] Include file reference in JSON translation files. --- inc/Export.php | 5 +++++ tests/phpunit/tests/Export.php | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/inc/Export.php b/inc/Export.php index 3f23d871..236cbb17 100644 --- a/inc/Export.php +++ b/inc/Export.php @@ -204,6 +204,11 @@ protected function build_json_files( $mapping ): void { foreach ( $mapping as $file => $entries ) { $contents = $format->print_exported_file( $this->project->get_project(), $this->locale, $this->translation_set, $entries ); + // Add comment with file reference for debugging. + $contents_decoded = json_decode( $contents ); + $contents_decoded->comment = [ 'reference' => $file ]; + $contents = wp_json_encode( $contents_decoded ); + $hash = md5( $file ); $file_name = "{$base_file_name}-{$hash}.json"; $temp_file = wp_tempnam( $file_name ); diff --git a/tests/phpunit/tests/Export.php b/tests/phpunit/tests/Export.php index fd01fcd4..19b741d1 100644 --- a/tests/phpunit/tests/Export.php +++ b/tests/phpunit/tests/Export.php @@ -294,4 +294,39 @@ public function test_js_entries_are_not_in_po_file(): void { $this->assertArrayHasKey( $original_2->singular, $json_2['locale_data']['messages'] ); $this->assertArrayHasKey( $original_3->singular, $translations->entries ); } + + public function test_json_files_include_file_reference_comment(): void { + $filename_1 = 'my-super-script'; + + /* @var \GP_Original $original_1 */ + $original_1 = $this->factory->original->create( + [ + 'project_id' => $this->translation_set->project_id, + 'references' => $filename_1 . '.js', + ] + ); + + $this->factory->translation->create( + [ + 'original_id' => $original_1->id, + 'translation_set_id' => $this->translation_set->id, + 'status' => 'current', + ] + ); + + $export = new E( $this->translation_set ); + + $actual = $export->export_strings(); + + $json_filename_1 = 'foo-project-de_DE-' . md5( $filename_1 . '.js' ) . '.json'; + + $json_1 = file_get_contents( $actual[ $json_filename_1 ] ); + + array_map( 'unlink', $actual ); + + $this->assertJson( $json_1 ); + + $json1_encoded = json_decode( $json_1 ); + $this->assertSame( $filename_1 . '.js', $json1_encoded->comment->reference ); + } }