wp-cli / i18n-command

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

wp i18n make-pot: Support PHP8 named parameters #386

Open lichtmetzger opened 5 months ago

lichtmetzger commented 5 months ago

PHP8 has been out since 2020, but the make-pot command still doesn't support named parameters in __() function calls. It would be nice to have this feature so we can write more modern code in our plugins and themes.

This works: __('Oops! Something went wrong!', 'wp-plugin-textdomain');

This does not work: __(text: 'Oops! Something went wrong!', domain: 'wp-plugin-textdomain');

swissspidy commented 5 months ago

This command uses an external library for scanning PHP files and extracting strings from them, see https://github.com/php-gettext/Gettext. Such support would need to be added there first if not already present. At first glance it doesn't seem like it, at least there aren't any tests. So this would require an upstream feature request first.

Second, and probably most important, it is worth noting that WordPress does not officially support named parameters and does not guarantee that parameters won't be renamed. Use at your own risk.

This risk is also relevant for string extraction. Right now the __() function in WordPress has this signature: function __( $text, $domain = 'default' ) {}. We would have to hardcode this signature in our code base. But should WordPress ever change the signature (again, there is no guarantee this won't happen), we would have to update it as well.

Given the lack of support from the WordPress side, and the (possible) limitation from the upstream library side, I don't see us supporting extraction with named parameters anytime soon.