Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(589)

Unified Diff: pkg/intl/lib/intl.dart

Issue 18543009: Plurals and Genders (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix typo Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/intl/lib/generate_localized.dart ('k') | pkg/intl/lib/src/intl_helpers.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/intl/lib/intl.dart
diff --git a/pkg/intl/lib/intl.dart b/pkg/intl/lib/intl.dart
index e0e32eeb977a9e10810d80e7804542d2f5cc03a8..8801031e1600c833706f2b89b98ce4b39c956549 100644
--- a/pkg/intl/lib/intl.dart
+++ b/pkg/intl/lib/intl.dart
@@ -71,7 +71,7 @@ part 'number_format.dart';
* '''I see ${Intl.plural(num_people,
* {'0': 'no one at all',
* '1': 'one other person',
- * 'other': '$num_people other people'})} in $place.'''',
+ * 'other': '$num_people other people'})} in $place.''',
* name: 'msg',
* args: [num_people, place],
* desc: 'Description of how many people are seen as program start.',
@@ -238,12 +238,42 @@ class Intl {
}
/**
- * Support method for message formatting. Select the correct plural form from
- * [cases] given [howMany].
+ * Format a message differently depending on [howMany]. Normally used
+ * as part of an `Intl.message` text that is to be translated.
+ * Selects the correct plural form from
+ * the provided alternatives. The [other] named argument is mandatory.
*/
- static String plural(var howMany, Map cases, [num offset=0]) {
- // TODO(efortuna): Deal with "few" and "many" cases, offset, and others!
- return select(howMany.toString(), cases);
+ static String plural(int howMany, {zero, one, two, few, many, other}) {
+ if (other == null) {
+ throw new ArgumentError("The 'other' named argument must be provided");
+ }
+ // TODO(alanknight): This algorithm needs to be locale-dependent.
+ switch (howMany) {
+ case 0 : return (zero == null) ? other : zero;
+ case 1 : return (one == null) ? other : one;
+ case 2: return (two == null) ? ((few == null) ? other : few) : two;
+ default:
+ if (howMany == 3 || howMany == 4 && few != null) return few;
+ if (howMany > 10 && howMany < 100 && many != null) return many;
+ return other;
+ }
+ throw new ArgumentError("Invalid plural usage for $howMany");
+ }
+
+ /**
+ * Format a message differently depending on [gender]. Normally used as part
+ * of an Intl.message message that is to be translated.
+ */
+ static String gender(String gender,
+ {String male, String female, String other}) {
+ if (other == null) {
+ throw new ArgumentError("The 'other' named argument must be specified");
+ }
+ switch(gender) {
+ case "female" : return female == null ? other : female;
+ case "male" : return male == null ? other : male;
+ default: return other;
+ }
}
/**
@@ -274,13 +304,10 @@ class Intl {
* usually) form from [cases] given the user [choice].
*/
static String select(String choice, Map cases) {
- if (cases.containsKey(choice)) {
- return cases[choice];
- } else if (cases.containsKey('other')){
- return cases['other'];
- } else {
- return '';
- }
+ var exact = cases[choice];
+ if (exact != null) return exact;
+ var other = cases["other"];
+ return (other == null) ? '' : other;
}
/**
« no previous file with comments | « pkg/intl/lib/generate_localized.dart ('k') | pkg/intl/lib/src/intl_helpers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698