Making WordPress.org

Changeset 10063


Ignore:
Timestamp:
07/11/2020 03:57:12 PM (4 years ago)
Author:
ocean90
Message:

Translate: Adjust make-core-pot command to merge references and comments of duplicate originals before removing the duplicates.

Also, fix duplicate file comments in POT files and improve exclude lists.

See #5314.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-make-core-pot.php

    r9210 r10063  
    33namespace WordPressdotorg\GlotPress\Customizations\CLI;
    44
     5use Gettext\Extractors\Po;
     6use Gettext\Translations;
    57use GP;
    68use WP_CLI;
    79use WP_CLI\Utils;
    810use WP_CLI_Command;
     11use WP_CLI\I18n\PotGenerator;
    912
    1013class Make_Core_Pot extends WP_CLI_Command {
     
    5760
    5861        $file_comment = sprintf(
    59             'Copyright (C) %s by the contributors\nThis file is distributed under the same license as the WordPress package.',
     62            "Copyright (C) %s by the contributors\nThis file is distributed under the same license as the WordPress package.",
    6063            date( 'Y' )
    6164        );
     65        PotGenerator::setCommentBeforeHeaders( $file_comment );
    6266
    6367        $command_args = [
     
    7276            'package-name'  => self::PACKAGE_NAME,
    7377            'headers'       => $headers,
    74             'file-comment'  => $file_comment,
     78            'file-comment'  => '',
    7579            'skip-js'       => true,
    7680            'skip-audit'    => true,
     
    108112            'wp-includes/js/codemirror/*',
    109113            'wp-includes/js/crop/*',
     114            'wp-includes/js/dist/vendor/*',
    110115            'wp-includes/js/imgareaselect/*',
    111116            'wp-includes/js/jcrop/*',
     
    115120            'wp-includes/js/swfupload/*',
    116121            'wp-includes/js/thickbox/*',
     122            'wp-includes/js/clipboard.js',
     123            'wp-includes/js/colorpicker.js',
     124            'wp-includes/js/hoverIntent.js',
     125            'wp-includes/js/json2.js',
     126            'wp-includes/js/swfobject.js',
    117127            'wp-includes/js/tw-sack.js',
     128            'wp-includes/js/twemoji.js',
     129            'wp-includes/js/underscore.js',
    118130        ];
    119131
     
    134146            'package-name'  => self::PACKAGE_NAME,
    135147            'headers'       => $headers,
    136             'file-comment'  => $file_comment,
     148            'file-comment'  => '',
    137149            'skip-audit'    => true,
    138150            'ignore-domain' => true,
     
    160172            'package-name'  => self::PACKAGE_NAME,
    161173            'headers'       => $headers,
    162             'file-comment'  => $file_comment,
     174            'file-comment'  => '',
    163175            'skip-js'       => true,
    164176            'skip-audit'    => true,
     
    184196        $admin_exclude = [
    185197            'wp-admin/includes/continents-cities.php',
    186             'editor.min.js', // Explicitly excluded as it's causing a memory leak: https://github.com/wp-cli/i18n-command/issues/185
    187198            // External libraries.
    188199            'wp-admin/includes/class-ftp*',
     
    190201        ];
    191202
     203        // Explicitly exclude minified JavaScript files as they may
     204        // cause memory leaks: https://github.com/wp-cli/i18n-command/issues/185.
     205        $admin_min_js  = array_map( 'basename', glob( $this->source . '/wp-admin/js/*.min.js' ) );
     206        $admin_exclude = array_merge( $admin_exclude, $admin_min_js );
     207
    192208        $admin_exclude = array_merge( $admin_exclude, $admin_network_files );
    193209
     
    203219            'include'       => 'wp-admin/*',
    204220            'merge'         => $hello_dolly_pot,
    205             'subtract'      => $this->destination . '/wordpress.pot',
    206             'package-name'  => self::PACKAGE_NAME,
    207             'headers'       => $headers,
    208             'file-comment'  => $file_comment,
     221            'package-name'  => self::PACKAGE_NAME,
     222            'headers'       => $headers,
     223            'file-comment'  => '',
    209224            'skip-audit'    => true,
    210225            'ignore-domain' => true,
     
    219234        unlink( $hello_dolly_pot );
    220235
     236        $this->merge_pot( $this->destination . '/wordpress.pot', $this->destination . '/wordpress-admin.pot' );
     237
    221238        // Admin Network.
    222239        $command_args = [
     
    229246        $command_assoc_args = [
    230247            'include'       => implode( ',', $admin_network_files ),
    231             'subtract'      => sprintf( '%1$s/wordpress.pot,%1$s/wordpress-admin.pot', $this->destination ),
    232             'package-name'  => self::PACKAGE_NAME,
    233             'headers'       => $headers,
    234             'file-comment'  => $file_comment,
     248            'package-name'  => self::PACKAGE_NAME,
     249            'headers'       => $headers,
     250            'file-comment'  => '',
    235251            'skip-js'       => true, // TODO: No use of wp.i18n, yet.
    236252            'skip-audit'    => true,
     
    239255
    240256        WP_CLI::run_command( $command_args, $command_assoc_args );
     257
     258        $this->merge_pot( $this->destination . '/wordpress.pot', $this->destination . '/wordpress-admin-network.pot' );
     259        $this->merge_pot( $this->destination . '/wordpress-admin.pot', $this->destination . '/wordpress-admin-network.pot' );
    241260    }
    242261
     
    254273        return preg_match( '/\$wp_version\s*=\s*\'(.*?)\';/', file_get_contents( $version_php ), $matches ) ? $matches[1] : false;
    255274    }
     275
     276    /**
     277     * Merges duplicate originals of two POT files.
     278     *
     279     * Preserves the references and comments of each original.
     280     *
     281     * @param string $to_file   Path to the POT file where duplicates should be merged into.
     282     * @param string $from_file Path to the POT file where duplicates should be extracted from.
     283     */
     284    private function merge_pot( $to_file, $from_file ) {
     285        $to = new Translations();
     286        Po::fromFile( $to_file, $to );
     287        $from = new Translations();
     288        Po::fromFile( $from_file, $from );
     289
     290        foreach ( $to as $original ) {
     291            // Check for duplicate.
     292            $existing = $from->find( $original );
     293            if ( ! $existing ) {
     294                continue;
     295            }
     296
     297            // Merge.
     298            $original->mergeWith( $existing );
     299
     300            // Remove original from source
     301            unset( $from[ $existing->getId() ] );
     302        }
     303
     304        $to->deleteHeader( Translations::HEADER_LANGUAGE );
     305        $from->deleteHeader( Translations::HEADER_LANGUAGE );
     306        PotGenerator::toFile( $to, $to_file );
     307        PotGenerator::toFile( $from, $from_file );
     308    }
    256309}
Note: See TracChangeset for help on using the changeset viewer.