Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class I18NextOptions with Diagnosticable {
this.nestingPrefix,
this.nestingSuffix,
this.nestingSeparator,
this.singularSuffix,
this.pluralSuffix,
this.missingKeyHandler,
this.missingInterpolationHandler,
Expand Down Expand Up @@ -87,6 +88,7 @@ class I18NextOptions with Diagnosticable {
nestingPrefix: r'$t(',
nestingSuffix: ')',
nestingSeparator: ',',
singularSuffix: 'singular',

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid breaking localization changes, the default should still be maintained

pluralSuffix: 'plural',
missingKeyHandler: null,
missingInterpolationHandler: null,
Expand Down Expand Up @@ -138,14 +140,26 @@ class I18NextOptions with Diagnosticable {
/// Defaults to '.'.
final String? keySeparator;

/// [singularSuffix] is used for the pluralization mechanism.
///
/// Defaults to 'singular' and is used for simple pluralization rules.
///
/// For example, in english where it only has singular or plural forms:
///
/// ```
/// "friend_singular": "A friend"
/// "friend_plural": "{{count}} friends"
/// ```
Comment on lines +145 to +152

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too, the default should be null to maintain the existing behavior

final String? singularSuffix;

/// [pluralSuffix] is used for the pluralization mechanism.
///
/// Defaults to 'plural' and is used for simple pluralization rules.
///
/// For example, in english where it only has singular or plural forms:
///
/// ```
/// "friend": "A friend"
/// "friend_singular": "A friend"
Comment on lines -148 to +162

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️ keep default behavior docs

/// "friend_plural": "{{count}} friends"
/// ```
final String? pluralSuffix;
Expand Down Expand Up @@ -256,6 +270,7 @@ class I18NextOptions with Diagnosticable {
contextSeparator: other.contextSeparator ?? contextSeparator,
pluralSeparator: other.pluralSeparator ?? pluralSeparator,
keySeparator: other.keySeparator ?? keySeparator,
singularSuffix: other.singularSuffix ?? singularSuffix,
pluralSuffix: other.pluralSuffix ?? pluralSuffix,
interpolationPrefix: other.interpolationPrefix ?? interpolationPrefix,
interpolationSuffix: other.interpolationSuffix ?? interpolationSuffix,
Expand Down Expand Up @@ -294,6 +309,7 @@ class I18NextOptions with Diagnosticable {
String? contextSeparator,
String? pluralSeparator,
String? keySeparator,
String? singularSuffix,
String? pluralSuffix,
String? interpolationPrefix,
String? interpolationSuffix,
Expand All @@ -320,6 +336,7 @@ class I18NextOptions with Diagnosticable {
contextSeparator: contextSeparator ?? this.contextSeparator,
pluralSeparator: pluralSeparator ?? this.pluralSeparator,
keySeparator: keySeparator ?? this.keySeparator,
singularSuffix: singularSuffix ?? this.singularSuffix,
pluralSuffix: pluralSuffix ?? this.pluralSuffix,
interpolationPrefix: interpolationPrefix ?? this.interpolationPrefix,
interpolationSuffix: interpolationSuffix ?? this.interpolationSuffix,
Expand Down Expand Up @@ -365,6 +382,7 @@ class I18NextOptions with Diagnosticable {
nestingPrefix,
nestingSuffix,
nestingSeparator,
singularSuffix,
pluralSuffix,
missingKeyHandler,
missingInterpolationHandler,
Expand Down Expand Up @@ -394,6 +412,7 @@ class I18NextOptions with Diagnosticable {
other.nestingPrefix == nestingPrefix &&
other.nestingSuffix == nestingSuffix &&
other.nestingSeparator == nestingSeparator &&
other.singularSuffix == singularSuffix &&
other.pluralSuffix == pluralSuffix &&
other.missingKeyHandler == missingKeyHandler &&
other.missingInterpolationHandler == missingInterpolationHandler &&
Expand Down Expand Up @@ -422,6 +441,7 @@ class I18NextOptions with Diagnosticable {
..add(StringProperty('nestingPrefix', nestingPrefix))
..add(StringProperty('nestingSuffix', nestingSuffix))
..add(StringProperty('nestingSeparator', nestingSeparator))
..add(StringProperty('singularSuffix', singularSuffix))
..add(StringProperty('pluralSuffix', pluralSuffix))
..add(StringProperty('missingKeyHandler', missingKeyHandler?.toString()))
..add(
Expand Down
11 changes: 9 additions & 2 deletions lib/src/plural_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ class PluralResolver {
final separator = options.pluralSeparator ?? '_';

if (_ruleUsesSimpleSuffixes(rule)) {
final suffix = options.pluralSuffix ?? 'plural';
return index == 0 ? '' : '$separator$suffix';
late final String suffix;

if (index == 0) {
suffix = options.singularSuffix ?? 'singular';
} else {
suffix = options.pluralSuffix ?? 'plural';
}

return suffix.isEmpty ? '' : '$separator$suffix';
Comment on lines -17 to +25

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the base options are merged, we could do the following here to simplify this expression:

  • the singular and plural suffixes from options could just be the values themselves (from option), and remove the fallbacks from this function as to rely only on options directly.
  • then the return should check if the suffix actually exists, or if it should be ignored

this is what I mean:

      final suffix = index == 0 ? options.singularSuffix : options.pluralSuffix;
      return suffix == null || suffix.isEmpty ? '' : '$separator$suffix';

} else {
return '$separator$index';
}
Expand Down