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') | 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;
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
71 * [msg_function_future] that takes no parameters and returns a String. We
72 * basically delay calling the message function proper until after the proper
73 * locale has been set.
74 */
75 static String withLocale(String locale, Function msg_function_future) {
Alan Knight 2012/06/13 15:37:09 "future" seems like a confusing suffix, since ther
Emily Fortuna 2012/06/13 17:24:04 Agreed. But what do you recommend instead? msg_fun
76 // We have to do this silliness because Locale is not known at compile time,
77 // but must be a static variable.
78 if (_locale == null) _locale = _getDefaultLocale();
79 var oldLocale = _locale;
80 _locale = locale;
81 var result = msg_function_future();
Alan Knight 2012/06/13 15:37:09 In the longer term, we'd probably want to use a fi
82 _locale = oldLocale;
83 return result;
84 }
85
86 /**
68 * Support method for message formatting. Select the correct exact (gender, 87 * Support method for message formatting. Select the correct exact (gender,
69 * usually) form from [cases] given the user [choice]. 88 * usually) form from [cases] given the user [choice].
70 */ 89 */
71 static String select(String choice, Map cases) { 90 static String select(String choice, Map cases) {
72 if (cases.containsKey(choice)) { 91 if (cases.containsKey(choice)) {
73 return cases[choice]; 92 return cases[choice];
74 } else if (cases.containsKey('other')){ 93 } else if (cases.containsKey('other')){
75 return cases['other']; 94 return cases['other'];
76 } else { 95 } else {
77 return ''; 96 return '';
78 } 97 }
79 } 98 }
99
100 /**
101 * Helper to detect the locale as defined at runtime.
102 */
103 static String _getDefaultLocale() {
104 // TODO(efortuna): Detect the default locale given the user preferences.
105 // Yay, hard-coding for now!
106 return 'en-US';
107 }
108
109 /**
110 * Accessor for the current locale. This should always == the default locale,
111 * unless for some reason this gets called inside a message that resets the
112 * locale.
113 */
114 static String getCurrentLocale() {
115 return _locale;
116 }
80 } 117 }
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