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

sprintf ESLint rule does not check for numbered placeholders #20554

Closed
swissspidy opened this issue Feb 29, 2020 · 1 comment · Fixed by #20574
Closed

sprintf ESLint rule does not check for numbered placeholders #20554

swissspidy opened this issue Feb 29, 2020 · 1 comment · Fixed by #20574
Assignees
Labels
Internationalization (i18n) Issues or PRs related to internationalization efforts [Status] In Progress Tracking issues with work in progress [Tool] ESLint plugin /packages/eslint-plugin [Type] Bug An existing feature does not function as intended

Comments

@swissspidy
Copy link
Member

swissspidy commented Feb 29, 2020

Describe the bug

While working on #19777 I noticed that the @wordpress/valid-sprintf rule does not catch the following issue:

sprintf(
	/* translators: 1: number of blocks. 2: average rating. */
	_n(
		'This author has %d block, with an average rating of %d.',
		'This author has %d blocks, with an average rating of %d.',
		authorBlockCount
	),
	authorBlockCount,
	authorBlockRating
)

As you can see, there are two occurrences of %d in each string, which makes it impossible for translators to change the order of placeholders in the sentence.

Expected behavior

It should warn about multiple placeholders being found, and should suggest using numbered placeholders instead:

sprintf(
	/* translators: 1: number of blocks. 2: average rating. */
	_n(
		'This author has %1$d block, with an average rating of %2$d.',
		'This author has %1$d blocks, with an average rating of %2$d.',
		authorBlockCount
	),
	authorBlockCount,
	authorBlockRating
)
@swissspidy swissspidy added [Type] Bug An existing feature does not function as intended [Tool] ESLint plugin /packages/eslint-plugin labels Feb 29, 2020
@swissspidy swissspidy assigned swissspidy and unassigned swissspidy Feb 29, 2020
@swissspidy
Copy link
Member Author

The rule can actually be much improved here by simply following what WPCS does in PHP: https://github.com/WordPress/WordPress-Coding-Standards/blob/2f927b0ba2bfcbffaa8f3251c086e109302d6622/WordPress/Sniffs/WP/I18nSniff.php

That one contains two different regex for catching unordered and ordered placeholders, making it easy to verify whether the code is wrong. Shouldn't be difficult to port that part.

@swissspidy swissspidy self-assigned this Feb 29, 2020
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Mar 2, 2020
@swissspidy swissspidy added the Internationalization (i18n) Issues or PRs related to internationalization efforts label Mar 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Internationalization (i18n) Issues or PRs related to internationalization efforts [Status] In Progress Tracking issues with work in progress [Tool] ESLint plugin /packages/eslint-plugin [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant