Making WordPress.org

Ticket #3876: 3876.3.diff

File 3876.3.diff, 6.0 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..dd811ffcb 100644
    class Language_Pack extends WP_CLI_Command { 
    374374        }
    375375
    376376        /**
    377          * Builds a PO file for translations.
     377         * Build a mapping of JS files to translation entries occurring in those files.
     378         * Translation entries occurring in other files are added to the 'po' key.
    378379         *
    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.
    384418         */
    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;
    389432                }
    390433
     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 ) {
    391448                $format     = gp_array_get( GP::$formats, 'po' );
    392449                $po_content = $format->print_exported_file( $gp_project, $gp_locale, $set, $entries );
    393450
    class Language_Pack extends WP_CLI_Command { 
    523580                        $export_directory = "{$data->svn_checkout}/{$data->domain}/{$data->version}/{$wp_locale}";
    524581                        $build_directory  = self::BUILD_DIR . "/{$data->type}s/{$data->domain}/{$data->version}";
    525582                        $filename         = "{$data->domain}-{$wp_locale}";
     583                        $json_file_base   = "{$export_directory}/{$filename}";
    526584                        $po_file          = "{$export_directory}/{$filename}.po";
    527585                        $mo_file          = "{$export_directory}/{$filename}.mo";
    528586                        $zip_file         = "{$export_directory}/{$filename}.zip";
    class Language_Pack extends WP_CLI_Command { 
    531589                        // Update/create directories.
    532590                        $this->update_svn_directory( $export_directory );
    533591
     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
    534608                        // Create PO file.
    535                         $last_modified = $this->build_po_file( $data->gp_project, $gp_locale, $set, $po_file );
     609                        $last_modified = $this->build_po_file( $data->gp_project, $gp_locale, $set, $po_entries, $po_file );
    536610
    537611                        if ( is_wp_error( $last_modified ) ) {
    538612                                WP_CLI::warning( sprintf( "PO generation for {$wp_locale} failed: %s", $last_modified->get_error_message() ) );
    class Language_Pack extends WP_CLI_Command { 
    562636
    563637                        // Create ZIP file.
    564638                        $result = $this->execute_command( sprintf(
    565                                 'zip -9 -j %s %s %s 2>&1',
     639                                'zip -9 -j %s %s %s %s 2>&1',
    566640                                escapeshellarg( $zip_file ),
    567641                                escapeshellarg( $po_file ),
    568                                 escapeshellarg( $mo_file )
     642                                escapeshellarg( $mo_file ),
     643                                implode( ' ', array_map( 'escapeshellarg', $json_files ) )
    569644                        ) );
    570645
    571646                        if ( is_wp_error( $result ) ) {