Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/basic plurals support #20

Closed

Conversation

defuncart
Copy link
Collaborator

@defuncart defuncart commented May 3, 2020

A possible solution to resolve #14.

By depending upon intl package, we can use the Intl.plural method to determine the plural form for a given number.

Thus an input key of the form

pluralTest,Time left: %seconds$d |seconds:second:seconds:seconds:seconds:seconds|

would be parsed into the following method:

import 'package:intl/intl.dart';

String pluralTest({
    @required int seconds,
  }) {
    String text = _getText("pluralTest");
    if (seconds != null) {
      text = text.replaceAll("%seconds\$d", seconds.toString());
    }
    final _pluralsRegExp = RegExp(r"\|(.*?)\|");
    final plurals =
        _pluralsRegExp.firstMatch(text).group(0).replaceAll('|', '').split(':');
    if (plurals.length != 6) {
      return 'Issue with key pluralTest. Incorrect number of plurals.';
    }
    return Intl.plural(
      seconds,
      zero: text.replaceAll(_pluralsRegExp, plurals[0]),
      one: text.replaceAll(_pluralsRegExp, plurals[1]),
      two: text.replaceAll(_pluralsRegExp, plurals[2]),
      few: text.replaceAll(_pluralsRegExp, plurals[3]),
      many: text.replaceAll(_pluralsRegExp, plurals[4]),
      other: text.replaceAll(_pluralsRegExp, plurals[5]),
    );
  }

and can then be used as follows:

seconds fr en es de
0 Temps restant: 0 seconde Time left: 0 seconds Tiempo que queda: 0 segundos Verbleibende Zeit: 0 Sekunden
1 Temps restant: 1 second Time left: 1 second Tiempo que queda: 1 segundo Verbleibende Zeit: 1 Sekunde
2 Temps restant: 2 secondes Time left: 2 seconds Tiempo que queda: 2 segundos Verbleibende Zeit: 2 Sekunden

(Note that I don't speak French or Spanish, so if I made a mistake, please let me know and I'll update the example).

Presently only one plural per key is supported. intl could also be used to add support for genders, however such advanced localizations may be better served by sticking to official ARB process.

For English and German, writing |seconds:second:seconds:seconds:seconds:seconds| and |Sekunden:Sekunde:Sekunden:Sekunden:Sekunden:Sekunden| is pretty overkill as it is always "seconds" except for one case. In the future some optimizations could be introduced so that not all six forms always need to be supplied.

@defuncart defuncart mentioned this pull request May 3, 2020
@defuncart defuncart force-pushed the feature/basic-plurals-support branch from c37ba15 to 1133586 Compare May 3, 2020 12:59
… into feature/basic-plurals-support

# Conflicts:
#	README.md
#	example/ios/Runner.xcodeproj/project.pbxproj
#	example/pubspec.lock
#	lib/flappy_translator.dart
#	lib/template.dart
@defuncart defuncart closed this Mar 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can handle plurals?
1 participant