diff --git a/features/makejson.feature b/features/makejson.feature index 948265f0..20f33461 100644 --- a/features/makejson.feature +++ b/features/makejson.feature @@ -984,3 +984,137 @@ Feature: Split PO files into JSON files. """ And the return code should be 0 And the foo-theme/my-custom-domain-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist + + Scenario: Should only process js/min.js extensions by default + Given an empty foo-theme directory + And a foo-theme/de_DE.po file: + """ + # Copyright (C) 2018 Foo Theme + # 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 \n" + "Language-Team: LANGUAGE \n" + "Language: de_DE\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-theme\n" + "Plural-Forms: nplurals=2; plural=(n != 1);\n" + + #: foo-theme.js:15 + msgid "Foo Theme" + msgstr "Foo Theme" + + #: bar.min.minified.min.js:15 + msgid "Foo Theme" + msgstr "Foo Theme" + + #: foo-theme.ts:15 + msgid "Foo Theme" + msgstr "Foo Theme" + + #: foo-theme.tag:15 + msgid "Foo Theme" + msgstr "Foo Theme" + """ + + When I run `wp i18n make-json foo-theme` + Then STDOUT should contain: + """ + Success: Created 2 files. + """ + And the return code should be 0 + And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist + And the foo-theme/foo-theme-de_DE-a9c6627f5fe96185c0a0d0ddd8fa0216.json file should exist + + Scenario: Allows processing custom file extensions + Given an empty foo-theme directory + And a foo-theme/de_DE.po file: + """ + # Copyright (C) 2018 Foo Theme + # 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 \n" + "Language-Team: LANGUAGE \n" + "Language: de_DE\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-theme\n" + "Plural-Forms: nplurals=2; plural=(n != 1);\n" + + #: foo-theme.js:15 + msgid "Foo Theme" + msgstr "Foo Theme" + + #: foo-theme.ts:15 + msgid "Foo Theme" + msgstr "Foo Theme" + + #: foo-theme.tag:15 + msgid "Foo Theme" + msgstr "Foo Theme" + """ + + When I run `wp i18n make-json foo-theme --extensions=ts,tag` + Then STDOUT should contain: + """ + Success: Created 3 files. + """ + And the return code should be 0 + And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist + And the foo-theme/foo-theme-de_DE-a4e9f6529ffa4750907c140158b834b9.json file should exist + And the foo-theme/foo-theme-de_DE-df975addfaa8d6579df3d3133999691a.json file should exist + + Scenario: Should use extensions, strip spaces and . from notation + Given an empty foo-theme directory + And a foo-theme/de_DE.po file: + """ + # Copyright (C) 2018 Foo Theme + # 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 \n" + "Language-Team: LANGUAGE \n" + "Language: de_DE\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-theme\n" + "Plural-Forms: nplurals=2; plural=(n != 1);\n" + + #: foo-theme.js:15 + msgid "Foo Theme" + msgstr "Foo Theme" + + #: foo-theme.ts:15 + msgid "Foo Theme" + msgstr "Foo Theme" + + #: foo-theme.tag:15 + msgid "Foo Theme" + msgstr "Foo Theme" + """ + + When I run `wp i18n make-json foo-theme --extensions=".ts, .tsx"` + Then STDOUT should contain: + """ + Success: Created 2 files. + """ + And the return code should be 0 + And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist + And the foo-theme/foo-theme-de_DE-a4e9f6529ffa4750907c140158b834b9.json file should exist diff --git a/src/MakeJsonCommand.php b/src/MakeJsonCommand.php index 7b662a0e..164ba06e 100644 --- a/src/MakeJsonCommand.php +++ b/src/MakeJsonCommand.php @@ -41,6 +41,9 @@ class MakeJsonCommand extends WP_CLI_Command { * [--domain=] * : Text domain to use for the JSON file name. Overrides the default one extracted from the PO file. * + * [--extensions=] + * : Additional custom JS extensions, comma separated list. By default searches for .min.js and .js extensions. + * * [--purge] * : Whether to purge the strings that were extracted from the original source file. Defaults to true, use `--no-purge` to skip the removal. * @@ -82,6 +85,12 @@ public function __invoke( $args, $assoc_args ) { $update_mo_files = Utils\get_flag_value( $assoc_args, 'update-mo-files', true ); $map_paths = Utils\get_flag_value( $assoc_args, 'use-map', false ); $domain = Utils\get_flag_value( $assoc_args, 'domain', '' ); + $extensions = array_map( + function ( $extension ) { + return trim( $extension, ' .' ); + }, + explode( ',', Utils\get_flag_value( $assoc_args, 'extensions', '' ) ) + ); if ( Utils\get_flag_value( $assoc_args, 'pretty-print', false ) ) { $this->json_options |= JSON_PRETTY_PRINT; @@ -119,7 +128,7 @@ public function __invoke( $args, $assoc_args ) { /** @var DirectoryIterator $file */ foreach ( $files as $file ) { if ( $file->isFile() && $file->isReadable() && 'po' === $file->getExtension() ) { - $result = $this->make_json( $file->getRealPath(), $destination, $map, $domain ); + $result = $this->make_json( $file->getRealPath(), $destination, $map, $domain, $extensions ); $result_count += count( $result ); if ( $purge ) { @@ -228,13 +237,15 @@ static function ( $value ) { * @param string $destination Path to the destination directory. * @param array|null $map Source to build file mapping. * @param string $domain Override text domain to use. + * @param array $extensions Additional extensions. * @return array List of created JSON files. */ - protected function make_json( $source_file, $destination, $map, $domain ) { + protected function make_json( $source_file, $destination, $map, $domain, $extensions ) { /** @var Translations[] $mapping */ $mapping = []; $translations = new Translations(); $result = []; + $extensions = array_merge( [ 'js' ], $extensions ); PoExtractor::fromFile( $source_file, $translations ); @@ -251,18 +262,13 @@ protected function make_json( $source_file, $destination, $map, $domain ) { // Find all unique sources this translation originates from. $sources = array_map( - static function ( $reference ) { - $file = $reference[0]; - - if ( substr( $file, - 7 ) === '.min.js' ) { - return substr( $file, 0, - 7 ) . '.js'; - } - - if ( substr( $file, - 3 ) === '.js' ) { - return $file; - } + static function ( $reference ) use ( $extensions ) { + $file = $reference[0]; + $extension = pathinfo( $file, PATHINFO_EXTENSION ); - return null; + return in_array( $extension, $extensions, true ) + ? preg_replace( "/.min.{$extension}$/", ".{$extension}", $file ) + : null; }, $this->reference_map( $translation->getReferences(), $map ) );