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

Add more i18n sniffs #567

Merged
merged 7 commits into from
Jun 22, 2016
Merged

Add more i18n sniffs #567

merged 7 commits into from
Jun 22, 2016

Conversation

deBhal
Copy link
Contributor

@deBhal deBhal commented Jun 8, 2016

This PR is to add another test to the I18nSniff.php to ensure that multiple placeholders are orderable.

e.g. from the PHP: sprintf page:
__( 'The %s contains %d monkeys' ); is a problem because the variables will need to be the other order in some languages, and the string should be:
__( 'The %1$s contains %2$d monkeys' );

The PR includes tests and a fixer.

One thing worth discussing is that the fixer could potentially get it wrong if:

  1. It's a pluralised string
  2. The order of the placeholders is different between the two strings
  3. The placeholders are unordered.

The test file gives an example of the sort of string I'm talking about:
_n( 'There is %d monkey in the %s', 'In the %s there are %d monkeys', $number, 'my-slug' );

My assessment here is that this is a bug in the code, and this sniff won't fix it, but it also won't make it any worse than it already was. I was tempted to not apply the fix in plural cases, but think the good outweighs the bad, so for now the fix does run on plurals.

@JDGrimes
Copy link
Contributor

JDGrimes commented Jun 8, 2016

This looks great, thanks @deBhal! In regard to the plurals and placeholders in the wrong order, I don't think that we really need to worry about that. As you say, it is a bug in the plugin itself. Though I suppose we could check if the order is the same in both of the strings, and report in error if it isn't. In some cases it would be undetectable though, if the user was just using %s both times instead of %d.

@deBhal deBhal changed the title Add test for unordered placeholders Add more I18n tests Jun 9, 2016
@deBhal
Copy link
Contributor Author

deBhal commented Jun 9, 2016

Thanks for the quick feedback!

I've added another new sniff for the singular problem described in https://codex.wordpress.org/I18n_for_WordPress_Developers#Plurals
_n( 'This is a subtle problem', 'These are subtle problems', $number ).
"Singular" means exactly one in English, but that's not the case in several languages.

I've also added a warning for mismatched placeholders, which will catch at least some of the cases we talked about above.

I'm adding these to this PR rather than opening separate ones because the way the tests work would force annoying re-edits if they came from separate PRs.

Quick question: I've used $this->compare_single_and_plural_arguments(), but the existing code uses call_user_func() instead of calling a member function directly. Is there a reason for this? PHP versions or something?

@deBhal deBhal changed the title Add more I18n tests Add more i18n sniffs Jun 9, 2016
$token = $tokens[ $stack_ptr ];
static $only_once = true;
if ( $only_once ) {
// echo( '$tokens = ' . var_export( $tokens, true ) . PHP_EOL ); //DEBUG
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debug code here (and below on line 80) can be removed. 😃

(And just FYI, if you have never used an IDE with support for xdebug built in, you are truly missing out! 😃)

@JDGrimes
Copy link
Contributor

JDGrimes commented Jun 9, 2016

"Singular" means exactly one in English, but that's not the case in several languages.

Thanks for this, I didn't realize that (or maybe had just forgotten).

Quick question: I've used $this->compare_single_and_plural_arguments(), but the existing code uses call_user_func() instead of calling a member function directly. Is there a reason for this? PHP versions or something?

I'm pretty sure that this is just a leftover from when the code called one of several different methods in that spot, but it has been refactored to just call that one now. So there is really no reason for call_user_func() to be used there anymore, it is just a relic.

@JDGrimes JDGrimes added this to the 0.10.0 milestone Jun 9, 2016
@@ -179,6 +183,14 @@ public function process( PHP_CodeSniffer_File $phpcs_file, $stack_ptr ) {
}
call_user_func( array( $this, 'check_argument_tokens' ), $phpcs_file, $argument_assertion_context );
}

// for _n*() calls, compare the singular and plural strings
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capital to start comments, and a full stop to end please.

@deBhal
Copy link
Contributor Author

deBhal commented Jun 12, 2016

Thanks, @GaryJones. I've fixed that comment and several others, and squashed the feedback changes into logical commits.

@deBhal
Copy link
Contributor Author

deBhal commented Jun 15, 2016

I've added one more test that checks for translations that are just placeholders ( '%s just wastes everyone's time).

I'm not expecting to add any more tests now, but I'll still respond to feedback.

@JDGrimes
Copy link
Contributor

Looks good to me.

@GaryJones, would you like to review (and then merge)?

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.

4 participants