addPluralAndGenderSupport static method

Map<String, dynamic> addPluralAndGenderSupport(
  1. Map<String, String> newStrings
)

Detect plural/gender patterns and generate ARB entries accordingly

Implementation

static Map<String, dynamic> addPluralAndGenderSupport(
    Map<String, String> newStrings) {
  final result = <String, dynamic>{};
  for (final entry in newStrings.entries) {
    final value = entry.value;

    // Check for potential plural patterns
    bool containsCount = value.toLowerCase().contains('count') ||
        value.contains('{count}') ||
        RegExp(r'\b\d+\s+\w+s?\b').hasMatch(value);

    bool containsPlural = value.contains('(s)') ||
        (value.contains(' item') || value.contains(' items')) ||
        (value.contains(' file') || value.contains(' files'));

    // Detect gender patterns
    bool containsGender = value.toLowerCase().contains(' he ') ||
        value.toLowerCase().contains(' she ') ||
        value.toLowerCase().contains(' they ') ||
        value.toLowerCase().contains(' his ') ||
        value.toLowerCase().contains(' her ') ||
        value.toLowerCase().contains(' their ');

    if (containsCount && containsPlural) {
      // Example: '{count} item(s)' or '1 item' or 'count items'
      String singularForm = value
          .replaceAll('(s)', '')
          .replaceAll(RegExp(r'\b(\d+|count)\s+items\b'), r'$1 item')
          .replaceAll(RegExp(r'\bitems\b'), 'item')
          .replaceAll(RegExp(r'\b\d+\b'), '{count}');

      String pluralForm = value
          .replaceAll('(s)', 's')
          .replaceAll(RegExp(r'\b(\d+|count)\s+item\b'), r'$1 items')
          .replaceAll(RegExp(r'\bitem\b'), 'items')
          .replaceAll(RegExp(r'\b\d+\b'), '{count}');

      result[entry.key] =
          '{count, plural, one{$singularForm} other{$pluralForm}}';
    } else if (containsGender) {
      // Create gender variations with ICU syntax
      String maleForm = value;
      String femaleForm = value;
      String otherForm = value;

      // Replace gender pronouns with ICU syntax
      if (value.toLowerCase().contains(' he ')) {
        maleForm = value.replaceAll(RegExp(r'\bhe\b', caseSensitive: false),
            '{gender, select, male{he} female{she} other{they}}');
        femaleForm =
            value.replaceAll(RegExp(r'\bhe\b', caseSensitive: false), 'she');
        otherForm =
            value.replaceAll(RegExp(r'\bhe\b', caseSensitive: false), 'they');
      } else if (value.toLowerCase().contains(' she ')) {
        maleForm =
            value.replaceAll(RegExp(r'\bshe\b', caseSensitive: false), 'he');
        femaleForm = value.replaceAll(
            RegExp(r'\bshe\b', caseSensitive: false),
            '{gender, select, male{he} female{she} other{they}}');
        otherForm = value.replaceAll(
            RegExp(r'\bshe\b', caseSensitive: false), 'they');
      } else if (value.toLowerCase().contains(' they ')) {
        maleForm =
            value.replaceAll(RegExp(r'\bthey\b', caseSensitive: false), 'he');
        femaleForm = value.replaceAll(
            RegExp(r'\bthey\b', caseSensitive: false), 'she');
        otherForm = value.replaceAll(
            RegExp(r'\bthey\b', caseSensitive: false),
            '{gender, select, male{he} female{she} other{they}}');
      }

      // Handle possessive pronouns
      if (value.toLowerCase().contains(' his ')) {
        maleForm = maleForm.replaceAll(
            RegExp(r'\bhis\b', caseSensitive: false),
            '{gender, select, male{his} female{her} other{their}}');
        femaleForm = femaleForm.replaceAll(
            RegExp(r'\bhis\b', caseSensitive: false), 'her');
        otherForm = otherForm.replaceAll(
            RegExp(r'\bhis\b', caseSensitive: false), 'their');
      } else if (value.toLowerCase().contains(' her ')) {
        maleForm = maleForm.replaceAll(
            RegExp(r'\bher\b', caseSensitive: false), 'his');
        femaleForm = femaleForm.replaceAll(
            RegExp(r'\bher\b', caseSensitive: false),
            '{gender, select, male{his} female{her} other{their}}');
        otherForm = otherForm.replaceAll(
            RegExp(r'\bher\b', caseSensitive: false), 'their');
      } else if (value.toLowerCase().contains(' their ')) {
        maleForm = maleForm.replaceAll(
            RegExp(r'\btheir\b', caseSensitive: false), 'his');
        femaleForm = femaleForm.replaceAll(
            RegExp(r'\btheir\b', caseSensitive: false), 'her');
        otherForm = otherForm.replaceAll(
            RegExp(r'\btheir\b', caseSensitive: false),
            '{gender, select, male{his} female{her} other{their}}');
      }

      result[entry.key] =
          '{gender, select, male{$maleForm} female{$femaleForm} other{$otherForm}}';
    } else {
      result[entry.key] = value;
    }
  }
  return result;
}