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

Side by Side Diff: pkg/intl/lib/intl.dart

Issue 22286013: Add support for Intl.select (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Changes from review Created 7 years, 4 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/intl/lib/extract_messages.dart ('k') | pkg/intl/lib/src/intl_message.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(gender) {
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
306 * of [choice] in [cases] and return the result, or an empty string if
307 * it is not found. Normally used as part
308 * of an Intl.message message that is to be translated.
309 */
310 static String select(String choice, Map<String, String> cases,
311 {String desc, Map examples, String locale, String name,
312 List<String>args}) {
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
315 // with ourselves as an argument.
316 if (name != null) {
317 return Intl.message(
318 Intl.select(choice, cases),
319 name: name,
320 args: args,
321 locale: locale);
322 }
323 var exact = cases[choice];
324 if (exact != null) return exact;
325 var other = cases["other"];
326 if (other == null)
327 throw new ArgumentError("The 'other' case must be specified");
328 return other;
329 }
330
331 /**
305 * Format the given function with a specific [locale], given a 332 * Format the given function with a specific [locale], given a
306 * [message_function] that takes no parameters. The [message_function] can be 333 * [message_function] that takes no parameters. The [message_function] can be
307 * a simple message function that just returns the result of `Intl.message()` 334 * a simple message function that just returns the result of `Intl.message()`
308 * it can be a wrapper around a message function that takes arguments, or it 335 * it can be a wrapper around a message function that takes arguments, or it
309 * can be something more complex that manipulates multiple message 336 * can be something more complex that manipulates multiple message
310 * functions. 337 * functions.
311 * 338 *
312 * In either case, the purpose of this is to delay calling [message_function] 339 * In either case, the purpose of this is to delay calling [message_function]
313 * until the proper locale has been set. This returns the result of calling 340 * until the proper locale has been set. This returns the result of calling
314 * [message_function], which could be of an arbitrary type. 341 * [message_function], which could be of an arbitrary type.
315 */ 342 */
316 static withLocale(String locale, Function message_function) { 343 static withLocale(String locale, Function message_function) {
317 // We have to do this silliness because Locale is not known at compile time, 344 // We have to do this silliness because Locale is not known at compile time,
318 // but must be a static variable in order to be visible to the Intl.message 345 // but must be a static variable in order to be visible to the Intl.message
319 // invocation. 346 // invocation.
320 var oldLocale = getCurrentLocale(); 347 var oldLocale = getCurrentLocale();
321 _defaultLocale = locale; 348 _defaultLocale = locale;
322 var result = message_function(); 349 var result = message_function();
323 _defaultLocale = oldLocale; 350 _defaultLocale = oldLocale;
324 return result; 351 return result;
325 } 352 }
326 353
327 /** 354 /**
328 * Support method for message formatting. Select the correct exact (gender,
329 * usually) form from [cases] given the user [choice].
330 */
331 static String select(String choice, Map cases) {
332 var exact = cases[choice];
333 if (exact != null) return exact;
334 var other = cases["other"];
335 return (other == null) ? '' : other;
336 }
337
338 /**
339 * Accessor for the current locale. This should always == the default locale, 355 * Accessor for the current locale. This should always == the default locale,
340 * 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
341 * locale. 357 * locale.
342 */ 358 */
343 static String getCurrentLocale() { 359 static String getCurrentLocale() {
344 if (_defaultLocale == null) _defaultLocale = systemLocale; 360 if (_defaultLocale == null) _defaultLocale = systemLocale;
345 return _defaultLocale; 361 return _defaultLocale;
346 } 362 }
347 363
348 toString() => "Intl($locale)"; 364 toString() => "Intl($locale)";
349 } 365 }
OLDNEW
« no previous file with comments | « pkg/intl/lib/extract_messages.dart ('k') | pkg/intl/lib/src/intl_message.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698