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 * This is part of the [intl package] | 10 * This is part of the [intl package] |
11 * (https://pub.dartlang.org/packages/intl). | 11 * (https://pub.dartlang.org/packages/intl). |
12 * | 12 * |
13 * For things that require locale or other data, there are multiple different | 13 * For things that require locale or other data, there are multiple different |
14 * ways of making that data available, which may require importing different | 14 * ways of making that data available, which may require importing different |
15 * libraries. See the class comments for more details. | 15 * libraries. See the class comments for more details. |
16 * | 16 * |
17 * There is also a simple example application that can be found in the | 17 * There is also a simple example application that can be found in the |
18 * [example/basic](https://github.com/dart-lang/intl/tree/master/example/basic) | 18 * [example/basic](https://github.com/dart-lang/intl/tree/master/example/basic) |
19 * directory. | 19 * directory. |
20 */ | 20 */ |
21 library intl; | 21 library intl; |
22 | 22 |
| 23 import 'dart:async'; |
23 import 'dart:collection'; | 24 import 'dart:collection'; |
24 import 'dart:convert'; | 25 import 'dart:convert'; |
25 import 'dart:math'; | 26 import 'dart:math'; |
26 | 27 |
27 import 'date_symbols.dart'; | 28 import 'date_symbols.dart'; |
28 import 'number_symbols.dart'; | 29 import 'number_symbols.dart'; |
29 import 'number_symbols_data.dart'; | 30 import 'number_symbols_data.dart'; |
30 import 'src/date_format_internal.dart'; | 31 import 'src/date_format_internal.dart'; |
31 import 'src/intl_helpers.dart'; | 32 import 'src/intl_helpers.dart'; |
32 | 33 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 */ | 82 */ |
82 //TODO(efortuna): documentation example involving the offset parameter? | 83 //TODO(efortuna): documentation example involving the offset parameter? |
83 | 84 |
84 class Intl { | 85 class Intl { |
85 /** | 86 /** |
86 * String indicating the locale code with which the message is to be | 87 * String indicating the locale code with which the message is to be |
87 * formatted (such as en-CA). | 88 * formatted (such as en-CA). |
88 */ | 89 */ |
89 String _locale; | 90 String _locale; |
90 | 91 |
91 /** The default locale. This defaults to being set from systemLocale, but | 92 /** |
| 93 * The default locale. This defaults to being set from systemLocale, but |
92 * can also be set explicitly, and will then apply to any new instances where | 94 * can also be set explicitly, and will then apply to any new instances where |
93 * the locale isn't specified. | 95 * the locale isn't specified. Note that a locale parameter to |
| 96 * [Intl.withLocale] |
| 97 * will supercede this value while that operation is active. Using |
| 98 * [Intl.withLocale] may be preferable if you are using different locales |
| 99 * in the same application. |
94 */ | 100 */ |
95 static String defaultLocale; | 101 static String get defaultLocale { |
| 102 var zoneLocale = Zone.current[#Intl.locale]; |
| 103 return zoneLocale == null ? _defaultLocale: zoneLocale; |
| 104 } |
| 105 static set defaultLocale(String newLocale) => _defaultLocale = newLocale; |
| 106 static String _defaultLocale; |
96 | 107 |
97 /** | 108 /** |
98 * The system's locale, as obtained from the window.navigator.language | 109 * The system's locale, as obtained from the window.navigator.language |
99 * or other operating system mechanism. Note that due to system limitations | 110 * or other operating system mechanism. Note that due to system limitations |
100 * this is not automatically set, and must be set by importing one of | 111 * this is not automatically set, and must be set by importing one of |
101 * intl_browser.dart or intl_standalone.dart and calling findSystemLocale(). | 112 * intl_browser.dart or intl_standalone.dart and calling findSystemLocale(). |
102 */ | 113 */ |
103 static String systemLocale = 'en_US'; | 114 static String systemLocale = 'en_US'; |
104 | 115 |
105 /** | 116 /** |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 } | 345 } |
335 var exact = cases[choice]; | 346 var exact = cases[choice]; |
336 if (exact != null) return exact; | 347 if (exact != null) return exact; |
337 var other = cases["other"]; | 348 var other = cases["other"]; |
338 if (other == null) | 349 if (other == null) |
339 throw new ArgumentError("The 'other' case must be specified"); | 350 throw new ArgumentError("The 'other' case must be specified"); |
340 return other; | 351 return other; |
341 } | 352 } |
342 | 353 |
343 /** | 354 /** |
344 * Format the given function with a specific [locale], given a | 355 * Run [function] with the default locale set to [locale] and |
345 * [message_function] that takes no parameters. The [message_function] can be | 356 * return the result. |
346 * a simple message function that just returns the result of `Intl.message()` | |
347 * it can be a wrapper around a message function that takes arguments, or it | |
348 * can be something more complex that manipulates multiple message | |
349 * functions. | |
350 * | 357 * |
351 * In either case, the purpose of this is to delay calling [message_function] | 358 * This is run in a zone, so async operations invoked |
352 * until the proper locale has been set. This returns the result of calling | 359 * from within [function] will still have the locale set. |
353 * [message_function], which could be of an arbitrary type. | 360 * |
| 361 * In simple usage [function] might be a single |
| 362 * `Intl.message()` call or number/date formatting operation. But it can |
| 363 * also be an arbitrary function that calls multiple Intl operations. |
| 364 * |
| 365 * For example |
| 366 * |
| 367 * Intl.withLocale("fr", () => new NumberFormat.format(123456)); |
| 368 * |
| 369 * or |
| 370 * |
| 371 * hello(name) => Intl.message( |
| 372 * "Hello $name.", |
| 373 * name: 'hello', |
| 374 * args: [name], |
| 375 * desc: 'Say Hello'); |
| 376 * Intl.withLocale("zh", new Timer(new Duration(milliseconds:10), |
| 377 * () => print(hello("World"))); |
354 */ | 378 */ |
355 static withLocale(String locale, Function message_function) { | 379 static withLocale(String locale, function()) { |
356 // We have to do this silliness because Locale is not known at compile time, | 380 var canonical = Intl.canonicalizedLocale(locale); |
357 // but must be a static variable in order to be visible to the Intl.message | 381 return runZoned(function, zoneValues: {#Intl.locale : canonical}); |
358 // invocation. | |
359 var oldLocale = getCurrentLocale(); | |
360 defaultLocale = Intl.canonicalizedLocale(locale); | |
361 var result = message_function(); | |
362 defaultLocale = oldLocale; | |
363 return result; | |
364 } | 382 } |
365 | 383 |
366 /** | 384 /** |
367 * Accessor for the current locale. This should always == the default locale, | 385 * Accessor for the current locale. This should always == the default locale, |
368 * unless for some reason this gets called inside a message that resets the | 386 * unless for some reason this gets called inside a message that resets the |
369 * locale. | 387 * locale. |
370 */ | 388 */ |
371 static String getCurrentLocale() { | 389 static String getCurrentLocale() { |
372 if (defaultLocale == null) defaultLocale = systemLocale; | 390 if (defaultLocale == null) defaultLocale = systemLocale; |
373 return defaultLocale; | 391 return defaultLocale; |
374 } | 392 } |
375 | 393 |
376 toString() => "Intl($locale)"; | 394 toString() => "Intl($locale)"; |
377 } | 395 } |
OLD | NEW |