Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to merge with existing POT file #31

Merged
merged 8 commits into from Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Expand Up @@ -31,7 +31,7 @@ wp i18n
Create a POT file for a WordPress plugin or theme.

~~~
wp i18n make-pot <source> [<destination>] [--slug=<slug>] [--domain=<domain>] [--exclude=<paths>]
wp i18n make-pot <source> [<destination>] [--slug=<slug>] [--domain=<domain>] [--merge[=<file>]] [--exclude=<paths>]
~~~

**OPTIONS**
Expand All @@ -43,17 +43,19 @@ wp i18n make-pot <source> [<destination>] [--slug=<slug>] [--domain=<domain>] [-
Name of the resulting POT file.

[--slug=<slug>]
Plugin slug. Defaults to the source directory's basename.
Plugin or theme slug. Defaults to the source directory's basename.

[--domain=<domain>]
Text domain to look for in the source code. Defaults to the plugin/theme slug.

[--merge[=<file>]]
Existing POT file file whose content should be merged with the extracted strings.
By default, the following files and folders are ignored: node_modules, .git, .svn, .CVS, .hg, vendor.
Leading and trailing slashes are ignored, i.e. `/my/directory/` is the same as `my/directory`.

[--exclude=<paths>]
Include additional ignored paths as CSV (e.g. 'tests,bin,.github').

By default, the following files and folders are ignored: node_modules, .git, .svn, .CVS, .hg, vendor.

Leading and trailing slashes are ignored, i.e. `/my/directory/` is the same as `my/directory`.
If left empty, defaults to the destination POT file.

**EXAMPLES**

Expand Down
171 changes: 171 additions & 0 deletions features/makepot.feature
Expand Up @@ -826,3 +826,174 @@ Feature: Generate a POT file of a WordPress plugin
"""
I am not being ignored either
"""

Scenario: Merges translations with the ones from an existing POT file
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.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 <LL@li.org>\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"

#: foo-plugin.js:15
msgid "Foo Plugin"
msgstr ""
"""

When I run `wp scaffold plugin hello-world --plugin_name="Hello World" --plugin_author="John Doe" --plugin_author_uri="https://example.com" --plugin_uri="https://foo.example.com"`
Then the wp-content/plugins/hello-world directory should exist
And the wp-content/plugins/hello-world/hello-world.php file should exist

When I run `wp i18n make-pot wp-content/plugins/hello-world wp-content/plugins/hello-world/languages/hello-world.pot --merge=foo-plugin/foo-plugin.pot`
Then the wp-content/plugins/hello-world/languages/hello-world.pot file should exist
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And STDERR should be empty
And the wp-content/plugins/hello-world/languages/hello-world.pot file should exist
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
"""
msgid "Hello World"
"""
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
"""
#: foo-plugin.js:15
"""
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
"""
msgid "Foo Plugin"
"""

Scenario: Merges translations with existing destination file
When I run `wp scaffold plugin hello-world --plugin_name="Hello World" --plugin_author="John Doe" --plugin_author_uri="https://example.com" --plugin_uri="https://foo.example.com"`
Then the wp-content/plugins/hello-world directory should exist
And the wp-content/plugins/hello-world/hello-world.php file should exist

Given a wp-content/plugins/hello-world/languages/hello-world.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 <LL@li.org>\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"

#: hello-world.js:15
msgid "Hello World JS"
msgstr ""
"""

When I run `wp i18n make-pot wp-content/plugins/hello-world wp-content/plugins/hello-world/languages/hello-world.pot --merge`
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And STDERR should be empty
And the wp-content/plugins/hello-world/languages/hello-world.pot file should exist
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
"""
msgid "Hello World"
"""
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
"""
#: hello-world.js:15
"""
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
"""
msgid "Hello World JS"
"""

Scenario: Uses newer file headers when merging translations
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.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: John Doe <johndoe@example.com>\n"
"Language-Team: Language Team <foo@example.com>\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"

#: foo-plugin.js:15
msgid "Foo Plugin"
msgstr ""
"""

When I run `wp scaffold plugin hello-world --plugin_name="Hello World" --plugin_author="John Doe" --plugin_author_uri="https://example.com" --plugin_uri="https://foo.example.com"`
Then the wp-content/plugins/hello-world directory should exist
And the wp-content/plugins/hello-world/hello-world.php file should exist

When I run `wp i18n make-pot wp-content/plugins/hello-world wp-content/plugins/hello-world/languages/hello-world.pot --merge=foo-plugin/foo-plugin.pot`
Then the wp-content/plugins/hello-world/languages/hello-world.pot file should exist
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And STDERR should be empty
And the wp-content/plugins/hello-world/languages/hello-world.pot file should exist
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
"""
# Copyright (C) 2018 Hello World
# This file is distributed under the same license as the Hello World package.
msgid ""
msgstr ""
"Project-Id-Version: Hello World 0.1.0\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/hello-world\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"""
And the wp-content/plugins/hello-world/languages/hello-world.pot file should not contain:
"""
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
"""
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
"""
"X-Domain: hello-world\n"
"""
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
"""
msgid "Hello World"
"""
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
"""
#: foo-plugin.js:15
"""
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
"""
msgid "Foo Plugin"
"""
38 changes: 33 additions & 5 deletions src/MakePotCommand.php
Expand Up @@ -2,6 +2,8 @@

namespace WP_CLI\I18n;

use Gettext\Extractors\Po;
use Gettext\Merge;
use Gettext\Translation;
use Gettext\Translations;
use WP_CLI;
Expand Down Expand Up @@ -29,6 +31,11 @@ class MakePotCommand extends WP_CLI_Command {
/**
* @var string
*/
protected $merge;

/**
* @var array
*/
protected $exclude = [ 'node_modules', '.git', '.svn', '.CVS', '.hg', 'vendor' ];

/**
Expand All @@ -53,18 +60,20 @@ class MakePotCommand extends WP_CLI_Command {
* : Name of the resulting POT file.
*
* [--slug=<slug>]
* : Plugin slug. Defaults to the source directory's basename.
* : Plugin or theme slug. Defaults to the source directory's basename.
*
* [--domain=<domain>]
* : Text domain to look for in the source code. Defaults to the plugin/theme slug.
*
* [--exclude=<paths>]
* : Include additional ignored paths as CSV (e.g. 'tests,bin,.github').
*
* [--merge[=<file>]]
* : Existing POT file file whose content should be merged with the extracted strings.
* By default, the following files and folders are ignored: node_modules, .git, .svn, .CVS, .hg, vendor.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging seems to have messed up the docblocks. --merge & --exclude have the wrong explanations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also need a refresh of the README.md once fixed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed now!

*
* Leading and trailing slashes are ignored, i.e. `/my/directory/` is the same as `my/directory`.
*
* [--exclude=<paths>]
* : Include additional ignored paths as CSV (e.g. 'tests,bin,.github').
* If left empty, defaults to the destination POT file.
*
* ## EXAMPLES
*
* # Create a POT file for the WordPress plugin/theme in the current directory
Expand Down Expand Up @@ -102,6 +111,18 @@ public function __invoke( $args, $assoc_args ) {
WP_CLI::error( 'Could not create destination directory!' );
}

if ( isset( $assoc_args['merge'] ) ) {
if ( true === $assoc_args['merge'] && file_exists( $this->destination ) ) {
$this->merge = $this->destination;
} elseif ( ! empty( $assoc_args['merge'] ) ) {
if ( ! file_exists( $assoc_args['merge'] ) ) {
WP_CLI::error( sprintf( 'Invalid file %s', $assoc_args['merge'] ) );
}

$this->merge = $assoc_args['merge'];
}
}

if ( isset( $assoc_args['exclude'] ) ) {
$this->exclude = array_filter( array_merge( $this->exclude, explode( ',', $assoc_args['exclude'] ) ) );
$this->exclude = array_map(function($exclude) {
Expand Down Expand Up @@ -217,6 +238,13 @@ protected function get_main_file_data() {
protected function makepot( $domain ) {
$this->translations = new Translations();

// Add existing strings first but don't keep headers.
if ( $this->merge ) {
$existing_translations = new Translations();
Po::fromFile( $this->merge, $existing_translations );
$this->translations->mergeWith( $existing_translations, Merge::ADD | Merge::REMOVE );
}

$meta = $this->get_meta_data();
PotGenerator::setCommentBeforeHeaders( $meta['comments'] );

Expand Down