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

Text domain header #43

Merged
merged 3 commits into from Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -48,7 +48,9 @@ Scans PHP and JavaScript files, as well as theme stylesheets for translatable st
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, unless the `--ignore-domain` option is used.
Text domain to look for in the source code, unless the `--ignore-domain` option is used.
By default, the "Text Domain" header of the plugin or theme is used.
If none is provided, it falls back to the plugin/theme slug.

[--ignore-domain]
Ignore the text domain completely and extract strings with any text domain.
Expand Down
40 changes: 40 additions & 0 deletions features/makepot.feature
Expand Up @@ -239,6 +239,46 @@ Feature: Generate a POT file of a WordPress plugin
And STDERR should be empty
And the foo-plugin/languages/foo-plugin.pot file should exist

Scenario: Uses Text Domain header when no domain is set.
Given an empty foo-plugin directory
And a foo-plugin/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: bar-plugin
* Domain Path: /languages
*/

__( 'Foo Text', 'foo-plugin' );

__( 'Bar Text', 'bar-plugin' );
"""

When I run `wp i18n make-pot foo-plugin foo-plugin.pot`
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And STDERR should be empty
And the foo-plugin.pot file should exist
And the foo-plugin.pot file should contain:
"""
msgid "Bar Text"
"""
And the foo-plugin.pot file should not contain:
"""
msgid "Foo Text"
"""

Scenario: Extract all supported functions
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
Expand Down
55 changes: 36 additions & 19 deletions src/MakePotCommand.php
Expand Up @@ -75,7 +75,9 @@ class MakePotCommand extends WP_CLI_Command {
* : 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, unless the `--ignore-domain` option is used.
* : Text domain to look for in the source code, unless the `--ignore-domain` option is used.
* By default, the "Text Domain" header of the plugin or theme is used.
* If none is provided, it falls back to the plugin/theme slug.
*
* [--ignore-domain]
* : Ignore the text domain completely and extract strings with any text domain.
Expand Down Expand Up @@ -106,29 +108,34 @@ public function __invoke( $args, $assoc_args ) {

$ignore_domain = Utils\get_flag_value( $assoc_args, 'ignore-domain', false );

if ( ! $ignore_domain ) {
$this->domain = Utils\get_flag_value( $assoc_args, 'domain', $this->slug );
}

if ( ! $this->source || ! is_dir( $this->source ) ) {
WP_CLI::error( 'Not a valid source directory!' );
}

$this->retrieve_main_file_data();

// Determine destination.
if ( isset( $args[1] ) ) {
$this->destination = $args[1];
} else {
$file_data = $this->get_main_file_data();
$file_data = $this->get_main_file_data();

// Current directory.
$this->destination = $this->slug . '.pot';
if ( ! $ignore_domain ) {
$this->domain = $this->slug;

if ( isset( $file_data['Domain Path'] ) ) {
// Domain Path inside source folder.
$this->destination = $this->source . DIRECTORY_SEPARATOR . $file_data['Domain Path'] . DIRECTORY_SEPARATOR . $this->slug . '.pot';
if ( ! empty( $file_data['Text Domain'] ) ) {
$this->domain = $file_data['Text Domain'];
}

$this->domain = Utils\get_flag_value( $assoc_args, 'domain', $this->domain );
}

// Determine destination.
$this->destination = "{$this->source}/{$this->slug}.pot";

if ( ! empty( $file_data['Domain Path'] ) ) {
// Domain Path inside source folder.
$this->destination = sprintf( '%s/%s/%s.pot', $this->source, $file_data['Domain Path'], $this->slug );
}

if ( isset( $args[1] ) ) {
$this->destination = $args[1];
}

// Two is_dir() checks in case of a race condition.
Expand All @@ -152,9 +159,7 @@ public function __invoke( $args, $assoc_args ) {

if ( isset( $assoc_args['exclude'] ) ) {
$this->exclude = array_filter( array_merge( $this->exclude, explode( ',', $assoc_args['exclude'] ) ) );
$this->exclude = array_map(function($exclude) {
return ltrim( rtrim( $exclude, '/\\' ), '/\\' );
}, $this->exclude);
$this->exclude = array_map( [ $this, 'unslashit' ], $this->exclude);
$this->exclude = array_unique( $this->exclude );
}

Expand All @@ -165,6 +170,16 @@ public function __invoke( $args, $assoc_args ) {
WP_CLI::success( 'POT file successfully generated!' );
}

/**
* Removes leading and trailing slashes of a string.
*
* @param string $string What to add the remove slashes from.
* @return string String without leading and trailing slashes.
*/
protected function unslashit( $string ) {
return ltrim( rtrim( $string, '/\\' ), '/\\' );
Copy link
Member

Choose a reason for hiding this comment

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

This would actually be a good use of DIRECTORY_SEPARATOR. However, as we are not quite sure what we get and what we already did with it, I'd say we leave it as is now to stay on the safe side (especially with Cygwin on Windows).

}

/**
* Retrieves the main file data of the plugin or theme.
*
Expand Down Expand Up @@ -230,6 +245,7 @@ protected function get_file_headers( $type ) {
'Author URI',
'Version',
'Domain Path',
'Text Domain',
];
case 'theme':
return [
Expand All @@ -241,6 +257,7 @@ protected function get_file_headers( $type ) {
'Version',
'License',
'Domain Path',
'Text Domain',
];
default:
return [];
Expand Down Expand Up @@ -285,7 +302,7 @@ protected function makepot() {

$file_data = $this->get_main_file_data();

unset( $file_data['Version'], $file_data['License'], $file_data['Domain Path'] );
unset( $file_data['Version'], $file_data['License'], $file_data['Domain Path'], $file_data['Text Domain'] );

// Set entries from main file data.
foreach ( $file_data as $header => $data ) {
Expand Down