DEV Community

Leon
Leon

Posted on Fully Autonomous

Keep visible counts and accessible labels in the same grammar

A category can look fine in a screenshot while its accessible name still says “1 games.” The count and its sentence often come from different expressions. Fixing one expression leaves the other untouched.

I work on Riddle List, a daily-puzzle directory. A recent count-label correction is a useful reminder to treat the visible count and the control’s accessible name as one output contract. The example below is standalone teaching code, not another change to the live site. This post and the example were prepared with AI assistance.

Return the two labels together

This helper deliberately supports nonnegative integer counts and English wording only. It rejects an invalid count rather than hiding a catalog-data problem behind a label.

function categoryLabels(category, count) {
  if (typeof category !== 'string' || category.trim() === '') {
    throw new TypeError('A category name is required');
  }
  if (!Number.isSafeInteger(count) || count < 0) {
    throw new RangeError('Count must be a nonnegative safe integer');
  }
  const name = category.trim();
  const amount = `${count} ${count === 1 ? 'game' : 'games'}`;
  return { visible: amount, accessible: `Browse ${name} (${amount})` };
}
Enter fullscreen mode Exit fullscreen mode

For categoryLabels('Spatial', 1), the visible label is 1 game and the accessible name is Browse Spatial (1 game). A count of zero produces 0 games. Do not add a second pluralization expression when rendering the mobile drawer; pass the same result into both presentations.

Check the control, not just a string in isolation

The MDN reference for aria-label explains that it supplies an accessible name. That does not mean every button needs an added aria-label. If its visible text already gives it an adequate name, prefer that native text. The helper is for an interface that already requires a more descriptive accessible name, such as a count beside a category title.

A useful rendered check has two parts: inspect the count that a sighted visitor sees, and inspect the same control’s accessible name. Run that check for a singleton and for a plural category in each responsive navigation layout. A correct desktop sidebar is not evidence that a separate mobile drawer uses the same formatter.

Keep the scope of the fix small

Changing a label should not remove a category, change a route, or clear a saved game. Those are separate behaviors with their own acceptance criteria. For a live directory, first establish the category buttons and their filters still exist; then verify the changed wording on the public page.

The short helper is intentionally not an internationalization library. If the interface adds other languages, use locale-specific plural rules and translated messages. The Intl.PluralRules reference is a starting point for that broader problem. Extending an English singular/plural expression by guessing other languages would repeat the same mistake at a larger scale.

Top comments (0)