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

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') | no next file with comments »
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 = _getDefaultLocale();
Alan Knight 2012/06/12 17:47:20 I think this would have to change in line with the
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([this._locale]) {
34 intlMsg = new IntlMessage(_locale); 34 intlMsg = new IntlMessage(_locale);
35 date = new DateFormat(_locale); 35 date = new DateFormat(_locale);
36 } 36 }
37 37
38 /** 38 /**
39 * Create a message that can be internationalized. It takes a 39 * Create a message that can be internationalized. It takes a
40 * [message_str] that will be translated, which may be interpolated 40 * [message_str] that will be translated, which may be interpolated
41 * based on one or more variables, a [desc] providing a description of usage 41 * 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 42 * 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 43 * substituted into the message. For example, if message="Hello, $name", then
44 * examples = {'name': 'Sparky'}. The values of [desc] and [examples] are 44 * 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 45 * if the locale is not easily detectable, explicitly pass [locale].
46 * they MUST be simple Strings available at compile time: no String 46 * The values of [desc] and [examples] are not used at run-time but are only
47 * interpolation or concatenation. 47 * made available to the translators, so they MUST be simple Strings available
48 * at compile time: no String interpolation or concatenation.
48 * The expected usage of this is inside a function that takes as parameters 49 * The expected usage of this is inside a function that takes as parameters
49 * the variables used in the interpolated string. 50 * the variables used in the interpolated string, and additionally also a
51 * locale (optional).
50 */ 52 */
51 String message(String message_str, [final String desc='', 53 static String message(String message_str, [final String desc='',
52 final Map examples=const {}]) { 54 final Map examples=const {}, String locale='']) {
Alan Knight 2012/06/12 17:47:20 Does it make sense now to have the locale paramete
53 return message_str; 55 return message_str;
54 } 56 }
55 57
56 /** 58 /**
57 * Support method for message formatting. Select the correct plural form from 59 * Support method for message formatting. Select the correct plural form from
58 * [cases] given [howMany]. 60 * [cases] given [howMany].
59 */ 61 */
60 static String plural(var howMany, Map cases, [num offset=0]) { 62 static String plural(var howMany, Map cases, [num offset=0]) {
61 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! 63 // 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); 64 return select(howMany.toString(), cases);
65 } 65 }
66 66
67 /** 67 /**
68 * Format the given function with a specific [locale], given a [msg_function]
69 * that takes no parameters and returns a String.
70 */
71 static String withLocale(String locale, Function msg_function) {
72 var oldLocale = _locale;
73 _locale = locale;
74 var result = msg_function();
75 _locale = oldLocale;
76 return result;
77 }
78
79 /**
68 * Support method for message formatting. Select the correct exact (gender, 80 * Support method for message formatting. Select the correct exact (gender,
69 * usually) form from [cases] given the user [choice]. 81 * usually) form from [cases] given the user [choice].
70 */ 82 */
71 static String select(String choice, Map cases) { 83 static String select(String choice, Map cases) {
72 if (cases.containsKey(choice)) { 84 if (cases.containsKey(choice)) {
73 return cases[choice]; 85 return cases[choice];
74 } else if (cases.containsKey('other')){ 86 } else if (cases.containsKey('other')){
75 return cases['other']; 87 return cases['other'];
76 } else { 88 } else {
77 return ''; 89 return '';
78 } 90 }
79 } 91 }
92
93 /**
94 * Helper to detect the locale as defined at runtime.
95 */
96 static String _getDefaultLocale() {
97 // TODO(efortuna): Detect the default locale given the user preferences.
98 // Yay, hard-coding for now!
99 return 'en-US';
Alan Knight 2012/06/12 17:47:20 Doesn't this need to be if (!(_locale == null))
100 }
80 } 101 }
OLDNEW
« no previous file with comments | « no previous file | lib/i18n/intl_message.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698