Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * This library provides internationalization and localization. This includes | 6 * This library provides internationalization and localization. This includes |
| 7 * message formatting and replacement, date and number formatting and parsing, | 7 * message formatting and replacement, date and number formatting and parsing, |
| 8 * and utilities for working with Bidirectional text. | 8 * and utilities for working with Bidirectional text. |
| 9 * | 9 * |
| 10 * ## Installing ## | 10 * ## Installing ## |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 * as part of an `Intl.message` text that is to be translated. | 242 * as part of an `Intl.message` text that is to be translated. |
| 243 * Selects the correct plural form from | 243 * Selects the correct plural form from |
| 244 * the provided alternatives. The [other] named argument is mandatory. | 244 * the provided alternatives. The [other] named argument is mandatory. |
| 245 */ | 245 */ |
| 246 static String plural(int howMany, {zero, one, two, few, many, other, | 246 static String plural(int howMany, {zero, one, two, few, many, other, |
| 247 desc, examples, locale, name, args}) { | 247 desc, examples, locale, name, args}) { |
| 248 // If we are passed a name and arguments, then we are operating as a | 248 // If we are passed a name and arguments, then we are operating as a |
| 249 // top-level message, so look up our translation by calling Intl.message | 249 // top-level message, so look up our translation by calling Intl.message |
| 250 // with ourselves as an argument. | 250 // with ourselves as an argument. |
| 251 if (name != null) { | 251 if (name != null) { |
| 252 return Intl.message( | 252 return message( |
| 253 Intl.plural(howMany, | 253 plural(howMany, |
| 254 zero: zero, one: one, two: two, few: few, many: many, other: other), | 254 zero: zero, one: one, two: two, few: few, many: many, other: other), |
| 255 name: name, | 255 name: name, |
| 256 args: args, | 256 args: args, |
| 257 locale: locale); | 257 locale: locale); |
| 258 } | 258 } |
| 259 if (other == null) { | 259 if (other == null) { |
| 260 throw new ArgumentError("The 'other' named argument must be provided"); | 260 throw new ArgumentError("The 'other' named argument must be provided"); |
| 261 } | 261 } |
| 262 // TODO(alanknight): This algorithm needs to be locale-dependent. | 262 // TODO(alanknight): This algorithm needs to be locale-dependent. |
| 263 switch (howMany) { | 263 switch (howMany) { |
| 264 case 0 : return (zero == null) ? other : zero; | 264 case 0 : return (zero == null) ? other : zero; |
| 265 case 1 : return (one == null) ? other : one; | 265 case 1 : return (one == null) ? other : one; |
| 266 case 2: return (two == null) ? ((few == null) ? other : few) : two; | 266 case 2: return (two == null) ? ((few == null) ? other : few) : two; |
| 267 default: | 267 default: |
| 268 if (howMany == 3 || howMany == 4 && few != null) return few; | 268 if (howMany == 3 || howMany == 4 && few != null) return few; |
| 269 if (howMany > 10 && howMany < 100 && many != null) return many; | 269 if (howMany > 10 && howMany < 100 && many != null) return many; |
| 270 return other; | 270 return other; |
| 271 } | 271 } |
| 272 throw new ArgumentError("Invalid plural usage for $howMany"); | 272 throw new ArgumentError("Invalid plural usage for $howMany"); |
| 273 } | 273 } |
| 274 | 274 |
| 275 /** | 275 /** |
| 276 * Format a message differently depending on [gender]. Normally used as part | 276 * Format a message differently depending on [targetGender]. Normally used as part |
|
Emily Fortuna
2013/08/15 21:59:12
80 char
Alan Knight
2013/08/15 22:27:32
Done.
| |
| 277 * of an Intl.message message that is to be translated. | 277 * of an Intl.message message that is to be translated. |
| 278 */ | 278 */ |
| 279 static String gender(String gender, | 279 static String gender(String targetGender, |
| 280 {String male, String female, String other, | 280 {String male, String female, String other, |
| 281 String desc, Map examples, String locale, String name, | 281 String desc, Map examples, String locale, String name, |
| 282 List<String>args}) { | 282 List<String>args}) { |
| 283 // If we are passed a name and arguments, then we are operating as a | 283 // If we are passed a name and arguments, then we are operating as a |
| 284 // top-level message, so look up our translation by calling Intl.message | 284 // top-level message, so look up our translation by calling Intl.message |
| 285 // with ourselves as an argument. | 285 // with ourselves as an argument. |
| 286 if (name != null) { | 286 if (name != null) { |
| 287 return Intl.message( | 287 return message( |
| 288 Intl.gender(gender, male: male, female: female, other: other), | 288 gender(targetGender, male: male, female: female, other: other), |
| 289 name: name, | 289 name: name, |
| 290 args: args, | 290 args: args, |
| 291 locale: locale); | 291 locale: locale); |
| 292 } | 292 } |
| 293 | 293 |
| 294 if (other == null) { | 294 if (other == null) { |
| 295 throw new ArgumentError("The 'other' named argument must be specified"); | 295 throw new ArgumentError("The 'other' named argument must be specified"); |
| 296 } | 296 } |
| 297 switch(gender) { | 297 switch(targetGender) { |
| 298 case "female" : return female == null ? other : female; | 298 case "female" : return female == null ? other : female; |
| 299 case "male" : return male == null ? other : male; | 299 case "male" : return male == null ? other : male; |
| 300 default: return other; | 300 default: return other; |
| 301 } | 301 } |
| 302 } | 302 } |
| 303 | 303 |
| 304 /** | 304 /** |
| 305 * Format a message differently depending on [choice]. We look up the value | 305 * Format a message differently depending on [choice]. We look up the value |
| 306 * of [choice] in [cases] and return the result, or an empty string if | 306 * of [choice] in [cases] and return the result, or an empty string if |
| 307 * it is not found. Normally used as part | 307 * it is not found. Normally used as part |
| 308 * of an Intl.message message that is to be translated. | 308 * of an Intl.message message that is to be translated. |
| 309 */ | 309 */ |
| 310 static String select(String choice, Map<String, String> cases, | 310 static String select(String choice, Map<String, String> cases, |
| 311 {String desc, Map examples, String locale, String name, | 311 {String desc, Map examples, String locale, String name, |
| 312 List<String>args}) { | 312 List<String>args}) { |
| 313 // If we are passed a name and arguments, then we are operating as a | 313 // If we are passed a name and arguments, then we are operating as a |
| 314 // top-level message, so look up our translation by calling Intl.message | 314 // top-level message, so look up our translation by calling Intl.message |
| 315 // with ourselves as an argument. | 315 // with ourselves as an argument. |
| 316 if (name != null) { | 316 if (name != null) { |
| 317 return Intl.message( | 317 return message( |
| 318 Intl.select(choice, cases), | 318 select(choice, cases), |
| 319 name: name, | 319 name: name, |
| 320 args: args, | 320 args: args, |
| 321 locale: locale); | 321 locale: locale); |
| 322 } | 322 } |
| 323 var exact = cases[choice]; | 323 var exact = cases[choice]; |
| 324 if (exact != null) return exact; | 324 if (exact != null) return exact; |
| 325 var other = cases["other"]; | 325 var other = cases["other"]; |
| 326 if (other == null) | 326 if (other == null) |
| 327 throw new ArgumentError("The 'other' case must be specified"); | 327 throw new ArgumentError("The 'other' case must be specified"); |
| 328 return other; | 328 return other; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 356 * unless for some reason this gets called inside a message that resets the | 356 * unless for some reason this gets called inside a message that resets the |
| 357 * locale. | 357 * locale. |
| 358 */ | 358 */ |
| 359 static String getCurrentLocale() { | 359 static String getCurrentLocale() { |
| 360 if (_defaultLocale == null) _defaultLocale = systemLocale; | 360 if (_defaultLocale == null) _defaultLocale = systemLocale; |
| 361 return _defaultLocale; | 361 return _defaultLocale; |
| 362 } | 362 } |
| 363 | 363 |
| 364 toString() => "Intl($locale)"; | 364 toString() => "Intl($locale)"; |
| 365 } | 365 } |
| OLD | NEW |