diff --git a/README.md b/README.md index 5782b1cc..51a941af 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,32 @@ wp i18n make-mo [] +### wp i18n make-php + +Create PHP files from PO files. + +~~~ +wp i18n make-php [] +~~~ + +**OPTIONS** + + + Path to an existing PO file or a directory containing multiple PO files. + + [] + Path to the destination directory for the resulting PHP files. Defaults to the source directory. + +**EXAMPLES** + + # Create PHP files for all PO files in the current directory. + $ wp i18n make-php . + + # Create a PHP file from a single PO file in a specific directory. + $ wp i18n make-php example-plugin-de_DE.po languages + + + ### wp i18n update-po Update PO files from a POT file. diff --git a/composer.json b/composer.json index 54d6b5c5..54bb1b2e 100644 --- a/composer.json +++ b/composer.json @@ -42,6 +42,7 @@ "i18n make-pot", "i18n make-json", "i18n make-mo", + "i18n make-php", "i18n update-po" ] }, diff --git a/features/makejson.feature b/features/makejson.feature index 0fde9cef..54e97fb4 100644 --- a/features/makejson.feature +++ b/features/makejson.feature @@ -7,7 +7,7 @@ Feature: Split PO files into JSON files. When I try `wp i18n make-json foo` Then STDERR should contain: """ - Error: Source file or directory does not exist! + Error: Source file or directory does not exist. """ And the return code should be 1 diff --git a/features/makemo.feature b/features/makemo.feature index 782035be..5fbb7014 100644 --- a/features/makemo.feature +++ b/features/makemo.feature @@ -7,7 +7,7 @@ Feature: Generate MO files from PO files When I try `wp i18n make-mo foo` Then STDERR should contain: """ - Error: Source file or directory does not exist! + Error: Source file or directory does not exist. """ And the return code should be 1 Scenario: Bail for destination being a file when source is a folder @@ -18,7 +18,7 @@ Feature: Generate MO files from PO files When I try `wp i18n make-mo foo test.mo ` Then STDERR should contain: """ - Error: Destination file not supported when source is a directory! + Error: Destination file not supported when source is a directory. """ And the return code should be 1 Scenario: Uses source folder as destination by default diff --git a/features/makephp.feature b/features/makephp.feature new file mode 100644 index 00000000..0fab416e --- /dev/null +++ b/features/makephp.feature @@ -0,0 +1,167 @@ +Feature: Generate PHP files from PO files + + Background: + Given an empty directory + + Scenario: Bail for invalid source directories + When I try `wp i18n make-php foo` + Then STDERR should contain: + """ + Error: Source file or directory does not exist. + """ + And the return code should be 1 + + Scenario: Uses source folder as destination by default + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin-de_DE.po 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 \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-plugin\n" + "Plural-Forms: nplurals=2; plural=(n != 1);\n" + + #: foo-plugin.js:15 + msgid "Foo Plugin" + msgstr "Foo Plugin" + """ + + When I run `wp i18n make-php foo-plugin` + Then STDOUT should contain: + """ + Success: Created 1 file. + """ + And the return code should be 0 + And the foo-plugin/foo-plugin-de_DE.l10n.php file should exist + + Scenario: Allows setting custom destination directory + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin-de_DE.po 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 \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-plugin\n" + "Plural-Forms: nplurals=2; plural=(n != 1);\n" + + #: foo-plugin.js:15 + msgid "Foo Plugin" + msgstr "Foo Plugin" + """ + + When I run `wp i18n make-php foo-plugin result` + Then STDOUT should contain: + """ + Success: Created 1 file. + """ + And the return code should be 0 + And the result/foo-plugin-de_DE.l10n.php file should exist + + Scenario: Does include headers + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin-de_DE.po 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 \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-plugin\n" + "Plural-Forms: nplurals=2; plural=(n != 1);\n" + + #: foo-plugin.js:15 + msgid "Foo Plugin" + msgstr "Foo Plugin" + """ + + When I run `wp i18n make-php foo-plugin` + Then STDOUT should contain: + """ + Success: Created 1 file. + """ + And the return code should be 0 + And STDERR should be empty + And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: + """ + 'language'=>'de_DE' + """ + And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: + """ + 'domain'=>'foo-plugin' + """ + And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: + """ + 'plural-forms'=>'nplurals=2; plural=(n != 1);' + """ + And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: + """ + 'messages'=>[''=>['Foo Plugin'=>['Foo Plugin']]] + """ + + Scenario: Does include translations + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin-de_DE.po 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 \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-plugin\n" + "Plural-Forms: nplurals=2; plural=(n != 1);\n" + + #: foo-plugin.js:15 + msgid "Foo Plugin" + msgstr "Bar Plugin" + """ + + When I run `wp i18n make-php foo-plugin` + Then STDOUT should contain: + """ + Success: Created 1 file. + """ + And the return code should be 0 + And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: + """ + 'messages'=>[''=>['Foo Plugin'=>['Bar Plugin']]] + """ diff --git a/features/makepot.feature b/features/makepot.feature index 59479d39..c918e625 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -7,7 +7,7 @@ Feature: Generate a POT file of a WordPress project When I try `wp i18n make-pot foo bar/baz.pot` Then STDERR should contain: """ - Error: Not a valid source directory! + Error: Not a valid source directory. """ And the return code should be 1 @@ -20,7 +20,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the wp-content/plugins/hello-world/languages/hello-world.pot file should exist @@ -35,7 +35,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the wp-content/plugins/hello-world/languages/hello-world.pot file should exist @@ -296,7 +296,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the foo-plugin/languages/foo-plugin.pot file should exist @@ -328,7 +328,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the foo-plugin.pot file should exist @@ -388,7 +388,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -595,7 +595,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -659,7 +659,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -681,7 +681,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Theme stylesheet detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the wp-content/themes/foobar/languages/foobar.pot file should exist @@ -722,7 +722,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should contain: """ @@ -751,7 +751,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should not contain: """ @@ -780,7 +780,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should not contain: """ @@ -808,7 +808,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should contain: """ @@ -832,7 +832,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should contain: """ @@ -859,7 +859,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should contain: """ @@ -887,7 +887,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should contain: """ @@ -915,7 +915,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should contain: """ @@ -942,7 +942,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should not contain: """ @@ -978,7 +978,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should contain: """ @@ -1523,7 +1523,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the wp-content/plugins/hello-world/languages/hello-world.pot file should exist @@ -1594,7 +1594,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the wp-content/plugins/hello-world/languages/hello-world.pot file should exist @@ -1650,7 +1650,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the wp-content/plugins/hello-world/languages/hello-world.pot file should exist @@ -1703,7 +1703,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the wp-content/plugins/hello-world/languages/hello-world.pot file should exist @@ -1821,7 +1821,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -1949,7 +1949,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -1983,7 +1983,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -2056,7 +2056,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -2146,7 +2146,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -2241,7 +2241,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin.pot file should contain: """ @@ -2281,7 +2281,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -2318,7 +2318,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -2352,7 +2352,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin.pot file should contain: """ @@ -2383,7 +2383,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin.pot file should contain: """ @@ -2418,7 +2418,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin.pot file should contain: """ @@ -2472,7 +2472,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the foo-plugin.pot file should contain: @@ -2532,7 +2532,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the foo-plugin.pot file should contain: @@ -2580,7 +2580,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should contain: """ @@ -2611,7 +2611,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should contain: """ @@ -2622,7 +2622,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should contain: """ @@ -2650,7 +2650,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Theme stylesheet detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should contain: """ @@ -2699,7 +2699,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Theme stylesheet detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty @@ -2763,7 +2763,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the foo-plugin.pot file should contain: @@ -2854,7 +2854,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should be empty And the foo-plugin.pot file should contain: @@ -2906,7 +2906,7 @@ Feature: Generate a POT file of a WordPress project When I try `wp i18n make-pot example-project result.pot --ignore-domain --debug` Then STDOUT should be: """ - Success: POT file successfully generated! + Success: POT file successfully generated. """ And STDERR should contain: """ @@ -2967,7 +2967,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Theme stylesheet detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the result.pot file should contain: """ @@ -3024,7 +3024,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Theme stylesheet detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the result.pot file should contain: """ @@ -3047,7 +3047,7 @@ Feature: Generate a POT file of a WordPress project When I run `wp i18n make-pot example-project result.pot --ignore-domain --package-name="Acme 1.2.3"` Then STDOUT should be: """ - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the result.pot file should contain: """ @@ -3070,7 +3070,7 @@ Feature: Generate a POT file of a WordPress project When I run `wp i18n make-pot example-project result.pot --ignore-domain --file-comment="Copyright (C) 2018 John Doe\nPowered by WP-CLI."` Then STDOUT should be: """ - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the result.pot file should contain: """ @@ -3104,7 +3104,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the contents of the result.pot file should match /^msgid/ @@ -3157,7 +3157,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -3283,7 +3283,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -3348,7 +3348,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -3453,7 +3453,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -3538,7 +3538,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Plugin file detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-plugin/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -3573,7 +3573,7 @@ Feature: Generate a POT file of a WordPress project When I try `wp i18n make-pot foo-theme --skip-theme-json` Then STDOUT should be: """ - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-theme/foo-theme.pot file should exist But the foo-theme/foo-theme.pot file should not contain: @@ -3614,7 +3614,7 @@ Feature: Generate a POT file of a WordPress project When I try `wp i18n make-pot foo-theme` Then STDOUT should be: """ - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-theme/foo-theme.pot file should exist And the foo-theme/foo-theme.pot file should contain: @@ -3671,7 +3671,7 @@ Feature: Generate a POT file of a WordPress project When I try `wp i18n make-pot foo-theme` Then STDOUT should be: """ - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-theme/foo-theme.pot file should exist And the foo-theme/foo-theme.pot file should contain: @@ -3720,7 +3720,7 @@ Feature: Generate a POT file of a WordPress project When I try `wp i18n make-pot foo-theme` Then STDOUT should be: """ - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-theme/foo-theme.pot file should exist And the foo-theme/foo-theme.pot file should contain: @@ -3762,7 +3762,7 @@ Feature: Generate a POT file of a WordPress project Then STDOUT should be: """ Theme stylesheet detected. - Success: POT file successfully generated! + Success: POT file successfully generated. """ And the foo-theme/foo-theme.pot file should exist And the foo-theme/foo-theme.pot file should contain: diff --git a/features/updatepo.feature b/features/updatepo.feature index 04895403..0e093e88 100644 --- a/features/updatepo.feature +++ b/features/updatepo.feature @@ -7,7 +7,7 @@ Feature: Update existing PO files from a POT file When I try `wp i18n update-po bar/baz.pot` Then STDERR should contain: """ - Error: Source file does not exist! + Error: Source file does not exist. """ And the return code should be 1 @@ -385,6 +385,6 @@ Feature: Update existing PO files from a POT file When I try `wp i18n update-po foo-plugin/foo-plugin.pot foo-plugin/test` Then STDERR should contain: """ - Error: Destination file/folder does not exist! + Error: Destination file/folder does not exist. """ And the return code should be 1 diff --git a/i18n-command.php b/i18n-command.php index b461e702..7187c325 100644 --- a/i18n-command.php +++ b/i18n-command.php @@ -30,4 +30,6 @@ WP_CLI::add_command( 'i18n make-mo', '\WP_CLI\I18n\MakeMoCommand' ); +WP_CLI::add_command( 'i18n make-php', '\WP_CLI\I18n\MakePhpCommand' ); + WP_CLI::add_command( 'i18n update-po', '\WP_CLI\I18n\UpdatePoCommand' ); diff --git a/src/MakeJsonCommand.php b/src/MakeJsonCommand.php index 12ba2b41..f54673de 100644 --- a/src/MakeJsonCommand.php +++ b/src/MakeJsonCommand.php @@ -86,7 +86,7 @@ public function __invoke( $args, $assoc_args ) { $source = realpath( $args[0] ); if ( ! $source || ( ! is_file( $source ) && ! is_dir( $source ) ) ) { - WP_CLI::error( 'Source file or directory does not exist!' ); + WP_CLI::error( 'Source file or directory does not exist.' ); } $destination = is_file( $source ) ? dirname( $source ) : $source; @@ -100,12 +100,8 @@ public function __invoke( $args, $assoc_args ) { WP_CLI::error( 'No valid keys found. No file was created.' ); } - // Two is_dir() checks in case of a race condition. - if ( ! is_dir( $destination ) - && ! mkdir( $destination, 0777, true ) - && ! is_dir( $destination ) - ) { - WP_CLI::error( 'Could not create destination directory!' ); + if ( ! is_dir( $destination ) && ! mkdir( $destination, 0777, true ) ) { + WP_CLI::error( 'Could not create destination directory.' ); } $result_count = 0; diff --git a/src/MakeMoCommand.php b/src/MakeMoCommand.php index b97bf3c7..a13eeabc 100644 --- a/src/MakeMoCommand.php +++ b/src/MakeMoCommand.php @@ -40,7 +40,7 @@ class MakeMoCommand extends WP_CLI_Command { public function __invoke( $args, $assoc_args ) { $source = realpath( $args[0] ); if ( ! $source || ( ! is_file( $source ) && ! is_dir( $source ) ) ) { - WP_CLI::error( 'Source file or directory does not exist!' ); + WP_CLI::error( 'Source file or directory does not exist.' ); } $destination = is_file( $source ) ? dirname( $source ) : $source; @@ -51,19 +51,15 @@ public function __invoke( $args, $assoc_args ) { // Destination is a file, make sure source is also a file if ( ! empty( $destination_pathinfo['filename'] ) && ! empty( $destination_pathinfo['extension'] ) ) { if ( ! is_file( $source ) ) { - WP_CLI::error( 'Destination file not supported when source is a directory!' ); + WP_CLI::error( 'Destination file not supported when source is a directory.' ); } $destination = $destination_pathinfo['dirname']; $custom_file_name = $destination_pathinfo['filename'] . '.' . $destination_pathinfo['extension']; } } - // Two is_dir() checks in case of a race condition. - if ( ! is_dir( $destination ) - && ! mkdir( $destination, 0777, true ) - && ! is_dir( $destination ) - ) { - WP_CLI::error( 'Could not create destination directory!' ); + if ( ! is_dir( $destination ) && ! mkdir( $destination, 0777, true ) ) { + WP_CLI::error( 'Could not create destination directory.' ); } if ( is_file( $source ) ) { diff --git a/src/MakePhpCommand.php b/src/MakePhpCommand.php new file mode 100644 index 00000000..3c3b559c --- /dev/null +++ b/src/MakePhpCommand.php @@ -0,0 +1,86 @@ + + * : Path to an existing PO file or a directory containing multiple PO files. + * + * [] + * : Path to the destination directory for the resulting PHP files. Defaults to the source directory. + * + * ## EXAMPLES + * + * # Create PHP files for all PO files in the current directory. + * $ wp i18n make-php . + * Success: Created 3 files. + * + * # Create a PHP file from a single PO file in a specific directory. + * $ wp i18n make-php example-plugin-de_DE.po languages + * Success: Created 1 file. + * + * @when before_wp_load + * + * @throws WP_CLI\ExitException + */ + public function __invoke( $args, $assoc_args ) { + $source = realpath( $args[0] ); + if ( ! $source || ( ! is_file( $source ) && ! is_dir( $source ) ) ) { + WP_CLI::error( 'Source file or directory does not exist.' ); + } + + $destination = is_file( $source ) ? dirname( $source ) : $source; + if ( isset( $args[1] ) ) { + $destination = $args[1]; + } + + if ( ! is_dir( $destination ) && ! mkdir( $destination, 0777, true ) ) { + WP_CLI::error( 'Could not create destination directory.' ); + } + + if ( is_file( $source ) ) { + $files = [ new SplFileInfo( $source ) ]; + } else { + $files = new IteratorIterator( new DirectoryIterator( $source ) ); + } + + $result_count = 0; + /** @var DirectoryIterator $file */ + foreach ( $files as $file ) { + if ( 'po' !== $file->getExtension() ) { + continue; + } + + if ( ! $file->isFile() || ! $file->isReadable() ) { + WP_CLI::warning( sprintf( 'Could not read file %s', $file->getFilename() ) ); + continue; + } + + $file_basename = basename( $file->getFilename(), '.po' ); + $destination_file = "{$destination}/{$file_basename}.l10n.php"; + + $translations = Translations::fromPoFile( $file->getPathname() ); + if ( ! PhpArrayGenerator::toFile( $translations, $destination_file ) ) { + WP_CLI::warning( sprintf( 'Could not create file %s', $destination_file ) ); + continue; + } + + ++$result_count; + } + + WP_CLI::success( sprintf( 'Created %d %s.', $result_count, Utils\pluralize( 'file', $result_count ) ) ); + } +} diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index 14aea975..75dfd8ff 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -297,10 +297,10 @@ public function __invoke( $args, $assoc_args ) { } if ( ! PotGenerator::toFile( $translations, $this->destination ) ) { - WP_CLI::error( 'Could not generate a POT file!' ); + WP_CLI::error( 'Could not generate a POT file.' ); } - WP_CLI::success( 'POT file successfully generated!' ); + WP_CLI::success( 'POT file successfully generated.' ); } /** @@ -331,7 +331,7 @@ public function handle_arguments( $args, $assoc_args ) { $ignore_domain = Utils\get_flag_value( $assoc_args, 'ignore-domain', false ); if ( ! $this->source || ! is_dir( $this->source ) ) { - WP_CLI::error( 'Not a valid source directory!' ); + WP_CLI::error( 'Not a valid source directory.' ); } $this->main_file_data = $this->get_main_file_data(); @@ -371,12 +371,8 @@ public function handle_arguments( $args, $assoc_args ) { WP_CLI::debug( sprintf( 'Destination: %s', $this->destination ), 'make-pot' ); - // Two is_dir() checks in case of a race condition. - if ( ! is_dir( dirname( $this->destination ) ) - && ! mkdir( dirname( $this->destination ), 0777, true ) - && ! is_dir( dirname( $this->destination ) ) - ) { - WP_CLI::error( 'Could not create destination directory!' ); + if ( ! is_dir( dirname( $this->destination ) ) && ! mkdir( dirname( $this->destination ), 0777, true ) ) { + WP_CLI::error( 'Could not create destination directory.' ); } if ( isset( $assoc_args['merge'] ) ) { diff --git a/src/PhpArrayGenerator.php b/src/PhpArrayGenerator.php new file mode 100644 index 00000000..28c1c575 --- /dev/null +++ b/src/PhpArrayGenerator.php @@ -0,0 +1,104 @@ + false, + ]; + + /** + * {@inheritdoc} + */ + public static function toString( Translations $translations, array $options = [] ) { + $array = static::generate( $translations, $options ); + + $language = $translations->getLanguage(); + if ( null !== $language ) { + $array['language'] = $language; + } + + $headers = [ + 'X-Generator' => 'x-generator', + ]; + + foreach ( $translations->getHeaders() as $name => $value ) { + if ( isset( $headers[ $name ] ) ) { + $array[ $headers[ $name ] ] = $value; + } + } + + return ' $arr The array being evaluated. + * @return bool True if array is a list, false otherwise. + */ + private static function array_is_list( array $arr ) { + if ( function_exists( 'array_is_list' ) ) { + return array_is_list( $arr ); + } + + if ( ( array() === $arr ) || ( array_values( $arr ) === $arr ) ) { + return true; + } + + $next_key = -1; + + foreach ( $arr as $k => $v ) { + if ( ++$next_key !== $k ) { + return false; + } + } + + return true; + } + + /** + * Outputs or returns a parsable string representation of a variable. + * + * Like {@see var_export()} but "minified", using short array syntax + * and no newlines. + * + * @since 4.0.0 + * + * @param mixed $value The variable you want to export. + * @return string The variable representation. + */ + private static function var_export( $value ) { + if ( ! is_array( $value ) ) { + return var_export( $value, true ); + } + + $entries = array(); + + $is_list = self::array_is_list( $value ); + + foreach ( $value as $key => $val ) { + $entries[] = $is_list ? self::var_export( $val ) : var_export( $key, true ) . '=>' . self::var_export( $val ); + } + + return '[' . implode( ',', $entries ) . ']'; + } +} diff --git a/src/UpdatePoCommand.php b/src/UpdatePoCommand.php index 556236f5..e739e18a 100644 --- a/src/UpdatePoCommand.php +++ b/src/UpdatePoCommand.php @@ -34,7 +34,7 @@ class UpdatePoCommand extends WP_CLI_Command { public function __invoke( $args, $assoc_args ) { $source = realpath( $args[0] ); if ( ! $source || ! is_file( $source ) ) { - WP_CLI::error( 'Source file does not exist!' ); + WP_CLI::error( 'Source file does not exist.' ); } $destination = dirname( $source ); @@ -44,7 +44,7 @@ public function __invoke( $args, $assoc_args ) { } if ( ! file_exists( $destination ) ) { - WP_CLI::error( 'Destination file/folder does not exist!' ); + WP_CLI::error( 'Destination file/folder does not exist.' ); } if ( is_file( $destination ) ) {