
A ticket inbox switches between grammatically different labels when a count changes. The locale returns a category that selects a translated message. The application still owns the actual words.
A runnable JavaScript fixture
The example keeps the input to Intl.PluralRules outside the function, so it is possible to run the same transformation in a test. The comparison uses concrete data rather than the rendered component. If the application accepts a value from JSON, run the fixture after parsing that JSON to preserve the relevant types.
function evaluate(value) {
return new Intl.PluralRules('en').select(value);
}
const samples = [0, 1, 2];
for (const sample of samples) console.log(evaluate(sample));The result for three concrete cases
| Input | Return value |
|---|---|
| 0 | 'other' |
| 1 | 'one' |
| 2 | 'other' |
For 0, the observed output is 'other'. With 1, it becomes 'one'. If a second implementation produces a different answer, begin with these small inputs. They allow a reviewer to trace the expression without reconstructing the entire application or replaying a production workload.
The result for 2 is text, 'other'. Compare the resulting characters directly in a test. If two outputs look identical on screen, printing a quoted representation can expose a trailing space, a line boundary or an escaped character.
An acceptance check for the real feature
Avoid writing one English suffix rule for every language. Some languages return more categories or classify zero differently.
Run one case twice through the real feature and look at both the displayed value and its stored representation. A component may coerce a result to text even when the implementation returns a text value. Keep a failing sample small enough that it can travel with a bug report, including the browser or Node version that produced it.
One more fixture before integrating
Pass the count into the translation system together with the selected category. A category name such as other is an input to the message catalog and should never leak into a user-facing label.
The same JavaScript function was also run with 0 / -1 / 1. These additional inputs change the boundary without changing the implementation. An exception name in the table records where the operation rejects an input; a returned empty or zero value records a successful call. Choose deliberately which of those outcomes the surrounding feature should expose.
| Additional fixture | Result or exception |
|---|---|
| 0 | 'other' |
| -1 | 'one' |
| 1 | 'one' |
JavaScript standard library reference · MDN JavaScript language guide
Sources and further reading
Cover photograph: Computer mouse laptop and phone on a desk by Enoch Leung. CC BY-SA 2.0. Cropped and typeset for this cover. The photograph illustrates the topic. This image adaptation uses the same share-alike license.
