Skip to content

ESLint: Enforce stricter translator comment placeholder matching #70458

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

Open
wants to merge 14 commits into
base: trunk
Choose a base branch
from

Conversation

USERSATOSHI
Copy link
Contributor

@USERSATOSHI USERSATOSHI commented Jun 17, 2025

What?

Closes #70453

This Pull Request aims to provide stricter translators comments via eslint.
Changes includes addition of placeholders in translator comments if the placeholders are being used in the below code.

Supports

Placeholders

  • Unnamed: %s, %d and %f ( with and without colon )
  • Positional: 1, %1, %1{s|d|f} and %1${s|d|f}
  • Named: named, %named

Missing Placeholders

  • Allows eslint to flag placeholders that aren't mentioned in translator comments.

Extra/Outdated Placeholders

  • Allows eslint to flag placeholders that are present in comment but not in code
  • Supports Unnamed and Positional Placeholder ( as of right now due to named: being false positive in some cases )

Why?

Current eslint rule only checks if translators comment is present or not which works but can be improved to force stricter checks like adding correct placeholders and removing extra placeholders.

This Pull Request aims this by checking the type of placeholder for unnamed, named and indexed and use that as inference to match with translators comments.

How?

This PR introduces 2 new helpers

extractPlaceholders: This extracts the placeholder being used in sprintf
extractTranslatorKeys: This extracts the placeholder used in translator comments

and a new Regex:
REGEXP_COMMENT_PLACEHOLDER: This regex matches the placeholders present in translators comments.

Using these 2 functions we can extract the placeholders used in sprintf and the one in comments and match against each other to find missing and extra placeholders.

Testing Instructions

  • Run npm run test:unit packages/eslint-plugin/rules/__tests__/i18n-translator-comments.js
  • See all checks being passed

Screenshots or screencast

Missing Placeholders in translator comments

image

Extra placeholder

image

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Jun 17, 2025
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @USERSATOSHI! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@USERSATOSHI USERSATOSHI changed the title Try/strict translator comments eslint ESLint: Enforce stricter translator comment placeholder matching Jun 17, 2025
@t-hamano t-hamano added Internationalization (i18n) Issues or PRs related to internationalization efforts [Tool] ESLint plugin /packages/eslint-plugin labels Jun 18, 2025
@swissspidy swissspidy added the [Type] Enhancement A suggestion for improvement. label Jun 18, 2025
@USERSATOSHI USERSATOSHI marked this pull request as ready for review June 23, 2025 18:18
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: USERSATOSHI <[email protected]>
Co-authored-by: swissspidy <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository Internationalization (i18n) Issues or PRs related to internationalization efforts [Tool] ESLint plugin /packages/eslint-plugin [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ESLint: make translator-comments rule more strict
3 participants