Making WordPress.org

Ticket #3876: 3876.6.diff

File 3876.6.diff, 6.1 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..2c8195b02 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.
    384383         */
    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                        }
    389408                }
    390409
     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 ) {
    391452                $format     = gp_array_get( GP::$formats, 'po' );
    392453                $po_content = $format->print_exported_file( $gp_project, $gp_locale, $set, $entries );
    393454
    class Language_Pack extends WP_CLI_Command { 
    523584                        $export_directory = "{$data->svn_checkout}/{$data->domain}/{$data->version}/{$wp_locale}";
    524585                        $build_directory  = self::BUILD_DIR . "/{$data->type}s/{$data->domain}/{$data->version}";
    525586                        $filename         = "{$data->domain}-{$wp_locale}";
     587                        $json_file_base   = "{$export_directory}/{$filename}";
    526588                        $po_file          = "{$export_directory}/{$filename}.po";
    527589                        $mo_file          = "{$export_directory}/{$filename}.mo";
    528590                        $zip_file         = "{$export_directory}/{$filename}.zip";
    class Language_Pack extends WP_CLI_Command { 
    531593                        // Update/create directories.
    532594                        $this->update_svn_directory( $export_directory );
    533595
     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
    534612                        // Create PO file.
    535                         $last_modified = $this->build_po_file( $data->gp_project, $gp_locale, $set, $po_file );
     613                        $last_modified = $this->build_po_file( $data->gp_project, $gp_locale, $set, $po_entries, $po_file );
    536614
    537615                        if ( is_wp_error( $last_modified ) ) {
    538616                                WP_CLI::warning( sprintf( "PO generation for {$wp_locale} failed: %s", $last_modified->get_error_message() ) );
    class Language_Pack extends WP_CLI_Command { 
    562640
    563641                        // Create ZIP file.
    564642                        $result = $this->execute_command( sprintf(
    565                                 'zip -9 -j %s %s %s 2>&1',
     643                                'zip -9 -j %s %s %s %s 2>&1',
    566644                                escapeshellarg( $zip_file ),
    567645                                escapeshellarg( $po_file ),
    568                                 escapeshellarg( $mo_file )
     646                                escapeshellarg( $mo_file ),
     647                                implode( ' ', array_map( 'escapeshellarg', $json_files ) )
    569648                        ) );
    570649
    571650                        if ( is_wp_error( $result ) ) {