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

Reusable block: Pluralize the message "Convert to regular blocks" depending on the number of blocks contained. #45819

Conversation

janusqa
Copy link
Contributor

@janusqa janusqa commented Nov 16, 2022

Reusable block: Pluralize the message "Convert to regular blocks" depending on the number of blocks contained

What?

The action "Convert to regular blocks" is in plural form for all cases, including when the reusable block only contains a single block. It would be great if we detect the number of blocks contained and pluralize the message:

  • Reusable block only contains a single block: Convert to a regular block
  • Reusable block contains multiple blocks: Convert to regular blocks

This PR fixes the issue by properly pluralizing the label where necessary.

Why?

It resolves #34749. The action "Convert to regular blocks" should be in singular if the reusable block only contains a single block.

How?

Based on @fluiddot advice the block count was determined by checking the inner blocks rendered within a reusable block, and the count was use to determine if to pluralize the label.

Testing Instructions

  1. Go to the web version of the editor.
  2. Open a post/page.
  3. Add any block and click on the options button located in the toolbar (the three dots icon).
  4. Click on "Add to Reusable blocks" and set a name for the block.
  5. Open the app (native version of the editor).
  6. Open a post/page.
  7. Tap on the heavy_plus_sign button.
  8. Navigate to the Reusable tab.
  9. Tap on the recently created reusable block and select the block.
  10. Tap on the block settings (three dots button).
  11. Observe that the action "Convert to regular blocks" has correct plural form depending on the number of blocks in the reusable block.

Screenshots or screencast

@codesandbox
Copy link

codesandbox bot commented Nov 16, 2022

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

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

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @janusqa! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

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

@Mamaduka Mamaduka added [Type] Enhancement A suggestion for improvement. [Block] Block The "Reusable Block" Block labels Nov 17, 2022
@janusqa janusqa force-pushed the fix/properly-pluralize-the-message-convert-to-regular-blocks branch from f580e63 to a7435eb Compare November 18, 2022 00:52
@fluiddot fluiddot self-requested a review November 18, 2022 09:45
Comment on lines 47 to 52
const innerBlockCount = useSelect(
( select ) => select( blockEditorStore ).getBlockCount( clientId ),
[ clientId ]
);
Copy link
Member

Choose a reason for hiding this comment

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

Since we're already selecting data from the block editor store, I would recommend combining these selectors. There's nothing wrong with using multiple useSelect in components, but in this case, I think it makes sense to fetch data using a single selector.

Here's an example:

const { canRemove, innerBlockCount } = useSelect(
  ( select ) => {
	  const { canRemoveBlock, getBlockCount } =
		  select( blockEditorStore );
  
	  return {
		  canRemove: canRemoveBlock( clientId ),
		  innerBlockCount: getBlockCount( clientId ),
	  };
  },
  [ clientId ]
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Mamaduka agreed. Will combine them in the other files as well if there is an per-existing select.

Comment on lines 103 to 106
const innerBlockCount = useSelect(
( select ) => select( blockEditorStore ).getBlockCount( clientId ),
[ clientId ]
);
Copy link
Member

Choose a reason for hiding this comment

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

Same as https://github.com/WordPress/gutenberg/pull/45819/files#r1026355761, and for the .native.js files as well, whenever applicable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'll do this.

@janusqa janusqa force-pushed the fix/properly-pluralize-the-message-convert-to-regular-blocks branch from a7435eb to 5f0661c Compare November 18, 2022 16:34
Copy link
Contributor

@fluiddot fluiddot left a comment

Choose a reason for hiding this comment

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

I tested the changes in the WordPress app and work as expected, thank you @janusqa for introducing this enhancement 🙇.

However, I identified a couple of places that we'd also need to pluralize the message:

Could we also update them? Thanks!

@janusqa
Copy link
Contributor Author

janusqa commented Nov 28, 2022

I tested the changes in the WordPress app and work as expected, thank you @janusqa for introducing this enhancement bow.

However, I identified a couple of places that we'd also need to pluralize the message:

* https://github.com/WordPress/gutenberg/blob/5f0661c3f5718c10751b4d4ea26e00ab56e5c4cc/packages/block-editor/src/components/block-mobile-toolbar/block-actions-menu.native.js#L204-L206

* https://github.com/WordPress/gutenberg/blob/5f0661c3f5718c10751b4d4ea26e00ab56e5c4cc/packages/block-library/src/block/edit.native.js#L131-L132

Could we also update them? Thanks!

@fluiddot yes we can, will work on this. Thanks for your feedback.

@janusqa
Copy link
Contributor Author

janusqa commented Nov 29, 2022

I tested the changes in the WordPress app and work as expected, thank you @janusqa for introducing this enhancement bow.
However, I identified a couple of places that we'd also need to pluralize the message:

* https://github.com/WordPress/gutenberg/blob/5f0661c3f5718c10751b4d4ea26e00ab56e5c4cc/packages/block-editor/src/components/block-mobile-toolbar/block-actions-menu.native.js#L204-L206

* https://github.com/WordPress/gutenberg/blob/5f0661c3f5718c10751b4d4ea26e00ab56e5c4cc/packages/block-library/src/block/edit.native.js#L131-L132

Could we also update them? Thanks!

@fluiddot yes we can, will work on this. Thanks for your feedback.

@fluiddot do you have a preference in the method used?

//method 1
const successNotice =
    innerBlockCount > 1
        ? 'converted to regular blocks'
        : 'converted to regular block';
createSuccessNotice(
    sprintf(
        /* translators: %1$s: name of the reusable block, %2$s: pluralized success message*/
        __('%1$s %2$s'),
        title,
        successNotice
    )
);
// method 2
createSuccessNotice(
    sprintf(
        /* translators: %s: block title */
        _n(
            '%s converted to regular block',
            '%s converted to regular blocks',
            innerBlockCount
        ),
        title
    )
);

@fluiddot
Copy link
Contributor

@janusqa I'd lean towards method 1 in order to properly extract the strings for translation in mobile, as we don't support yet plurals in the extraction process to include them in the apps translation pipeline. Alternatively, I'd like to share a third option where each string is referenced in its own __ call, this option will generate two strings to translate instead of three:

  • Convert to regular blocks
  • Convert to regular block
  • %1$s %2$s

Third option:

const successNotice =
	innerBlockCount > 1
		? /* translators: %s: name of the reusable block */
			__( '%s converted to regular blocks' )
		: /* translators: %s: name of the reusable block */
			__( '%s converted to regular block' );
createSuccessNotice(
	sprintf( successNotice, reusableBlock?.title?.raw || blockTitle )
);

@janusqa
Copy link
Contributor Author

janusqa commented Nov 29, 2022

@janusqa I'd lean towards method 1 in order to properly extract the strings for translation in mobile, as we don't support yet plurals in the extraction process to include them in the apps translation pipeline. Alternatively, I'd like to share a third option where each string is referenced in its own __ call, this option will generate two strings to translate instead of three:

* `Convert to regular blocks`

* `Convert to regular block`

* `%1$s %2$s`

Third option:

const successNotice =
	innerBlockCount > 1
		? /* translators: %s: name of the reusable block */
			__( '%s converted to regular blocks' )
		: /* translators: %s: name of the reusable block */
			__( '%s converted to regular block' );
createSuccessNotice(
	sprintf( successNotice, reusableBlock?.title?.raw || blockTitle )
);

Well well well, nicely done. this is very clean. I will use this one indeed.

@janusqa janusqa force-pushed the fix/properly-pluralize-the-message-convert-to-regular-blocks branch from 882847c to 73eb85c Compare November 29, 2022 20:14
@janusqa
Copy link
Contributor Author

janusqa commented Nov 29, 2022

@fluiddot
Although the changes are made we may need to tweak three (3) places in reusable-blocks.test.js

    await clickBlockToolbarButton( 'Convert to regular blocks' )

It looks like we will need to tweak these test to look for the correct string depending on what the test creates as a reusable block.

@fluiddot
Copy link
Contributor

@fluiddot Although the changes are made we may need to tweak three (3) places in reusable-blocks.test.js

    await clickBlockToolbarButton( 'Convert to regular blocks' )

It looks like we will need to tweak these test to look for the correct string depending on what the test creates as a reusable block.

@janusqa Good point, in fact, that's the reason why the End-to-End Tests / Admin - 3 check is failing in this PR. As you mentioned, we definitely need to update those tests and use the proper string in each case depending on if the Reusable block has one or more blocks 👍 . Would you be able to update it? Thanks!

@janusqa janusqa force-pushed the fix/properly-pluralize-the-message-convert-to-regular-blocks branch from 5d86e36 to 7b55aa1 Compare November 30, 2022 15:53
@janusqa
Copy link
Contributor Author

janusqa commented Nov 30, 2022

@fluiddot Although the changes are made we may need to tweak three (3) places in reusable-blocks.test.js

    await clickBlockToolbarButton( 'Convert to regular blocks' )

It looks like we will need to tweak these test to look for the correct string depending on what the test creates as a reusable block.

@janusqa Good point, in fact, that's the reason why the End-to-End Tests / Admin - 3 check is failing in this PR. As you mentioned, we definitely need to update those tests and use the proper string in each case depending on if the Reusable block has one or more blocks +1 . Would you be able to update it? Thanks!

@fluiddot Yes I should be able to. Test updated. Used a simplistic approach based on the assumption that we know the type of reusable block we will create in the test ahead of time. Let me know if we need something more dynamic.

Copy link
Contributor

@fluiddot fluiddot left a comment

Choose a reason for hiding this comment

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

LGTM 🎊 ! I've tested the changes in the mobile app and they work as expected, thanks @janusqa for this fix.

I also gave a quick look at the web version to verify that it also works there. However, @Mamaduka since you're also assigned as a reviewer, let me know if you'd like to review the PR before merging it. Thanks!

@Mamaduka
Copy link
Member

Mamaduka commented Dec 2, 2022

Thanks for the ping, @fluiddot! Everything looks good to me 👍

Congrats on your first contribution to the Gutenberg project, @janusqa 🎉

@fluiddot fluiddot merged commit 9850eec into WordPress:trunk Dec 2, 2022
@github-actions
Copy link

github-actions bot commented Dec 2, 2022

Congratulations on your first merged pull request, @janusqa! We'd like to credit you for your contribution in the post announcing the next WordPress release, but we can't find a WordPress.org profile associated with your GitHub account. When you have a moment, visit the following URL and click "link your GitHub account" under "GitHub Username" to link your accounts:

https://profiles.wordpress.org/me/profile/edit/

And if you don't have a WordPress.org account, you can create one on this page:

https://login.wordpress.org/register

Kudos!

@github-actions github-actions bot added this to the Gutenberg 14.8 milestone Dec 2, 2022
@janusqa
Copy link
Contributor Author

janusqa commented Dec 2, 2022

@Mamaduka @fluiddot thank you both for your guidance and assistance!

@janusqa janusqa deleted the fix/properly-pluralize-the-message-convert-to-regular-blocks branch December 2, 2022 19:44
mpkelly pushed a commit to mpkelly/gutenberg that referenced this pull request Dec 7, 2022
…ending on the number of blocks contained. (WordPress#45819)

* Properly pluralize 'Convert to regular blocks' label on menus

* Properly pluralize 'Convert to regular blocks' label on menus

* Properly pluralized the success message when converting reusable blocks to regular blocks

* Refactored generation of pluralized success notification for resuable block conversion, to be more clean and efficient by reducing the number of string translations needed from three to two

* Fixed two reusable block test to look for correct button label in singular form
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Block The "Reusable Block" Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reusable block: Pluralize the message "Convert to regular blocks" depending on the number of blocks contained
4 participants