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

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

Issue 10536105: Make Intl.message static (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 6 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 | lib/i18n/intl_message.dart » ('j') | tests/lib/i18n/intl_message_test.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /** 1 /**
2 * Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 4 * BSD-style license that can be found in the LICENSE file.
5 * 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 16
17 /** 17 /**
18 * String indicating the locale code with which the message is to be 18 * String indicating the locale code with which the message is to be
19 * formatted (such as en-CA). 19 * formatted (such as en-CA).
20 */ 20 */
21 String _locale; 21 static String _locale;
22 22
23 IntlMessage intlMsg; 23 IntlMessage intlMsg;
24 24
25 DateFormat date; 25 DateFormat date;
26 26
27 /** 27 /**
28 * Constructor optionally [_locale] for specifics of the language 28 * Constructor optionally [_locale] for specifics of the language
29 * locale to be used, otherwise, we will attempt to infer it (acceptable if 29 * 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 30 * Dart is running on the client, we can infer from the browser/client
31 * preferences). 31 * preferences).
32 */ 32 */
33 Intl([this._locale]) { 33 Intl([a_locale]) {
34 _locale = a_locale;
35 if (_locale == null) _locale = _getDefaultLocale();
34 intlMsg = new IntlMessage(_locale); 36 intlMsg = new IntlMessage(_locale);
35 date = new DateFormat(_locale); 37 date = new DateFormat(_locale);
36 } 38 }
37 39
38 /** 40 /**
39 * Create a message that can be internationalized. It takes a 41 * Create a message that can be internationalized. It takes a
40 * [message_str] that will be translated, which may be interpolated 42 * [message_str] that will be translated, which may be interpolated
41 * based on one or more variables, a [desc] providing a description of usage 43 * 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 44 * 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 45 * substituted into the message. For example, if message="Hello, $name", then
44 * examples = {'name': 'Sparky'}. The values of [desc] and [examples] are 46 * examples = {'name': 'Sparky'}. If not using the user's default locale, or
45 * not used at run-time but are only made available to the translators, so 47 * if the locale is not easily detectable, explicitly pass [locale].
46 * they MUST be simple Strings available at compile time: no String 48 * The values of [desc] and [examples] are not used at run-time but are only
47 * interpolation or concatenation. 49 * made available to the translators, so they MUST be simple Strings available
50 * at compile time: no String interpolation or concatenation.
48 * The expected usage of this is inside a function that takes as parameters 51 * The expected usage of this is inside a function that takes as parameters
49 * the variables used in the interpolated string. 52 * the variables used in the interpolated string, and additionally also a
53 * locale (optional).
50 */ 54 */
51 String message(String message_str, [final String desc='', 55 static String message(String message_str, [final String desc='',
52 final Map examples=const {}]) { 56 final Map examples=const {}, String locale='']) {
53 return message_str; 57 return message_str;
54 } 58 }
55 59
56 /** 60 /**
57 * Support method for message formatting. Select the correct plural form from 61 * Support method for message formatting. Select the correct plural form from
58 * [cases] given [howMany]. 62 * [cases] given [howMany].
59 */ 63 */
60 static String plural(var howMany, Map cases, [num offset=0]) { 64 static String plural(var howMany, Map cases, [num offset=0]) {
61 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! 65 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others!
62 // TODO(alanknight): Should we have instance methods instead/as well?
63 // Or have the others as statics?
64 return select(howMany.toString(), cases); 66 return select(howMany.toString(), cases);
65 } 67 }
66 68
67 /** 69 /**
70 * Format the given function with a specific [locale], given a [msg_function]
71 * that takes no parameters and returns a String.
72 */
73 static String withLocale(String locale, Function msg_function) {
74 // We have to do this silliness because Locale is not known at compile time,
75 // but must be a static variable.
76 if (_locale == null) _locale = _getDefaultLocale();
77 var oldLocale = _locale;
78 _locale = locale;
79 var result = msg_function();
80 _locale = oldLocale;
81 return result;
Alan Knight 2012/06/13 00:52:32 If I set the locale here temporarily, how does the
Emily Fortuna 2012/06/13 01:27:48 This is where the we take advantage of the fact th
82 }
83
84 /**
68 * Support method for message formatting. Select the correct exact (gender, 85 * Support method for message formatting. Select the correct exact (gender,
69 * usually) form from [cases] given the user [choice]. 86 * usually) form from [cases] given the user [choice].
70 */ 87 */
71 static String select(String choice, Map cases) { 88 static String select(String choice, Map cases) {
72 if (cases.containsKey(choice)) { 89 if (cases.containsKey(choice)) {
73 return cases[choice]; 90 return cases[choice];
74 } else if (cases.containsKey('other')){ 91 } else if (cases.containsKey('other')){
75 return cases['other']; 92 return cases['other'];
76 } else { 93 } else {
77 return ''; 94 return '';
78 } 95 }
79 } 96 }
97
98 /**
99 * Helper to detect the locale as defined at runtime.
100 */
101 static String _getDefaultLocale() {
102 // TODO(efortuna): Detect the default locale given the user preferences.
103 // Yay, hard-coding for now!
104 return 'en-US';
105 }
80 } 106 }
OLDNEW
« no previous file with comments | « no previous file | lib/i18n/intl_message.dart » ('j') | tests/lib/i18n/intl_message_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698