@@ -173,7 +173,7 @@ class Intl {
173
173
/// the placeholder automatically translated.
174
174
static String message (String message_str,
175
175
{String desc: '' ,
176
- Map <String , Object > examples: const {} ,
176
+ Map <String , Object > examples,
177
177
String locale,
178
178
String name,
179
179
List <Object > args,
@@ -267,11 +267,17 @@ class Intl {
267
267
return '${aLocale [0 ]}${aLocale [1 ]}_$region ' ;
268
268
}
269
269
270
- /// Format a message differently depending on [howMany] . Normally used
271
- /// as part of an `Intl.message` text that is to be translated.
272
- /// Selects the correct plural form from
273
- /// the provided alternatives. The [other] named argument is mandatory.
274
- static String plural (int howMany,
270
+ /// Formats a message differently depending on [howMany] .
271
+ ///
272
+ /// Selects the correct plural form from the provided alternatives.
273
+ /// The [other] named argument is mandatory.
274
+ /// The [precision] is the number of fractional digits that would be rendered
275
+ /// when [howMany] is formatted. In some cases just knowing the numeric value
276
+ /// of [howMany] itsef is not enough, for example "1 mile" vs "1.00 miles"
277
+ ///
278
+ /// For an explanation of plurals and the [zero] , [one] , [two] , [few] , [many]
279
+ /// categories see http://cldr.unicode.org/index/cldr-spec/plural-rules
280
+ static String plural (num howMany,
275
281
{String zero,
276
282
String one,
277
283
String two,
@@ -281,6 +287,7 @@ class Intl {
281
287
String desc,
282
288
Map <String , Object > examples,
283
289
String locale,
290
+ int precision,
284
291
String name,
285
292
List <Object > args,
286
293
String meaning,
@@ -295,19 +302,21 @@ class Intl {
295
302
many: many,
296
303
other: other,
297
304
locale: locale,
305
+ precision: precision,
298
306
name: name,
299
307
args: args,
300
308
meaning: meaning);
301
309
}
302
310
303
- static String _plural (int howMany,
311
+ static String _plural (num howMany,
304
312
{String zero,
305
313
String one,
306
314
String two,
307
315
String few,
308
316
String many,
309
317
String other,
310
318
String locale,
319
+ int precision,
311
320
String name,
312
321
List <Object > args,
313
322
String meaning}) {
@@ -325,29 +334,40 @@ class Intl {
325
334
few: few,
326
335
many: many,
327
336
other: other,
328
- locale: locale);
337
+ locale: locale,
338
+ precision: precision);
329
339
}
330
340
331
341
/// Internal: Implements the logic for plural selection - use [plural] for
332
342
/// normal messages.
333
- static pluralLogic (int howMany,
334
- {zero, one, two, few, many, other, String locale, String meaning}) {
343
+ static pluralLogic (num howMany,
344
+ {zero, one, two, few, many, other, String locale, int precision,
345
+ String meaning}) {
335
346
if (other == null ) {
336
347
throw new ArgumentError ("The 'other' named argument must be provided" );
337
348
}
338
349
if (howMany == null ) {
339
350
throw new ArgumentError ("The howMany argument to plural cannot be null" );
340
351
}
341
- // If there's an explicit case for the exact number, we use it. This is not
342
- // strictly in accord with the CLDR rules, but it seems to be the
343
- // expectation. At least I see e.g. Russian translations that have a zero
344
- // case defined. The rule for that locale will never produce a zero, and
345
- // treats it as other. But it seems reasonable that, even if the language
346
- // rules treat zero as other, we might want a special message for zero.
347
- if (howMany == 0 && zero != null ) return zero;
348
- if (howMany == 1 && one != null ) return one;
349
- if (howMany == 2 && two != null ) return two;
350
- var pluralRule = _pluralRule (locale, howMany);
352
+
353
+ // This is for backward compatibility.
354
+ // We interpret the presence of [precision] parameter as an "opt-in" to
355
+ // the new behavior, since [precision] did not exist before.
356
+ // For an English example: if the precision is 2 then the formatted string
357
+ // would not map to 'one' (for example "1.00 miles")
358
+ if (precision == null || precision == 0 ) {
359
+ // If there's an explicit case for the exact number, we use it. This is
360
+ // not strictly in accord with the CLDR rules, but it seems to be the
361
+ // expectation. At least I see e.g. Russian translations that have a zero
362
+ // case defined. The rule for that locale will never produce a zero, and
363
+ // treats it as other. But it seems reasonable that, even if the language
364
+ // rules treat zero as other, we might want a special message for zero.
365
+ if (howMany == 0 && zero != null ) return zero;
366
+ if (howMany == 1 && one != null ) return one;
367
+ if (howMany == 2 && two != null ) return two;
368
+ }
369
+
370
+ var pluralRule = _pluralRule (locale, howMany, precision);
351
371
var pluralCase = pluralRule ();
352
372
switch (pluralCase) {
353
373
case plural_rules.PluralCase .ZERO :
@@ -371,8 +391,8 @@ class Intl {
371
391
static var _cachedPluralRule;
372
392
static String _cachedPluralLocale;
373
393
374
- static _pluralRule (String locale, int howMany) {
375
- plural_rules.startRuleEvaluation (howMany);
394
+ static _pluralRule (String locale, num howMany, int precision ) {
395
+ plural_rules.startRuleEvaluation (howMany, precision );
376
396
var verifiedLocale = Intl .verifiedLocale (
377
397
locale, plural_rules.localeHasPluralRules,
378
398
onFailure: (locale) => 'default' );
@@ -414,7 +434,6 @@ class Intl {
414
434
String male,
415
435
String other,
416
436
String desc,
417
- Map <String , Object > examples,
418
437
String locale,
419
438
String name,
420
439
List <Object > args,
0 commit comments