-
-
Notifications
You must be signed in to change notification settings - Fork 487
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
Add checking for translators comments to the I18n sniff. #742
Conversation
$comment_text = trim( $tokens[ $previous_comment ]['content'] ); | ||
|
||
// If it's multi-line /* */ comment, collect all the parts. | ||
if ( '*/' === substr( $comment_text, -2 ) && '/*' !== substr( $comment_text, 0, 2 ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this "collect all parts from a multi-line comment" be abstracted into a re-usable method in the base class?
Also, how does it handle something like:
/*
* This is a multiline comment,
* But it also has * at the start
of some lines ;-)
*/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this "collect all parts from a multi-line comment" be abstracted into a re-usable method in the base class?
It could, but I've not seen any other sniff within the WPCS which would need it. I suggest this can be done later if/when another sniff would need something similar.
Also - the snippet only handles multi-line /* */ type comments, no other type and (currently) does not care about how the lines are added together as for this sniff, only the start of the text is interesting, so if we would want it to be more generic, it will need some tweaking.
how does it handle something like
<code snippet>
It handles that fine, just tested it to be sure + added an additional unit test to that effect.
printf( | ||
/* translators: number of monkeys, location. */ | ||
__( 'There are %1$d monkeys in the %2$s', 'my-slug' ), | ||
intval( $number ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
intval
tends to be PHP 4 - (int)
would be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
Code generally looks good - great work. Pinging @ramiy, as I think he does a lot with translator comments, and can offer feedback on any missed cases / false positives. |
Implemented based on the following specs: * Only one error should be thrown per gettext call. * A translators comment is only required when (one of) the text string(s) contains placeholders. * It's ok for there to be a translators comment if the text string(s) does not contain placeholders. * The translators comment must be directly above the gettext call. * If the comment is not on the *line* directly above the gettext call, there should only be whitespace lines between the comment and the line containing the gettext call. * The translators comment must be in `//` or `/* */` format, docblocks are not allowed. * A translators comment must start with `translators:` * The check defaults to throwing a warning. If the `extra` ruleset is enabled, the error level will change to `error`.
73fd741
to
ed0eb5d
Compare
Rebased after #733 to allow unit tests to pass. |
Looks good!!! As for: The translators comment must be directly above the gettext call. And the examples on the testing file: printf(
/* translators: number of monkeys, location. */
__( 'There are %1$d monkeys in the %2$s', 'my-slug' ),
(int) $number,
esc_html( $string )
); // Ok.
/* translators: number of monkeys, location. */
printf(
__( 'There are %1$d monkeys in the %2$s', 'my-slug' ),
(int) $number,
esc_html( $string )
); // Bad - comment not directly before line containing the gettext call. Although I prefer to add comments above the gettext call, WordPress core has tons of translators comments above the |
Hi @ramiy Thanks for looking this over!
I've set that quite strict as in my personal experience with PoEdit, the comments don't get picked up otherwise. Not sure how well GlotPress/makepot does in picking up those comments if they are not on the line directly above (I expect just as badly as PoEdit uses makepot underneath the hood). If it's on one line, it should be fine, see the last unit test: /* translators: number of monkeys, location. */
printf( __( 'There are %1$d monkeys in the %2$s', 'my-slug' ), intval( $number ), esc_html( $string ) ); // Ok - comment is directly before line containing the gettext call. And if the gettext call is on the same line as the /* translators: number of monkeys, location. */
printf( __(
'There are %1$d monkeys in the %2$s', 'my-slug' ),
intval( $number ),
esc_html( $string )
); // Ok - comment is directly before line containing the gettext call. |
I've just read through a number of trac tickets around this, starting from https://core.trac.wordpress.org/ticket/39116 and am left with one question: should |
I don't think so because PoEdit had to add this functionality.
I found this in one of my emails with the PoEdit Developer. |
Edit: Edit: Found it, it wasn't capitalisation I was thinking of, I'll post a follow up email for repo watchers |
Via https://core.trac.wordpress.org/ticket/30603 Translator comments must be on a new line, and the line begin with either |
Comments with lowercase I'd love to allow just capitalised |
Just a general FYI: GlotPress doesn't generate POT files from source files, that's why we have makepot. makepot doesn't care where a translator comment is. If there is a comment it stores it and adds the comment to the next gettext call, no matter how many lines there are between the comment and the actual gettext call.
There is no need for that, see also https://make.wordpress.org/core/handbook/contribute/code-refactoring/. |
It did for the automatic WP project setup which it now offers out of the box, but before that they already supported it as long as you configured it right: Oh and it also corrects me regarding what they use under the hood - not |
Includes additional unit tests.
Based on the feedback I've made the checking for the
Based on this: should we loosen up the check for a translators comment on the line directly above the gettext call ?
Last question as asked before in #423 (comment):
I can imagine we should have a whitelist comment as - if I remember correctly - if the same phrase is used several times, it only needs a translators comment once. Opinions ? |
Poedit developer here. That’s correct, and the limitations come from the de-facto standard GNU gettext tools, not Poedit itself: there can only be one such tag (further
Allow me to add another reason: compatibility with |
@vslavik Thank you for your input & clarification! Very useful. |
Anyone still wants to have a look at this ? If not, I propose we merge it tomorrow. |
Implemented based on the following specs:
//
or/* */
format, docblocks are not allowed.translators:
extra
ruleset is enabled, the error level will change toerror
.Fixes #423
I've added a sniff property
$check_translator_comments
to enable us to unit test the other checks within the I18n sniff without getting bogged down by errors thrown by the translators comment check.This is a public property, but is only intended to be used in combination with the unit tests.
It could be used in custom rulesets too, but this shouldn't be needed as the translator comment checks can be excluded via their error codes.
Regarding the travis run failure:
The unit tests against PHPCS 2.7.0 are failing because the ruleset is erroneously taken into account when running the unit tests, which leads to PHPCS throwing an
error
instead of the expectedwarning
.This was fixed in PHPCS 2.7.1.
This only affects the unit tests however, not the functioning of the sniff itself.
Related: #733, #696 and squizlabs/PHP_CodeSniffer#1177