export function pluralize()

in src/utils/strings.js [92:120]


export function pluralize(choices, count) {
  const { cardinal } = PluralRuleType;
  const { one, other } = PluralCategory;

  const defaultLocale = 'en';
  const defaultCategory = count === 1 ? one : other;

  // at minimum, there should at least be a "one" and "other" choice for the
  // "en" locale for use as fallback text in the case that a choice for the
  // user's resolved locale category is not provided/available
  if (!choices[defaultLocale] || !choices[defaultLocale][defaultCategory]) {
    throw new Error(`No default choices provided to pluralize using default locale ${defaultLocale}`);
  }

  let locale = defaultLocale;
  let category = defaultCategory;
  if (('Intl' in window) && ('PluralRules' in window.Intl)) {
    const preferredLocales = navigator.languages ? navigator.languages : [navigator.language];
    const pluralRules = new Intl.PluralRules(preferredLocales, { type: cardinal });
    const resolvedCategory = pluralRules.select(count);
    const resolvedLocale = pluralRules.resolvedOptions().locale;
    if (choices[resolvedLocale] && choices[resolvedLocale][resolvedCategory]) {
      locale = resolvedLocale;
      category = resolvedCategory;
    }
  }

  return choices[locale][category];
}