Making WordPress.org

Ticket #3876: 3876.diff

File 3876.diff, 5.2 KB (added by herregroen, 6 years ago)
  • wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-language-pack.php

    diff --git wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-language-pack.php wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-language-pack.php
    index 0252d9fe7..2567246b5 100644
    class Language_Pack extends WP_CLI_Command { 
    374374        }
    375375
    376376        /**
    377          * Builds a PO file for translations.
     377         * Builds a mapping of JS file names to translation entries.
    378378         *
    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.
     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 Translation_Entry[] $entries    The translation entries.
     383         * @param string              $dest       Destination file name.
     384         * @return array An array of translation files built, may be empty if no translations in JS files exist.
    384385         */
    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.' );
     386        private function build_json_files(  $gp_project, $gp_locale, $set, $entries, $base_dest ) {
     387                // Build a mapping of JS files to translation entries occuring in those files.
     388                $mapping = array();
     389                foreach ( $entries as $entry ) {
     390                        /** @var Translation_Entry $entry */
     391                        foreach ( $entry->references as $reference ) {
     392                                // Split the references to get the filename. References are in relative_to_root_path/file.exit:linenum form.
     393                                $parts = explode( ':', $reference );
     394                                $file  = $parts[0];
     395
     396                                if ( substr( $file, -3 ) === '.js' ) {
     397                                        $mapping[ $file ][] = $entry;
     398                                }
     399                        }
    389400                }
    390401
     402                // Export translations for each JS file to a separate translation file.
     403                $files  = array();
     404                $format = gp_array_get( GP::$formats, 'json' );
     405                foreach ( $mapping as $file => $file_entries ) {
     406                        $json_content = $format->print_exported_file( $gp_project, $gp_locale, $set, $file_entries );
     407
     408                        $hash = md5( $file );
     409                        $dest = "{$base_dest}-{$hash}.json";
     410
     411                        file_put_contents( $dest, $json_content );
     412
     413                        $files[] = $dest;
     414                }
     415
     416                return $files;
     417        }
     418
     419        /**
     420         * Builds a PO file for translations.
     421         *
     422         * @param GP_Project          $gp_project The GlotPress project.
     423         * @param GP_Locale           $gp_locale  The GlotPress locale.
     424         * @param GP_Translation_Set  $set        The translation set.
     425         * @param Translation_Entry[] $entries    The translation entries.
     426         * @param string              $dest       Destination file name.
     427         * @return string|WP_Error Last updated date on success, WP_Error on failure.
     428         */
     429        private function build_po_file( $gp_project, $gp_locale, $set, $entries, $dest ) {
    391430                $format     = gp_array_get( GP::$formats, 'po' );
    392431                $po_content = $format->print_exported_file( $gp_project, $gp_locale, $set, $entries );
    393432
    class Language_Pack extends WP_CLI_Command { 
    523562                        $export_directory = "{$data->svn_checkout}/{$data->domain}/{$data->version}/{$wp_locale}";
    524563                        $build_directory  = self::BUILD_DIR . "/{$data->type}s/{$data->domain}/{$data->version}";
    525564                        $filename         = "{$data->domain}-{$wp_locale}";
     565                        $json_file_base   = "{$export_directory}/{$filename}";
    526566                        $po_file          = "{$export_directory}/{$filename}.po";
    527567                        $mo_file          = "{$export_directory}/{$filename}.mo";
    528568                        $zip_file         = "{$export_directory}/{$filename}.zip";
    class Language_Pack extends WP_CLI_Command { 
    531571                        // Update/create directories.
    532572                        $this->update_svn_directory( $export_directory );
    533573
     574                        $entries = GP::$translation->for_export( $data->gp_project, $set, [ 'status' => 'current' ] );
     575                        if ( ! $entries ) {
     576                                WP_CLI::warning( "No current translations available for {$wp_locale}." );
     577
     578                                continue;
     579                        }
     580
     581                        $json_files = $this->build_json_files( $data->gp_project, $gp_locale, $set, $entries, $json_file_base );
     582
    534583                        // Create PO file.
    535                         $last_modified = $this->build_po_file( $data->gp_project, $gp_locale, $set, $po_file );
     584                        $last_modified = $this->build_po_file( $data->gp_project, $gp_locale, $set, $entries, $po_file );
    536585
    537586                        if ( is_wp_error( $last_modified ) ) {
    538587                                WP_CLI::warning( sprintf( "PO generation for {$wp_locale} failed: %s", $last_modified->get_error_message() ) );
    class Language_Pack extends WP_CLI_Command { 
    562611
    563612                        // Create ZIP file.
    564613                        $result = $this->execute_command( sprintf(
    565                                 'zip -9 -j %s %s %s 2>&1',
     614                                'zip -9 -j %s %s %s %s 2>&1',
    566615                                escapeshellarg( $zip_file ),
    567616                                escapeshellarg( $po_file ),
    568                                 escapeshellarg( $mo_file )
     617                                escapeshellarg( $mo_file ),
     618                                implode( ' ', array_map( 'escapeshellarg', $json_files ) )
    569619                        ) );
    570620
    571621                        if ( is_wp_error( $result ) ) {