Pluralization question #370
-
Beta Was this translation helpful? Give feedback.
Answered by
aymericzip
Jan 20, 2026
Replies: 2 comments 1 reply
-
|
Hey @vabyars Thanks for reporting that. I suggest to use Intl global class as a quick solution by waiting a solution from the intlayer side const dictionary = {
key_ordinal_one: "{{count}}st place",
key_ordinal_two: "{{count}}nd place",
key_ordinal_few: "{{count}}rd place",
key_ordinal_other: "{{count}}th place"
};
const getOrdinalMessage = (count, locale) => {
// Initialize PluralRules with type: 'ordinal'
const rules = new Intl.PluralRules(locale, { type: 'ordinal' });
// Get the category ('one', 'two', 'few', 'other')
const category = rules.select(count);
// Map the category to your specific keys
const key = `key_ordinal_${category}`;
// Retrieve and parse the string (simple replacement)
const template = dictionary[key];
// Fallback if the key doesn't exist (safety check)
if (!template) return dictionary['key_ordinal_other'].replace('{{count}}', count);
return template.replace('{{count}}', count);
};
console.log(getOrdinalMessage(1)); // "1st place" (matches 'one')
console.log(getOrdinalMessage(2)); // "2nd place" (matches 'two')
console.log(getOrdinalMessage(3)); // "3rd place" (matches 'few')
console.log(getOrdinalMessage(4)); // "4th place" (matches 'other')
console.log(getOrdinalMessage(11)); // "11th place" (Correctly matches 'other', not 'one')
console.log(getOrdinalMessage(21)); // "21st place" (Correctly matches 'one') |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Hey @vabyars Here is another solution: import { enu, insert, type Dictionary } from 'intlayer';
const content = {
key: 'ranking_component',
content: {
ordinal: enu({
1: insert('{{count}}st place'),
2: insert('{{count}}nd place'),
3: insert('{{count}}rd place'),
'fallback': insert('{{count}}th place'),
}),
},
} satisfies Dictionary;
export default content;const { ordinal } = useIntlayer('ranking_component');
const value = 5
const lastDigit = Math.abs(count) % 10
return <>{ordinal(lastDigit)({ count })}</> |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
vabyars
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Hey @vabyars
Here is another solution: