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.' ); |
| 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, -7 ) === '.min.js' ) { |
| 395 | return substr( $file, 0, -7 ) . '.js'; |
| 396 | } |
| 397 | |
| 398 | if ( substr( $file, -3 ) === '.js' ) { |
| 399 | return $file; |
| 400 | } |
| 401 | return 'po'; |
| 402 | }, $entry->references ); |
| 403 | $sources = array_unique( $sources ); |
| 404 | |
| 405 | foreach ( $sources as $source ) { |
| 406 | $mapping[ $source ][] = $entry; |
| 407 | } |
| 410 | return $mapping; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Builds a mapping of JS file names to translation entries. |
| 415 | * |
| 416 | * @param GP_Project $gp_project The GlotPress project. |
| 417 | * @param GP_Locale $gp_locale The GlotPress locale. |
| 418 | * @param GP_Translation_Set $set The translation set. |
| 419 | * @param array $mapping A mapping of files to translation entries. |
| 420 | * @param string $base_dest Destination file name. |
| 421 | * @return array An array of translation files built, may be empty if no translations in JS files exist. |
| 422 | */ |
| 423 | private function build_json_files( $gp_project, $gp_locale, $set, $mapping, $base_dest ) { |
| 424 | // Export translations for each JS file to a separate translation file. |
| 425 | $files = array(); |
| 426 | $format = gp_array_get( GP::$formats, 'jed1x' ); |
| 427 | foreach ( $mapping as $file => $entries ) { |
| 428 | $json_content = $format->print_exported_file( $gp_project, $gp_locale, $set, $entries ); |
| 429 | |
| 430 | $hash = md5( $file ); |
| 431 | $dest = "{$base_dest}-{$hash}.json"; |
| 432 | |
| 433 | file_put_contents( $dest, $json_content ); |
| 434 | |
| 435 | $files[] = $dest; |
| 436 | } |
| 437 | |
| 438 | return $files; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Builds a PO file for translations. |
| 443 | * |
| 444 | * @param GP_Project $gp_project The GlotPress project. |
| 445 | * @param GP_Locale $gp_locale The GlotPress locale. |
| 446 | * @param GP_Translation_Set $set The translation set. |
| 447 | * @param Translation_Entry[] $entries The translation entries. |
| 448 | * @param string $dest Destination file name. |
| 449 | * @return string|WP_Error Last updated date on success, WP_Error on failure. |
| 450 | */ |
| 451 | private function build_po_file( $gp_project, $gp_locale, $set, $entries, $dest ) { |
| 596 | $entries = GP::$translation->for_export( $data->gp_project, $set, [ 'status' => 'current' ] ); |
| 597 | if ( ! $entries ) { |
| 598 | WP_CLI::warning( "No current translations available for {$wp_locale}." ); |
| 599 | |
| 600 | continue; |
| 601 | } |
| 602 | |
| 603 | // Build a mapping based on where the translation entries occur and separate the po entries. |
| 604 | $mapping = $this->build_mapping( $entries ); |
| 605 | $po_entries = array_key_exists( 'po', $mapping ) ? $mapping['po'] : array(); |
| 606 | |
| 607 | unset( $mapping['po'] ); |
| 608 | |
| 609 | // Create JED json files for each JS file. |
| 610 | $json_files = $this->build_json_files( $data->gp_project, $gp_locale, $set, $mapping, $json_file_base ); |
| 611 | |