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

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

Issue 13814018: Make use of the patterns in Intl number formatting (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Don't always initialize _defaultLocale Created 7 years, 8 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 | « no previous file | pkg/intl/lib/number_format.dart » ('j') | pkg/intl/lib/number_format.dart » ('J')
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 * For things that require locale or other data, there are multiple different 10 * For things that require locale or other data, there are multiple different
11 * ways of making that data available, which may require importing different 11 * ways of making that data available, which may require importing different
12 * libraries. See the class comments for more details. 12 * libraries. See the class comments for more details.
13 * 13 *
14 * There is also a simple example application that can be found in the 14 * There is also a simple example application that can be found in the
15 * `example/basic` directory. 15 * `example/basic` directory.
16 */ 16 */
17 library intl; 17 library intl;
18 18
19 import 'dart:async'; 19 import 'dart:async';
20 import 'src/intl_helpers.dart'; 20 import 'src/intl_helpers.dart';
21 import 'dart:math'; 21 import 'dart:math';
22 import 'date_symbols.dart'; 22 import 'date_symbols.dart';
23 import 'src/date_format_internal.dart'; 23 import 'src/date_format_internal.dart';
24 import "number_symbols.dart";
25 import "number_symbols_data.dart";
24 26
25 part 'date_format.dart'; 27 part 'date_format.dart';
26 part 'src/date_format_field.dart'; 28 part 'src/date_format_field.dart';
27 part 'src/date_format_helpers.dart'; 29 part 'src/date_format_helpers.dart';
28 part 'bidi_formatter.dart'; 30 part 'bidi_formatter.dart';
29 part 'bidi_utils.dart'; 31 part 'bidi_utils.dart';
32 part 'number_format.dart';
30 33
31 /** 34 /**
32 * The Intl class provides a common entry point for internationalization 35 * The Intl class provides a common entry point for internationalization
33 * related tasks. An Intl instance can be created for a particular locale 36 * related tasks. An Intl instance can be created for a particular locale
34 * and used to create a date format via `anIntl.date()`. Static methods 37 * and used to create a date format via `anIntl.date()`. Static methods
35 * on this class are also used in message formatting. 38 * on this class are also used in message formatting.
36 * 39 *
37 * Message example: 40 * Message example:
38 * '''I see ${Intl.plural(num_people, 41 * '''I see ${Intl.plural(num_people,
39 * {'0': 'no one at all', 42 * {'0': 'no one at all',
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 * Note that null is interpreted as meaning the default locale, so if 166 * Note that null is interpreted as meaning the default locale, so if
164 * [newLocale] is null it will be returned. 167 * [newLocale] is null it will be returned.
165 */ 168 */
166 static String verifiedLocale(String newLocale, Function localeExists, 169 static String verifiedLocale(String newLocale, Function localeExists,
167 {Function onFailure: _throwLocaleError}) { 170 {Function onFailure: _throwLocaleError}) {
168 // TODO(alanknight): Previously we kept a single verified locale on the Intl 171 // TODO(alanknight): Previously we kept a single verified locale on the Intl
169 // object, but with different verification for different uses, that's more 172 // object, but with different verification for different uses, that's more
170 // difficult. As a result, we call this more often. Consider keeping 173 // difficult. As a result, we call this more often. Consider keeping
171 // verified locales for each purpose if it turns out to be a performance 174 // verified locales for each purpose if it turns out to be a performance
172 // issue. 175 // issue.
173 if (newLocale == null) return systemLocale; 176 if (newLocale == null) return getCurrentLocale();
174 if (localeExists(newLocale)) { 177 if (localeExists(newLocale)) {
175 return newLocale; 178 return newLocale;
176 } 179 }
177 for (var each in 180 for (var each in
178 [canonicalizedLocale(newLocale), _shortLocale(newLocale)]) { 181 [canonicalizedLocale(newLocale), _shortLocale(newLocale)]) {
179 if (localeExists(each)) { 182 if (localeExists(each)) {
180 return each; 183 return each;
181 } 184 }
182 } 185 }
183 return onFailure(newLocale); 186 return onFailure(newLocale);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 * functions. 240 * functions.
238 * 241 *
239 * In either case, the purpose of this is to delay calling [message_function] 242 * In either case, the purpose of this is to delay calling [message_function]
240 * until the proper locale has been set. This returns the result of calling 243 * until the proper locale has been set. This returns the result of calling
241 * [msg_function], which could be of an arbitrary type. 244 * [msg_function], which could be of an arbitrary type.
242 */ 245 */
243 static withLocale(String locale, Function message_function) { 246 static withLocale(String locale, Function message_function) {
244 // We have to do this silliness because Locale is not known at compile time, 247 // We have to do this silliness because Locale is not known at compile time,
245 // but must be a static variable in order to be visible to the Intl.message 248 // but must be a static variable in order to be visible to the Intl.message
246 // invocation. 249 // invocation.
247 if (_defaultLocale == null) _defaultLocale = systemLocale; 250 var oldLocale = getCurrentLocale();
248 var oldLocale = _defaultLocale;
249 _defaultLocale = locale; 251 _defaultLocale = locale;
250 var result = message_function(); 252 var result = message_function();
251 _defaultLocale = oldLocale; 253 _defaultLocale = oldLocale;
252 return result; 254 return result;
253 } 255 }
254 256
255 /** 257 /**
256 * Support method for message formatting. Select the correct exact (gender, 258 * Support method for message formatting. Select the correct exact (gender,
257 * usually) form from [cases] given the user [choice]. 259 * usually) form from [cases] given the user [choice].
258 */ 260 */
(...skipping 12 matching lines...) Expand all
271 * unless for some reason this gets called inside a message that resets the 273 * unless for some reason this gets called inside a message that resets the
272 * locale. 274 * locale.
273 */ 275 */
274 static String getCurrentLocale() { 276 static String getCurrentLocale() {
275 if (_defaultLocale == null) _defaultLocale = systemLocale; 277 if (_defaultLocale == null) _defaultLocale = systemLocale;
276 return _defaultLocale; 278 return _defaultLocale;
277 } 279 }
278 280
279 toString() => "Intl($locale)"; 281 toString() => "Intl($locale)";
280 } 282 }
OLDNEW
« no previous file with comments | « no previous file | pkg/intl/lib/number_format.dart » ('j') | pkg/intl/lib/number_format.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698