OLD | NEW |
---|---|
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
1 /** | 5 /** |
2 * Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
3 * for details. All rights reserved. Use of this source code is governed by a | |
4 * BSD-style license that can be found in the LICENSE file. | |
5 * | |
6 * Internationalization object providing access to message formatting objects, | 6 * Internationalization object providing access to message formatting objects, |
7 * date formatting, parsing, bidirectional text relative to a specific locale. | 7 * date formatting, parsing, bidirectional text relative to a specific locale. |
8 */ | 8 */ |
9 | 9 |
10 #library('Intl'); | 10 #library('Intl'); |
11 | 11 |
12 #import('intl_message.dart'); | 12 #import('intl_message.dart'); |
13 #import('date_format.dart'); | 13 #import('date_format.dart'); |
14 | 14 |
15 class Intl { | 15 class Intl { |
16 | |
17 /** | 16 /** |
18 * String indicating the locale code with which the message is to be | 17 * String indicating the locale code with which the message is to be |
19 * formatted (such as en-CA). | 18 * formatted (such as en-CA). |
20 */ | 19 */ |
21 String _locale; | 20 String _locale; |
22 | 21 |
23 IntlMessage intlMsg; | 22 IntlMessage intlMsg; |
24 | 23 |
25 DateFormat date; | 24 /** |
25 * Return a new date format using the specified [pattern] or with no | |
26 * pattern if it was omitted. In the second case we assume that pattern | |
27 * will be set later, otherwise the format is not useful. | |
Emily Fortuna
2012/07/31 05:52:53
why make pattern optional?
Alan Knight
2012/08/03 23:02:15
OK, made it required.
| |
28 * If [locale] is not specified, then we default to the locale specified | |
Emily Fortuna
2012/07/31 05:52:53
desiredLocale
Alan Knight
2012/08/03 23:02:15
Done.
| |
29 * in this instance. Note that this is different from using the static | |
Emily Fortuna
2012/07/31 05:52:53
"locale specified in this instance" please clarify
Alan Knight
2012/08/03 23:02:15
Done.
| |
30 * version of this, in which case we will default to the overall default | |
Emily Fortuna
2012/07/31 05:52:53
please rephrase this last sentence. "this" is a bi
Alan Knight
2012/08/03 23:02:15
Moot, since I removed the static version as per a
| |
31 * locale. | |
32 */ | |
33 DateFormat date([String pattern, String desiredLocale]) { | |
34 var actualLocale = (desiredLocale == null) ? _locale : desiredLocale; | |
35 return new DateFormat(pattern,actualLocale); | |
36 } | |
37 | |
38 /** | |
39 * Return a new date format using the specified [pattern] or with no | |
40 * pattern if it was omitted. In the second case we assume that pattern | |
41 * will be set later, otherwise the format is not useful. | |
42 * If [locale] is not specified, then we use the global default locale. | |
43 * Note that this is different from using the instance method | |
44 * version of this, in which case we will default to the locale of that | |
45 * instance. | |
46 */ | |
47 static DateFormat Date([pattern, desiredLocale]) { | |
Emily Fortuna
2012/07/31 05:52:53
types for parameters
Alan Knight
2012/08/03 23:02:15
Method removed.
| |
48 //TODO(alanknight): I don't love the uppercase for static convention here | |
49 // but couldn't come up with better names, and it seems useful to be able | |
50 // to do this both statically and on an instance. | |
Emily Fortuna
2012/07/31 05:52:53
if you're going to do this statically the user mig
Alan Knight
2012/08/03 23:02:15
Done.
| |
51 var actualLocale = (desiredLocale == null) ? defaultLocale: desiredLocale; | |
52 return new DateFormat(pattern,actualLocale); | |
53 } | |
26 | 54 |
27 /** | 55 /** |
28 * Constructor optionally [_locale] for specifics of the language | 56 * Constructor optionally [_locale] for specifics of the language |
29 * locale to be used, otherwise, we will attempt to infer it (acceptable if | 57 * locale to be used, otherwise, we will attempt to infer it (acceptable if |
30 * Dart is running on the client, we can infer from the browser/client | 58 * Dart is running on the client, we can infer from the browser/client |
31 * preferences). | 59 * preferences). |
32 */ | 60 */ |
33 Intl([this._locale]) { | 61 Intl([locale]) { |
62 _locale = locale; | |
34 intlMsg = new IntlMessage(_locale); | 63 intlMsg = new IntlMessage(_locale); |
35 date = new DateFormat(_locale); | |
36 } | 64 } |
37 | 65 |
38 /** | 66 /** |
39 * Create a message that can be internationalized. It takes a | 67 * Create a message that can be internationalized. It takes a |
40 * [message_str] that will be translated, which may be interpolated | 68 * [message_str] that will be translated, which may be interpolated |
41 * based on one or more variables, a [desc] providing a description of usage | 69 * based on one or more variables, a [desc] providing a description of usage |
42 * for the [message_str], and a map of [examples] for each data element to be | 70 * for the [message_str], and a map of [examples] for each data element to be |
43 * substituted into the message. For example, if message="Hello, $name", then | 71 * substituted into the message. For example, if message="Hello, $name", then |
44 * examples = {'name': 'Sparky'}. The values of [desc] and [examples] are | 72 * examples = {'name': 'Sparky'}. The values of [desc] and [examples] are |
45 * not used at run-time but are only made available to the translators, so | 73 * not used at run-time but are only made available to the translators, so |
46 * they MUST be simple Strings available at compile time: no String | 74 * they MUST be simple Strings available at compile time: no String |
47 * interpolation or concatenation. | 75 * interpolation or concatenation. |
48 * The expected usage of this is inside a function that takes as parameters | 76 * The expected usage of this is inside a function that takes as parameters |
49 * the variables used in the interpolated string. | 77 * the variables used in the interpolated string. |
50 */ | 78 */ |
51 String message(String message_str, [final String desc='', | 79 String message(String message_str, [final String desc='', |
52 final Map examples=const {}]) { | 80 final Map examples=const {}]) { |
53 return message_str; | 81 return message_str; |
54 } | 82 } |
55 | 83 |
84 /** The default locale for this environment. */ | |
85 //TODO(alanknight): Return the correct locale using either | |
86 // window.navigator.language or appropriate environment variable or other | |
87 // mechanism for command-line apps. | |
88 static String defaultLocale = 'en_US'; | |
89 | |
90 /** | |
91 * Return the locale for this instance. If none was set, returns the | |
92 * default. | |
93 */ | |
94 String get locale() { | |
95 if (_locale == null) { | |
Emily Fortuna
2012/07/31 05:52:53
please sync your files before submitting for revie
| |
96 return defaultLocale; | |
97 } else { | |
98 return _locale;} | |
99 } | |
100 | |
56 /** | 101 /** |
57 * Support method for message formatting. Select the correct plural form from | 102 * Support method for message formatting. Select the correct plural form from |
58 * [cases] given [howMany]. | 103 * [cases] given [howMany]. |
59 */ | 104 */ |
60 static String plural(var howMany, Map cases, [num offset=0]) { | 105 static String plural(var howMany, Map cases, [num offset=0]) { |
61 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! | 106 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! |
62 // TODO(alanknight): Should we have instance methods instead/as well? | 107 // TODO(alanknight): Should we have instance methods instead/as well? |
63 // Or have the others as statics? | 108 // Or have the others as statics? |
64 return select(howMany.toString(), cases); | 109 return select(howMany.toString(), cases); |
65 } | 110 } |
66 | 111 |
67 /** | 112 /** |
68 * Support method for message formatting. Select the correct exact (gender, | 113 * Support method for message formatting. Select the correct exact (gender, |
69 * usually) form from [cases] given the user [choice]. | 114 * usually) form from [cases] given the user [choice]. |
70 */ | 115 */ |
71 static String select(String choice, Map cases) { | 116 static String select(String choice, Map cases) { |
72 if (cases.containsKey(choice)) { | 117 if (cases.containsKey(choice)) { |
73 return cases[choice]; | 118 return cases[choice]; |
74 } else if (cases.containsKey('other')){ | 119 } else if (cases.containsKey('other')){ |
75 return cases['other']; | 120 return cases['other']; |
76 } else { | 121 } else { |
77 return ''; | 122 return ''; |
78 } | 123 } |
79 } | 124 } |
80 } | 125 } |
OLD | NEW |