379 | | * @param GP_Project $gp_project The GlotPress project. |
380 | | * @param GP_Locale $gp_locale The GlotPress locale. |
381 | | * @param GP_Translation_Set $set The translation set. |
382 | | * @param string $dest Destination file name. |
383 | | * @return string|WP_Error Last updated date on success, WP_Error on failure. |
| 380 | * @param Translation_Entry[] $entries The translation entries to map. |
| 381 | * |
| 382 | * @return array The mapping of sources to translation entries. |
| 383 | */ |
| 384 | private function build_mapping( $entries ) { |
| 385 | $mapping = array(); |
| 386 | foreach ( $entries as $entry ) { |
| 387 | /** @var Translation_Entry $entry */ |
| 388 | |
| 389 | // Find all unique sources this translation originates from. |
| 390 | $sources = array_map( function ( $reference ) { |
| 391 | $parts = explode( ':', $reference ); |
| 392 | $file = $parts[0]; |
| 393 | |
| 394 | if ( substr( $file, -3 ) === '.js' ) { |
| 395 | return $file; |
| 396 | } |
| 397 | return 'po'; |
| 398 | }, $entry->references ); |
| 399 | $sources = array_unique( $sources ); |
| 400 | |
| 401 | foreach ( $sources as $source ) { |
| 402 | $mapping[ $source ][] = $entry; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | return $mapping; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Builds a mapping of JS file names to translation entries. |
| 411 | * |
| 412 | * @param GP_Project $gp_project The GlotPress project. |
| 413 | * @param GP_Locale $gp_locale The GlotPress locale. |
| 414 | * @param GP_Translation_Set $set The translation set. |
| 415 | * @param array $mapping A mapping of files to translation entries. |
| 416 | * @param string $base_dest Destination file name. |
| 417 | * @return array An array of translation files built, may be empty if no translations in JS files exist. |
385 | | private function build_po_file( $gp_project, $gp_locale, $set, $dest ) { |
386 | | $entries = GP::$translation->for_export( $gp_project, $set, [ 'status' => 'current' ] ); |
387 | | if ( ! $entries ) { |
388 | | return new WP_Error( 'no_translations', 'No current translations available.' ); |
| 419 | private function build_json_files( $gp_project, $gp_locale, $set, $mapping, $base_dest ) { |
| 420 | // Export translations for each JS file to a separate translation file. |
| 421 | $files = array(); |
| 422 | $format = gp_array_get( GP::$formats, 'jed1x' ); |
| 423 | foreach ( $mapping as $file => $entries ) { |
| 424 | $json_content = $format->print_exported_file( $gp_project, $gp_locale, $set, $entries ); |
| 425 | |
| 426 | $hash = md5( $file ); |
| 427 | $dest = "{$base_dest}-{$hash}.json"; |
| 428 | |
| 429 | file_put_contents( $dest, $json_content ); |
| 430 | |
| 431 | $files[] = $dest; |
| 434 | return $files; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Builds a PO file for translations. |
| 439 | * |
| 440 | * @param GP_Project $gp_project The GlotPress project. |
| 441 | * @param GP_Locale $gp_locale The GlotPress locale. |
| 442 | * @param GP_Translation_Set $set The translation set. |
| 443 | * @param Translation_Entry[] $entries The translation entries. |
| 444 | * @param string $dest Destination file name. |
| 445 | * @return string|WP_Error Last updated date on success, WP_Error on failure. |
| 446 | */ |
| 447 | private function build_po_file( $gp_project, $gp_locale, $set, $entries, $dest ) { |
| 592 | $entries = GP::$translation->for_export( $data->gp_project, $set, [ 'status' => 'current' ] ); |
| 593 | if ( ! $entries ) { |
| 594 | WP_CLI::warning( "No current translations available for {$wp_locale}." ); |
| 595 | |
| 596 | continue; |
| 597 | } |
| 598 | |
| 599 | // Build a mapping based on where the translation entries occur and separate the po entries. |
| 600 | $mapping = $this->build_mapping( $entries ); |
| 601 | $po_entries = array_key_exists( 'po', $mapping ) ? $mapping['po'] : array(); |
| 602 | |
| 603 | unset( $mapping['po'] ); |
| 604 | |
| 605 | // Create JED json files for each JS file. |
| 606 | $json_files = $this->build_json_files( $data->gp_project, $gp_locale, $set, $mapping, $json_file_base ); |
| 607 | |