From 98963dd0f67c487d41edeea88ab33e72d72f8af7 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 6 Jul 2018 15:08:55 +0200 Subject: [PATCH] Add option to extract strings with any text domain --- README.md | 7 +++- features/makepot.feature | 75 +++++++++++++++++++++++++++++++++++++ src/JsFunctionsScanner.php | 30 ++++----------- src/MakePotCommand.php | 26 ++++++++++--- src/PhpFunctionsScanner.php | 40 ++++---------------- 5 files changed, 115 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 10ff4627..bc59db0d 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ wp i18n Create a POT file for a WordPress plugin or theme. ~~~ -wp i18n make-pot [] [--slug=] [--domain=] [--merge[=]] [--exclude=] [--skip-js] +wp i18n make-pot [] [--slug=] [--domain=] [--ignore-domain] [--merge[=]] [--exclude=] [--skip-js] ~~~ Scans PHP and JavaScript files, as well as theme stylesheets for translatable strings. @@ -48,7 +48,10 @@ 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=] - Text domain to look for in the source code. Defaults to the plugin/theme slug. + Text domain to look for in the source code. Defaults to the plugin/theme slug, unless the `--ignore-domain` option is used. + + [--ignore-domain] + Ignore the text domain completely and extract strings with any text domain. [--merge[=]] Existing POT file file whose content should be merged with the extracted strings. diff --git a/features/makepot.feature b/features/makepot.feature index 097ce661..149ef0cb 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -1222,6 +1222,11 @@ Feature: Generate a POT file of a WordPress plugin """ When I run `wp i18n make-pot foo-plugin foo-plugin.pot --domain=bar` + Then STDOUT should be: + """ + Plugin file detected. + Success: POT file successfully generated! + """ And the foo-plugin.pot file should contain: """ msgid "Foo" @@ -1254,6 +1259,11 @@ Feature: Generate a POT file of a WordPress plugin """ When I run `wp i18n make-pot foo-plugin foo-plugin.pot --skip-js` + Then STDOUT should be: + """ + Plugin file detected. + Success: POT file successfully generated! + """ And the foo-plugin.pot file should contain: """ msgid "Foo Plugin" @@ -1262,3 +1272,68 @@ Feature: Generate a POT file of a WordPress plugin """ msgid "Hello World" """ + + Scenario: Extract all strings regardless of text domain + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin.php file: + """ + getArguments() as $argument ) { @@ -106,39 +106,23 @@ public function saveGettextFunctions( Translations $translations, array $options switch ( $functions[ $callee->getName() ] ) { case 'text_domain': case 'gettext': - if ( ! isset( $args[1] ) ) { - break; - } - - list( $original, $domain ) = $args; + list( $original, $domain ) = array_pad( $args, 2, null ); break; case 'text_context_domain': - if ( ! isset( $args[2] ) ) { - break; - } - - list( $original, $context, $domain ) = $args; + list( $original, $context, $domain ) = array_pad( $args, 3, null ); break; case 'single_plural_number_domain': - if ( ! isset( $args[3] ) ) { - break; - } - - list( $original, $plural, $number, $domain ) = $args; + list( $original, $plural, $number, $domain ) = array_pad( $args, 4, null ); break; case 'single_plural_number_context_domain': - if ( ! isset( $args[4] ) ) { - break; - } - - list( $original, $plural, $number, $context, $domain ) = $args; + list( $original, $plural, $number, $context, $domain ) = array_pad( $args, 5, null ); break; } - if ( (string) $original !== '' && ( $domain === null || $domain === $translations->getDomain() ) ) { + if ( (string) $original !== '' && ( $domain === $translations->getDomain() || null === $translations->getDomain() ) ) { $translation = $translations->insert( $context, $original, $plural ); $translation->addReference( $file, $node->getLocation()->getStart()->getLine() ); diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index 43990d39..f3bf27db 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -53,6 +53,11 @@ class MakePotCommand extends WP_CLI_Command { */ protected $skip_js = false; + /** + * @var string + */ + protected $domain; + /** * Create a POT file for a WordPress plugin or theme. * @@ -70,7 +75,10 @@ class MakePotCommand extends WP_CLI_Command { * : Plugin or theme slug. Defaults to the source directory's basename. * * [--domain=] - * : Text domain to look for in the source code. Defaults to the plugin/theme slug. + * : Text domain to look for in the source code. Defaults to the plugin/theme slug, unless the `--ignore-domain` option is used. + * + * [--ignore-domain] + * : Ignore the text domain completely and extract strings with any text domain. * * [--merge[=]] * : Existing POT file file whose content should be merged with the extracted strings. @@ -96,6 +104,12 @@ public function __invoke( $args, $assoc_args ) { $this->slug = Utils\get_flag_value( $assoc_args, 'slug', Utils\basename( $this->source ) ); $this->skip_js = Utils\get_flag_value( $assoc_args, 'skip-js', $this->skip_js ); + $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!' ); } @@ -144,7 +158,7 @@ public function __invoke( $args, $assoc_args ) { $this->exclude = array_unique( $this->exclude ); } - if ( ! $this->makepot( Utils\get_flag_value( $assoc_args, 'domain', $this->slug ) ) ) { + if ( ! $this->makepot() ) { WP_CLI::error( 'Could not generate a POT file!' ); } @@ -245,11 +259,9 @@ protected function get_main_file_data() { /** * Creates a POT file and stores it on disk. * - * @param string $domain The text domain to extract. - * * @return bool True on success, false otherwise. */ - protected function makepot( $domain ) { + protected function makepot() { $this->translations = new Translations(); // Add existing strings first but don't keep headers. @@ -267,7 +279,9 @@ protected function makepot( $domain ) { // POT files have no Language header. $this->translations->deleteHeader( Translations::HEADER_LANGUAGE ); - $this->translations->setDomain( $domain ); + if ( $this->domain ) { + $this->translations->setDomain( $this->domain ); + } $file_data = $this->get_main_file_data(); diff --git a/src/PhpFunctionsScanner.php b/src/PhpFunctionsScanner.php index 16e92f5f..e065a142 100644 --- a/src/PhpFunctionsScanner.php +++ b/src/PhpFunctionsScanner.php @@ -20,56 +20,32 @@ public function saveGettextFunctions( Translations $translations, array $options continue; } - $domain = $context = $original = $plural = null; + $context = $plural = null; switch ( $functions[ $name ] ) { case 'text_domain': case 'gettext': - if ( ! isset( $args[1] ) ) { - continue 2; - } - - list( $original, $domain ) = $args; + list( $original, $domain ) = array_pad( $args, 2, null ); break; case 'text_context_domain': - if ( ! isset( $args[2] ) ) { - continue 2; - } - - list( $original, $context, $domain ) = $args; + list( $original, $context, $domain ) = array_pad( $args, 3, null ); break; case 'single_plural_number_domain': - if ( ! isset( $args[3] ) ) { - continue 2; - } - - list( $original, $plural, $number, $domain ) = $args; + list( $original, $plural, $number, $domain ) = array_pad( $args, 4, null ); break; case 'single_plural_number_context_domain': - if ( ! isset( $args[4] ) ) { - continue 2; - } - - list( $original, $plural, $number, $context, $domain ) = $args; + list( $original, $plural, $number, $context, $domain ) = array_pad( $args, 5, null ); break; case 'single_plural_domain': - if ( ! isset( $args[2] ) ) { - continue 2; - } - - list( $original, $plural, $domain ) = $args; + list( $original, $plural, $domain ) = array_pad( $args, 3, null ); break; case 'single_plural_context_domain': - if ( ! isset( $args[3] ) ) { - continue 2; - } - - list( $original, $plural, $context, $domain ) = $args; + list( $original, $plural, $context, $domain ) = array_pad( $args, 4, null ); break; default: @@ -77,7 +53,7 @@ public function saveGettextFunctions( Translations $translations, array $options \WP_CLI::error( sprintf( "Internal error: unknown function map '%s' for '%s'.", $functions[ $name ], $name ) ); } - if ( (string) $original !== '' && ( $domain === null || $domain === $translations->getDomain() ) ) { + if ( (string) $original !== '' && ( $domain === $translations->getDomain() || null === $translations->getDomain() ) ) { $translation = $translations->insert( $context, $original, $plural ); $translation = $translation->addReference( $file, $line );