plural function

String plural(
  1. PluralForm callback(
    1. TranslateCallback
    )
)

Handles pluralization with support for all CLDR pluralization categories

This function supports complex pluralization rules for various languages including:

  • English-like languages (2 forms: one, other)
  • Slavic languages like Russian (3 forms: one, few, other)
  • Polish (4 forms: one, few, many, other)
  • Arabic (6 forms: zero, one, two, few, many, other)
  • And many more languages

Example usage:

String message = plural((translate) {
  return PluralForm(
    one: translate('item.one'),
    other: translate('item.other'),
    few: translate('item.few'), // Optional for Slavic languages
    many: translate('item.many'), // Optional for Polish/Arabic
    two: translate('item.two'), // Optional for Arabic
    zero: translate('item.zero'), // Optional for Arabic
    value: count,
    locale: 'ru', // Optional, uses current locale if not provided
  );
});

Implementation

String plural(PluralForm Function(TranslateCallback) callback) {
  final pluralForm = callback(_internalTranslate);

  // Get the appropriate translation based on the value and locale
  final translation = pluralForm.getAppropriateTranslation();

  // Translate the selected string and substitute the value
  return translate(translation, {
    'value': pluralForm.value,
  });
}