first commit
Some checks failed
Build / run (push) Has been cancelled

This commit is contained in:
maher
2025-10-29 11:42:25 +01:00
commit 703f50a09d
4595 changed files with 385164 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
import memoize from 'nano-memoize';
import {MessageDescriptor} from './message-descriptor';
// this will get memoized by enclosing function (<Trans> or useTrans)
export function handlePluralMessage(
localeCode: string,
{message, values}: MessageDescriptor
): string {
// find plural config e.g. [one 1 item|other :count items]
const match = message.match(/\[(.+?)]/);
const count = values?.count;
if (match && match[1] && !Number.isNaN(count)) {
// get config without brackets and split by pipe e.g. [one 1 item, other :count items]
const [pluralPlaceholder, pluralConfig] = match;
const choices = pluralConfig.split('|');
if (!choices.length) return message;
// use Intl.PluralRules to determine which choice to use, based on special "count" value
const rules = getRules(localeCode);
const choiceName = rules.select(count as number);
// find the correct choice from config, or use first one
let choiceConfig = choices.find(c => {
return c.startsWith(choiceName);
});
if (!choiceConfig) {
choiceConfig = choices[0];
}
// get rid of plural prefix e.g. one 1 item => 1 item
const choice = choiceConfig.substring(choiceConfig.indexOf(' ') + 1);
return message.replace(pluralPlaceholder, choice);
}
return message;
}
const getRules = memoize((localeCode: string) => {
return new Intl.PluralRules(localeCode);
});