diff --git a/README.md b/README.md index 5782b1c..51a941a 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 54d6b5c..54bb1b2 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 0fde9ce..54e97fb 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 782035b..5fbb701 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 0000000..0fab416 --- /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 5b7bcfc..902d4de 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 @@ -330,7 +330,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 @@ -362,7 +362,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 @@ -422,7 +422,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: @@ -629,7 +629,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: @@ -693,7 +693,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: @@ -715,7 +715,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 @@ -756,7 +756,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: """ @@ -785,7 +785,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: """ @@ -814,7 +814,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: """ @@ -842,7 +842,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: """ @@ -866,7 +866,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: """ @@ -893,7 +893,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: """ @@ -921,7 +921,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: """ @@ -949,7 +949,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: """ @@ -976,7 +976,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: """ @@ -1012,7 +1012,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: """ @@ -1557,7 +1557,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 @@ -1628,7 +1628,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 @@ -1684,7 +1684,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 @@ -1737,7 +1737,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 @@ -1855,7 +1855,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: @@ -2017,7 +2017,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: @@ -2090,7 +2090,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: @@ -2180,7 +2180,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: @@ -2275,7 +2275,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: """ @@ -2315,7 +2315,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/foo-plugin.pot file should exist And the foo-plugin/foo-plugin.pot file should contain: @@ -2386,7 +2386,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: """ @@ -2417,7 +2417,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: """ @@ -2452,7 +2452,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: """ @@ -2506,7 +2506,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: @@ -2566,7 +2566,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: @@ -2614,7 +2614,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: """ @@ -2645,7 +2645,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: """ @@ -2656,7 +2656,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: """ @@ -2684,7 +2684,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: """ @@ -2733,7 +2733,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 @@ -2797,7 +2797,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: @@ -2888,7 +2888,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: @@ -2940,7 +2940,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: """ @@ -3001,7 +3001,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: """ @@ -3058,7 +3058,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: """ @@ -3081,7 +3081,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: """ @@ -3104,7 +3104,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: """ @@ -3138,7 +3138,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/ @@ -3191,7 +3191,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: @@ -3317,7 +3317,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: @@ -3382,7 +3382,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: @@ -3487,7 +3487,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: @@ -3572,7 +3572,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: @@ -3607,7 +3607,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: @@ -3648,7 +3648,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: @@ -3705,7 +3705,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: @@ -3754,7 +3754,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: @@ -3796,7 +3796,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 0489540..0e093e8 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 b461e70..7187c32 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 12ba2b4..f54673d 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 b97bf3c..a13eeab 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 0000000..3c3b559 --- /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 e022775..7fe9940 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -302,10 +302,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.' ); } /** @@ -336,7 +336,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(); @@ -376,12 +376,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 0000000..28c1c57 --- /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 556236f..e739e18 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 ) ) {