Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Enhancements

- Format footer item and selection counts with locale-specific separators ([#84622](https://github.com/WordPress/gutenberg/pull/84622)).
- DataForm: The `text` control's `prefix` and `suffix` accept a string, and the new `prefixPadding` and `suffixPadding` options use the minimal slot padding for icons and buttons. ([#84314](https://github.com/WordPress/gutenberg/pull/84314))

### Bug Fixes
Expand Down
20 changes: 10 additions & 10 deletions packages/dataviews/src/utils/get-footer-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ export default function getFooterMessage(
): string {
if ( selectionCount > 0 ) {
return sprintf(
/* translators: %d: number of items. */
_n( '%d Item selected', '%d Items selected', selectionCount ),
selectionCount
/* translators: %s: formatted number of items. */
_n( '%s Item selected', '%s Items selected', selectionCount ),
selectionCount.toLocaleString()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we also update DataViewsPageSelect in a similar way while we're here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I bet there are more, I found this when doing a wider fix in Calypso, will hunt for more, thanks for the nudge.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sounds good. We can see to that one (and possibly others) on follow ups.

);
}

// No selection - show item count
if ( onlyTotalCount || totalItems <= itemsCount ) {
return sprintf(
/* translators: %d: number of items. */
_n( '%d Item', '%d Items', totalItems ),
totalItems
/* translators: %s: formatted number of items. */
_n( '%s Item', '%s Items', totalItems ),
totalItems.toLocaleString()
);
}

return sprintf(
/* translators: %1$d: number of items. %2$d: total number of items. */
_n( '%1$d of %2$d Item', '%1$d of %2$d Items', totalItems ),
itemsCount,
totalItems
/* translators: %1$s: formatted number of items. %2$s: formatted total number of items. */
_n( '%1$s of %2$s Item', '%1$s of %2$s Items', totalItems ),
itemsCount.toLocaleString(),
totalItems.toLocaleString()
);
}
51 changes: 51 additions & 0 deletions packages/dataviews/src/utils/test/get-footer-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import getFooterMessage from '../get-footer-message';

const toLocaleString = Number.prototype.toLocaleString;

describe( 'getFooterMessage', () => {
beforeEach( () => {
vi.spyOn( Number.prototype, 'toLocaleString' ).mockImplementation(
function ( this: number ) {
return toLocaleString.call( this, 'en-US' );
}
);
} );

afterEach( () => vi.restoreAllMocks() );

it.each( [
[ 1234, 10, 1234567, false, '1,234 Items selected' ],
[ 0, 1234, 1234567, false, '1,234 of 1,234,567 Items' ],
[ 0, 1234567, 1234567, false, '1,234,567 Items' ],
[ 0, 10, 1234567, true, '1,234,567 Items' ],
[ 1, 10, 1234567, false, '1 Item selected' ],
[ 0, 1, 1, false, '1 Item' ],
[ 0, 0, 0, false, '0 Items' ],
] )(
'formats the count for selection %i, page %i, total %i, total-only %s',
( selection, page, total, totalOnly, expected ) => {
expect(
getFooterMessage( selection, page, total, totalOnly )
).toBe( expected );
}
);

it( 'uses the browser’s number formatting for every count', () => {
vi.mocked( Number.prototype.toLocaleString ).mockImplementation(
function ( this: number ) {
return toLocaleString.call( this, 'de-DE' );
}
);

expect( getFooterMessage( 1234, 10, 1234567 ) ).toBe(
'1.234 Items selected'
);
expect( getFooterMessage( 0, 1234, 1234567 ) ).toBe(
'1.234 of 1.234.567 Items'
);
expect( getFooterMessage( 0, 10, 1234567, true ) ).toBe(
'1.234.567 Items'
);
} );
} );
Loading