wp-cli / i18n-command

Provides internationalization tools for WordPress projects.
MIT License
96 stars 52 forks source link

`update-po`: Empty glob result results in PHP fatal error #332

Closed strarsis closed 1 year ago

strarsis commented 2 years ago

Feature Request

Describe your use case and the problem you are facing When the glob passed to wp i18n update-po results in an empty result set (e.g. a new project with no *.po files yet in the languages directory), update-po will crash with fatal errors: wp i18n update-po ./resources/lang/sage.pot ./resources/lang/*.po:

PHP Fatal error:  Uncaught UnexpectedValueException: DirectoryIterator::__construct(./resources/lang/*.po): failed to open dir: No such file or directory in /home/build/.wp-cli/packages/vendor/wp-cli/i18n-command/src/UpdatePoCommand.php:49
Stack trace:
#0 /home/build/.wp-cli/packages/vendor/wp-cli/i18n-command/src/UpdatePoCommand.php(49): DirectoryIterator->__construct()
#1 [internal function]: WP_CLI\I18n\UpdatePoCommand->__invoke()
#2 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php(100): call_user_func()
#3 [internal function]: WP_CLI\Dispatcher\CommandFactory::WP_CLI\Dispatcher\{closure}()
#4 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php(491): call_user_func()
#5 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(417): WP_CLI\Dispatcher\Subcommand->invoke()
#6 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(440): WP_CLI\Runner->run_command()
#7 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1 in /home/build/.wp-cli/packages/vendor/wp-cli/i18n-command/src/UpdatePoCommand.php on line 49

Describe the solution you'd like No fatal PHP error, but an explanative, user-friendly message, e.g. Notice: Updated 0 file. or Notice: No files found/matched for updating.. The exit code may be different than the one when files were actually updated.

There is one practical use case where this is especially relevant: For handling translations (e.g. in a theme), scripts (npm scripts) are often used. When there are no translations (PO files) yet, the PO update script causes a scary looking error instead just skipping as nothing was found (the glob itself is valid).

swissspidy commented 2 years ago

Good catch! Thanks for reporting.

elenachavdarova commented 1 year ago

Pull request: https://github.com/wp-cli/i18n-command/pull/356