Skip to content

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 <tusharbharti@git.wordpress.org>
Co-authored-by: swissspidy <swissspidy@git.wordpress.org>

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

@swissspidy swissspidy self-requested a review July 2, 2025 09:22
Copy link
Member

@swissspidy swissspidy left a comment

Choose a reason for hiding this comment

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

If we can avoid the escaping workaround then this looks good to me!

@swissspidy swissspidy enabled auto-merge (squash) July 3, 2025 11:59
@swissspidy swissspidy merged commit b187b29 into WordPress:trunk Jul 3, 2025
59 of 61 checks passed
@github-actions github-actions bot added this to the Gutenberg 21.2 milestone Jul 3, 2025
@tbradsha
Copy link

tbradsha commented Aug 7, 2025

I'll note that I came across two false positives that I think are a result of this PR:

  1. It doesn't seem to like float-with-precision placeholders like %1.1f
// translators: %1.1f: numeric amount of GB used.
const test = __( 'Using %1.1fGB', 'some-pkg' );

Lint → Translator comment missing description(s) for placeholder(s): %1.1f.
  1. It seems to think "6:00AM" is a named placeholder or something:
// translators: %s: a formatted time (e.g. 6:00AM).
const test = __( 'Alarm is set for %s', 'some-pkg' );

Lint → Translator comment has extra placeholder(s): 6.

And this is a false negative (no error emitted):

// translators: 1
__( 'Using %1$dGB', 'some-pkg' ),

@USERSATOSHI
Copy link
Contributor Author

USERSATOSHI commented Aug 7, 2025

I believe 3rd case was left due to comments like

// translators: address for 1
  // translators: %s: a formatted time (e.g. 6:00AM).
  const test = __( 'Alarm is set for %s', 'some-pkg' );

  // Lint → Translator comment has extra placeholder(s): 6.

hmm, this case seems to be due to regex not making it mandatory for space after :, a work around in meantime would be to escape :

  // translators: %1.1f: numeric amount of GB used.
  const test = __( 'Using %1.1fGB', 'some-pkg' );

  // Lint → Translator comment missing description(s) for placeholder(s): %1.1f.

As I believe, @tannin/sprintf doesn't support minimum width ( padding ), supporting this wouldn't be viable as it will not be possible to use in sprintf.

I also found other types which are allowed but not typed supported like 1$.1f, %(id).5d etc in translator comments and sprintf.

I will do a follow up PR for both sprintf and eslint rule to support this.

Thank you for informing.
Regards

cc: @tbradsha, @swissspidy

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
4 participants