Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes I18NSniff for multi-line gettext calls #1962

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

sdnunca
Copy link

@sdnunca sdnunca commented Nov 29, 2020

Currently, there is an issue with the PHPCS Sniff that validates translator comments.
Translator comments usually need to be placed on the line above the gettext call.

However, in the case of multi-line translation function calls, the comment needs to be placed between the line of the function call and the first string.

Here's an example of possible comment locations:

image

This PR changes the sniff implementation to also take into account the multi-line calls edge case.

@jrfnl
Copy link
Member

jrfnl commented Nov 29, 2020

However, in the case of multi-line translation function calls, the comment needs to be placed between the line of the function call and the first string.

@sdnunca What do you base this on ?

The sniff implementation as-is is based on the PO specs + the implementation of the most commonly used tools. None of which agree with your statement.

@sdnunca
Copy link
Author

sdnunca commented Nov 29, 2020

@jrfnl Thanks for looking into this so quickly. I've encountered this extracting comments with xgettext, as you can see in the screenshot above.

If the call is multi line and the comment is on the line above, the text is not extracted.

@jrfnl
Copy link
Member

jrfnl commented Nov 29, 2020

Related #742

@sdnunca
Copy link
Author

sdnunca commented Nov 29, 2020

I'm not sure if this is particular to gettext but the use-case is also mentioned in their documentation: :
https://www.gnu.org/software/gettext/manual/gettext.html#Operation-mode

image

@jrfnl
Copy link
Member

jrfnl commented Nov 29, 2020

Ok, interesting. Not sure if that was changed between the original implementation and now, but never mind that.

For this PR to be considered, tests will need to be run with all commonly used tooling to extract translatable strings, like makepot, POEdit, the grunt tooling commonly used, the tooling used by WP Core itself etc.

Someone will need to create an overview of how each of those tools handle the various situations highlighted in the screenshot above. Once that is done, it can be evaluated whether this PR would improve or break compatibility with the various tools.

Only if it would turn out that it would improve compatibility, a full code review of this PR will be done.

@sdnunca Please don't take this push-back the wrong way. I appreciate your initiative and interest in improving WPCS.
However, as WPCS is used by so many projects, we really need to be diligent about changes like this which could break existing integrations.

It might have been better to initially open an issue about this to discuss the problem instead of opening a PR.

@sdnunca
Copy link
Author

sdnunca commented Nov 29, 2020

@jrfnl Again, thank you so much for taking the time to look into this.

It might have been better to initially open an issue about this to discuss the problem instead of opening a PR.

I'll definitely keep that in mind for the future :) . I'll further investigate and get back with my findings on compatibility.

@sdnunca
Copy link
Author

sdnunca commented Dec 10, 2020

Here's the result of my testing.

List of tools checked:

  • wp i18n make-pot - also used by WordPress.org
  • POEdit (and any other tool based on gettext, such as Loco Translate)
  • grunt tooling → grunt-wp-i18n → node-wp-i18n → makepot.php

Test file:

<?php
/*
 * Plugin Name: Test plugin
 * Description: Test description
 * Text Domain: test-plugin
 */

/* translators: number of monkeys, location 1. */
__( 'There are %1$d monkeys in the %2$s 1'
); // Ok, the string is on the same line as the gettext call

__( /* translators: number of monkeys, location 2.*/
	'There are %1$d monkeys in the %2$s 2'
); // OK, comment is on the same line as the gettext call

/* translators: number of monkeys, location 3. */
_n(
	'There are %1$d monkeys in the %2$s 3',
	'There are %1$d monkeys in the %2$s 3'
); // Bad, comment will not be extracted

_n(
/* translators: number of monkeys, location 4. */
	'There are %1$d monkeys in the %2$s 4',
	'There are %1$d monkeys in the %2$s 4'
); // Ok, comment will be extracted

function test_plugin_init() {
    load_plugin_textdomain( 'test-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); 
}
add_action( 'plugins_loaded', 'test_plugin_init' );

1. wp i18n make-pot (also used by core)

Command:
image

Result:
image

Conclusion: All locations are extracted by wp i18n make-pot

2. POEdit

image

Results:
image

image

Locations #1, #2, #4 are successfully extracted ✅.

#3 is NOT ❌.

Conclusion: The current implementation is not working with POEdit.

3. Grunt tooling

Testing grunt-wp-i18n

Results:
image

Locations #2 and #4 are NOT extracted ❌.

Conclusion: grunt-wp-i18n uses the old makepot.php implementation which is no longer used by WordPress.org

Conclusions

As a conclusion for the whole experiment, we will lose compatibility with makepot.php implementation from above and gain compatibility with xgettext based tools.

grunt-wp-i18n is planning to switch to wp i18n make-pot however those efforts have been in progress for a while.

Please let me know if there are any other tools you feel need testing.

@sdnunca
Copy link
Author

sdnunca commented Jan 11, 2021

Hey @jrfnl ,
Just a quick ping :), whenever you get the chance I'd really appreciate your thoughts on the results above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants