Skip to content

Commit

Permalink
Merge pull request #267 from wp-cli/add/subtract-and-merge
Browse files Browse the repository at this point in the history
make-pot: add `subtract-and-merge` flag
  • Loading branch information
swissspidy authored Jul 20, 2021
2 parents 35c860f + 291b65a commit 49fb6d9
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 25 deletions.
103 changes: 103 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2701,6 +2701,109 @@ Feature: Generate a POT file of a WordPress project
msgid "Some other text"
"""

@less-than-php-7.3
Scenario: Add references to files used in exception list
Given an empty directory
And a exception.pot file:
"""
# Copyright (C) 2018 Foo Plugin
# This file is distributed under the same license as the Foo Plugin package.
msgid ""
msgstr ""
"Project-Id-Version: Foo Plugin\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
"X-Domain: foo-plugin\n"
#: bar-plugin.php:2
#: bar-baz-plugin.php:20
msgid "Foo Bar"
msgstr ""
#: bar-plugin.php:5
#: bar-bar.php:50
msgid "Bar Baz"
msgstr ""
#: bar-plugin.php:17
#: bar-baz-plugin.php:99
#: foobar/plugin.php:39
msgid "Some other text"
msgstr ""
"""
And a foo-plugin.php file:
"""
<?php
/**
* Plugin Name: Foo Plugin
* Plugin URI: https://example.com
* Description:
* Version: 0.1.0
* Author:
* Author URI:
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: foo-plugin
* Domain Path: /languages
*/
__( 'Hello World', 'foo-plugin' );
__( 'Foo Bar', 'foo-plugin' );
__( 'Bar Baz', 'foo-plugin' );
__( 'Some text', 'foo-plugin' );
__( 'Some other text', 'foo-plugin' );
"""

When I run `wp i18n make-pot . foo-plugin.pot --domain=foo-plugin --subtract=exception.pot --subtract-and-merge`
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And STDERR should be empty
And the foo-plugin.pot file should contain:
"""
msgid "Hello World"
"""
And the foo-plugin.pot file should contain:
"""
msgid "Some text"
"""
And the foo-plugin.pot file should not contain:
"""
msgid "Foo Bar"
"""
And the foo-plugin.pot file should not contain:
"""
msgid "Bar Baz"
"""
And the foo-plugin.pot file should not contain:
"""
msgid "Some other text"
"""
And the exception.pot file should contain:
"""
#: foo-plugin.php:17
"""
And the exception.pot file should contain:
"""
#: foo-plugin.php:19
"""
And the exception.pot file should contain:
"""
#: foo-plugin.php:23
"""

Scenario: Extract strings for a generic project
Given an empty example-project directory
And a example-project/stuff.php file:
Expand Down
63 changes: 38 additions & 25 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ class MakePotCommand extends WP_CLI_Command {
protected $merge = [];

/**
* @var Translations
* @var Translations[]
*/
protected $exceptions;
protected $exceptions = [];

/**
* @var bool
*/
protected $subtract_and_merge;

/**
* @var array
Expand Down Expand Up @@ -190,6 +195,10 @@ class MakePotCommand extends WP_CLI_Command {
* This can be useful when you want to create multiple POT files from the same source directory with slightly
* different content and no duplicate strings between them.
*
* [--subtract-and-merge]
* : Whether source code references and comments from the generated POT file should be instead added to the POT file
* used for subtraction. Warning: this modifies the files passed to `--subtract`!
*
* [--include=<paths>]
* : Comma-separated list of files and paths that should be used for string extraction.
* If provided, only these files and folders will be taken into account for string extraction.
Expand Down Expand Up @@ -393,31 +402,21 @@ function ( $file ) {
}
}

$this->exceptions = new Translations();

if ( isset( $assoc_args['subtract'] ) ) {
$exceptions = explode( ',', $assoc_args['subtract'] );

$exceptions = array_filter(
$exceptions,
function ( $exception ) {
if ( ! file_exists( $exception ) ) {
WP_CLI::warning( sprintf( 'Invalid file provided to --subtract: %s', $exception ) );

return false;
}
$this->subtract_and_merge = Utils\get_flag_value( $assoc_args, 'subtract-and-merge', false );

$exception_translations = new Translations();
$files = explode( ',', $assoc_args['subtract'] );

Po::fromFile( $exception, $exception_translations );
$this->exceptions->mergeWith( $exception_translations );

return true;
foreach ( $files as $file ) {
if ( ! file_exists( $file ) ) {
WP_CLI::warning( sprintf( 'Invalid file provided to --subtract: %s', $file ) );
continue;
}
);

if ( ! empty( $exceptions ) ) {
WP_CLI::debug( sprintf( 'Ignoring any string already existing in: %s', implode( ',', $exceptions ) ), 'make-pot' );
WP_CLI::debug( sprintf( 'Ignoring any string already existing in: %s', $file ), 'make-pot' );

$this->exceptions[ $file ] = new Translations();
Po::fromFile( $file, $this->exceptions[ $file ] );
}
}

Expand Down Expand Up @@ -666,9 +665,23 @@ protected function extract_strings() {
WP_CLI::error( $e->getMessage() );
}

foreach ( $this->exceptions as $translation ) {
if ( $translations->find( $translation ) ) {
unset( $translations[ $translation->getId() ] );
foreach ( $this->exceptions as $file => $exception_translations ) {
/** @var Translation $exception_translation */
foreach ( $exception_translations as $exception_translation ) {
if ( ! $translations->find( $exception_translation ) ) {
continue;
}

if ( $this->subtract_and_merge ) {
$translation = $translations[ $exception_translation->getId() ];
$exception_translation->mergeWith( $translation );
}

unset( $translations[ $exception_translation->getId() ] );
}

if ( $this->subtract_and_merge ) {
PotGenerator::toFile( $exception_translations, $file );
}
}

Expand Down

0 comments on commit 49fb6d9

Please sign in to comment.