| 65 | * Generates a language pack for core. |
| 66 | * |
| 67 | * Examples: |
| 68 | * wp @translate wporg-translate language-pack generate core dev --version=5.0-beta |
| 69 | * wp @translate wporg-translate language-pack generate core 4.9.x --locale=de --version=4.9.8 |
| 70 | * |
| 71 | * @param string $slug Slug of the core version. |
| 72 | * @param array $args Extra arguments. |
| 73 | */ |
| 74 | private function generate_core( $slug, $args ) { |
| 75 | $projects = [ |
| 76 | "wp/$slug" => 'default', |
| 77 | "wp/$slug/admin" => 'admin', |
| 78 | "wp/$slug/admin/network" => 'admin-network', |
| 79 | "wp/$slug/cc" => 'continents-cities', |
| 80 | ]; |
| 81 | |
| 82 | $version = $args['version']; |
| 83 | if ( ! $version ) { |
| 84 | WP_CLI::error( 'Missing version.' ); |
| 85 | } |
| 86 | |
| 87 | foreach ( $projects as $path => $domain ) { |
| 88 | $gp_project = GP::$project->by_path( $path ); |
| 89 | if ( ! $gp_project ) { |
| 90 | WP_CLI::error( "Invalid core path: $path." ); |
| 91 | } |
| 92 | |
| 93 | $translation_sets = GP::$translation_set->by_project_id( $gp_project->id ); |
| 94 | if ( ! $translation_sets ) { |
| 95 | WP_CLI::error( 'No translation sets available.' ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Filters the arguments passed to the WP-CLI command. |
| 100 | * |
| 101 | * @param array $args CLI arguments. |
| 102 | * @param string $path Path of the GP Project. |
| 103 | */ |
| 104 | $args = apply_filters( 'wporg_translate_language_pack_core_args', $args, $path ); |
| 105 | |
| 106 | if ( $args['locale'] ) { |
| 107 | $translation_sets = wp_list_filter( $translation_sets, [ |
| 108 | 'locale' => $args['locale'], |
| 109 | 'slug' => $args['locale-slug'], |
| 110 | ] ); |
| 111 | } |
| 112 | |
| 113 | if ( 'continents-cities' === $domain ) { |
| 114 | $translation_sets = array_filter( $translation_sets, function ( $set ) { |
| 115 | return substr( $set->locale, 0, 3 ) !== 'en_'; |
| 116 | } ); |
| 117 | } |
| 118 | |
| 119 | $svn_command = $this->get_svn_command(); |
| 120 | $svn_checkout = self::get_temp_directory( $slug ); |
| 121 | |
| 122 | $result = $this->execute_command( sprintf( |
| 123 | '%s checkout --quiet --depth=empty %s %s 2>&1', |
| 124 | $svn_command, |
| 125 | escapeshellarg( self::SVN_URL . '/core' ), |
| 126 | escapeshellarg( $svn_checkout ) |
| 127 | ) ); |
| 128 | |
| 129 | if ( is_wp_error( $result ) ) { |
| 130 | WP_CLI::error_multi_line( $result->get_error_data() ); |
| 131 | WP_CLI::error( 'SVN export failed.' ); |
| 132 | } |
| 133 | |
| 134 | $data = new stdClass(); |
| 135 | $data->type = 'core'; |
| 136 | $data->domain = 'default'; |
| 137 | $data->version = $args['version']; |
| 138 | $data->translation_sets = $translation_sets; |
| 139 | $data->gp_project = $gp_project; |
| 140 | $data->svn_checkout = $svn_checkout; |
| 141 | $this->build_language_packs( $data ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 461 | * Build a mapping of JS files to translation entries occurring in those files. |
| 462 | * Translation entries occurring in other files are added to the 'po' key. |
| 463 | * |
| 464 | * @param Translation_Entry[] $entries The translation entries to map. |
| 465 | * |
| 466 | * @return array The mapping of sources to translation entries. |
| 467 | */ |
| 468 | private function build_mapping( $entries ) { |
| 469 | $mapping = array(); |
| 470 | foreach ( $entries as $entry ) { |
| 471 | /** @var Translation_Entry $entry */ |
| 472 | |
| 473 | // Find all unique sources this translation originates from. |
| 474 | $sources = array_map( function ( $reference ) { |
| 475 | $parts = explode( ':', $reference ); |
| 476 | $file = $parts[0]; |
| 477 | |
| 478 | if ( substr( $file, -7 ) === '.min.js' ) { |
| 479 | return substr( $file, 0, -7 ) . '.js'; |
| 480 | } |
| 481 | |
| 482 | if ( substr( $file, -3 ) === '.js' ) { |
| 483 | return $file; |
| 484 | } |
| 485 | return 'po'; |
| 486 | }, $entry->references ); |
| 487 | // Always add all entries to the PO file. |
| 488 | $sources[] = 'po'; |
| 489 | $sources = array_unique( $sources ); |
| 490 | |
| 491 | foreach ( $sources as $source ) { |
| 492 | $mapping[ $source ][] = $entry; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return $mapping; |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Builds a mapping of JS file names to translation entries. |
| 501 | * |
| 502 | * @param GP_Project $gp_project The GlotPress project. |
| 503 | * @param GP_Locale $gp_locale The GlotPress locale. |
| 504 | * @param GP_Translation_Set $set The translation set. |
| 505 | * @param array $mapping A mapping of files to translation entries. |
| 506 | * @param string $base_dest Destination file name. |
| 507 | * @return array An array of translation files built, may be empty if no translations in JS files exist. |
| 508 | */ |
| 509 | private function build_json_files( $gp_project, $gp_locale, $set, $mapping, $base_dest ) { |
| 510 | // Export translations for each JS file to a separate translation file. |
| 511 | $files = array(); |
| 512 | $format = gp_array_get( GP::$formats, 'jed1x' ); |
| 513 | foreach ( $mapping as $file => $entries ) { |
| 514 | $json_content = $format->print_exported_file( $gp_project, $gp_locale, $set, $entries ); |
| 515 | |
| 516 | $hash = md5( $file ); |
| 517 | $dest = "{$base_dest}-{$hash}.json"; |
| 518 | |
| 519 | file_put_contents( $dest, $json_content ); |
| 520 | |
| 521 | $files[] = $dest; |
| 522 | } |
| 523 | |
| 524 | return $files; |
| 525 | } |
| 526 | |
| 527 | /** |
523 | | $export_directory = "{$data->svn_checkout}/{$data->domain}/{$data->version}/{$wp_locale}"; |
524 | | $build_directory = self::BUILD_DIR . "/{$data->type}s/{$data->domain}/{$data->version}"; |
525 | | $filename = "{$data->domain}-{$wp_locale}"; |
526 | | $po_file = "{$export_directory}/{$filename}.po"; |
527 | | $mo_file = "{$export_directory}/{$filename}.mo"; |
528 | | $zip_file = "{$export_directory}/{$filename}.zip"; |
529 | | $build_zip_file = "{$build_directory}/{$wp_locale}.zip"; |
| 670 | $working_directory = 'core' === $data->type ? $data->svn_checkout : "{$data->svn_checkout}/{$data->domain}"; |
| 671 | $export_directory = "{$working_directory}/{$data->version}/{$wp_locale}"; |
| 672 | $build_directory = 'core' === $data->type |
| 673 | ? self::BUILD_DIR . "/core/{$data->version}" |
| 674 | : self::BUILD_DIR . "/{$data->type}s/{$data->domain}/{$data->version}"; |
| 686 | $entries = GP::$translation->for_export( $data->gp_project, $set, [ 'status' => 'current' ] ); |
| 687 | if ( ! $entries ) { |
| 688 | WP_CLI::warning( "No current translations available for {$wp_locale}." ); |
| 689 | |
| 690 | continue; |
| 691 | } |
| 692 | |
| 693 | // Build a mapping based on where the translation entries occur and separate the po entries. |
| 694 | $mapping = $this->build_mapping( $entries ); |
| 695 | $po_entries = array_key_exists( 'po', $mapping ) ? $mapping['po'] : array(); |
| 696 | |
| 697 | unset( $mapping['po'] ); |
| 698 | |
| 699 | // Create JED json files for each JS file. |
| 700 | $json_files = $this->build_json_files( $data->gp_project, $gp_locale, $set, $mapping, $json_file_base ); |
| 701 | |