i18n-command icon indicating copy to clipboard operation
i18n-command copied to clipboard

wp i18n make-pot: Support PHP8 named parameters

Open lichtmetzger opened this issue 1 year ago • 1 comments

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');

lichtmetzger avatar Feb 14 '24 14:02 lichtmetzger

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.

swissspidy avatar Feb 14 '24 14:02 swissspidy