Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Include file reference in JSON translation files #178

Merged
merged 1 commit into from
Jul 20, 2020
Merged
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
5 changes: 5 additions & 0 deletions inc/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
35 changes: 35 additions & 0 deletions tests/phpunit/tests/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}