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

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

Issue 10494005: Updated message formatting to be more compatible with Dart string interpolation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixed up some merge errors, tweaked comments a little. 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
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 * Message/plural format library with locale support. 6 * Library for internationalizing and localizing user messages, including
7 * support for customizing them based on plurals and genders.
7 * 8 *
8 * Message format grammar: 9 * Messages are written as functions with either one parameter (which can be
10 * omitted). The function name serves as an identifier for the message, and is
11 * customarily given a unique prefix such as intl_ to distinguish it from
12 * application functions.
13 * The function is expected to return a single interpolated string, based
14 * on the parameter (which can be a Map or a complex object).
9 * 15 *
10 * messageFormatPattern := string ( "{" messageFormatElement "}" string )* 16 * For example
11 * messageFormatElement := argumentIndex [ "," elementFormat ] 17 * intl_helloWorld () => "Hello world";
12 * elementFormat := "plural" "," pluralStyle 18 * intl_oneValue (waiting) => "There are $waiting jobs waiting";
13 * | "select" "," selectStyle 19 * intl_dict (dict) =>
14 * pluralStyle := pluralFormatPattern 20 * "Hello ${dict['name']}, your waiting time is ${dict['minutes']} minutes";
15 * selectStyle := selectFormatPattern
16 * pluralFormatPattern := \\[ "offset" ":" offsetIndex ] pluralForms*
17 * selectFormatPattern := pluralForms*
18 * pluralForms := stringKey "{" ( "{" messageFormatElement "}"|string )* "}"
19 * 21 *
22 * More complex formatting can make use of the Intl object and associated
23 * message format.
24 * Here is a more complete example of usage with complex formatting
25 * var intl = new Intl();
26 * intl_howManyPeopleAreHere (NUM)
27 * //@desc Lists how many people are here
28 * => "There ${intl.plural(NUM,{'0': 'are', '1': 'is', 'other': 'are'})} "
29 * "$NUM other ${intl.plural(NUM, {'1':'person', 'other': 'people'})} here.";
30 * var msg_format = new MessageFormat(intl_howManyPeopleAreHere);
31 * msg_format.format(2);
20 * 32 *
21 * Message example: 33 * See tests/message_format_test.dart for more comprehensive examples.
22 *
23 * I see {NUM_PEOPLE, plural, offset:1
24 * =0 {no one at all}
25 * =1 {{WHO}}
26 * one {{WHO} and one other person}
27 * other {{WHO} and # other people}}
28 * in {PLACE}.
29 *
30 * Calling format({'NUM_PEOPLE': 2, 'WHO': 'Mark', 'PLACE': 'Athens'}) would
31 * produce "I see Mark and one other person in Athens." as output.
32 *
33 * See tests/message_format_test.dart for more examples.
34 */ 34 */
35 35
36 #library('MessageFormat'); 36 #library('MessageFormat');
37 #import('intl.dart');
37 38
38 class MessageFormat { 39 class MessageFormat {
39 40
40 /** 41 /**
41 * String that is used to determin the particular case and gender needed to be 42 * The definition of this message. This should be a function which returns
42 * returned. The format of this string follows the same pattern as in Closure, 43 * a string. The string may use Dart's string interpolation feature to
43 * Java, and C++. This pattern is described at the beginning of this class 44 * substitute values based on the function's parameter.
44 * definition. See tests/message_format_test.dart for more examples. 45 * See tests/message_format_test.dart for more examples.
45 */ 46 */
46 final String _messageFunction; 47 final Function _messageFunction;
47 48
48 /** 49 /**
49 * String indicating a language code with which the message is to be 50 * String indicating a locale code with which the message is to be
50 * formatted (such as en-US). 51 * formatted (such as en-CA).
51 */ 52 */
53
Emily Fortuna 2012/06/07 21:33:36 remove extra whitespace
Alan Knight 2012/06/07 21:58:53 Done.
52 final String _locale; 54 final String _locale;
53 55
54 /** 56 /**
55 * Constructor. The constructor expects you do provide annotations in comments 57 * Constructor. The constructor expects you do provide annotations in comments
56 * above your declaration of this constructor with an @desc describing the 58 * above your declaration of this constructor with an @desc describing the
57 * context for the useage of the function. You may also specify @ex to specify 59 * context for the useage of the function. You may also specify @ex to specify
58 * examples of inputs for each particular argument that the [_messageFunction] 60 * examples of inputs for each particular argument that the [_messageFunction]
59 * accepts. The String [_messageFunction] is used to determine the particular 61 * accepts. The String [_messageFunction] is used to determine the particular
60 * case and gender for the given instance. An optional [_locale] can be 62 * case and gender for the given instance. An optional [_locale] can be
61 * provided for specifics of the language locale to be used, otherwise, we 63 * provided for specifics of the language locale to be used, otherwise, we
62 * will attempt to infer it (acceptable if Dart is running on the client, we 64 * will attempt to infer it (acceptable if Dart is running on the client, we
63 * can infer from the browser). 65 * can infer from the browser).
64 */ 66 */
65 //TODO(efortuna): _locale is not currently inferred. 67 //TODO(efortuna): _locale is not currently inferred.
66 const MessageFormat(this._messageFunction, [this._locale = 'en-US']); 68 const MessageFormat([this._messageFunction, this._locale = 'en-US']);
Emily Fortuna 2012/06/07 21:33:36 why is messageFunction optional? don't we want to
Alan Knight 2012/06/07 21:58:53 We have a version of format below which can take a
67 69
68 /** 70 /**
69 * Formats a message. By default, we treat '#' with special meaning 71 * Formats the _messageFunction message and returns the correctly
70 * representing the number (plural_variable - plural_offset). If [ignorePound] 72 * formatted message in the language of the current locale.
71 * is true, then we do not treat '#' as a special character, and it is just 73 *
72 * treated literally. [namedParameters] is a map of String keys to String or 74 * The variable [messageParameters] can be null, in which case the message
73 * int values to influence the formatting of the message or data in the 75 * function is called with no arguments. Otherwise, it is called with
74 * message. For example, example, in the call to 76 * [messageParameters] as the single argument. If the message function
75 * msg_formatter.format({'NUM_PEOPLE': 5, 'NAME': 'Angela'}), 77 * requires more than one variable, then messageParameters can be a
76 * object \{'NUM_PEOPLE': 5, 'NAME': 'Angela'\} holds positional parameters. 78 * map with the values in it.
77 * 1st parameter could mean 5 people, which could influence plural format, 79 */
Emily Fortuna 2012/06/07 21:33:36 add space back in please
Alan Knight 2012/06/07 21:58:53 Done.
78 * and 2nd parameter is just relevant data to be printed out in the proper 80 String format(var messageParameters) {
79 * position in the message. 81 return _messageFunction(messageParameters);
80 * Returns the correctly formatted message in the desired language. 82 }
81 */ 83
82 String format(Map<String, dynamic> messageParameters, 84 /**
83 [bool ignorePound=false]) { 85 * Formats the [messageFunction] argument and returns the correctly
84 // TODO(efortuna): actually perform the translation here. For now, I'm just 86 * formatted message in the language of the current locale.
85 // returning a description of the message to be returned. 87 *
86 return _messageDescription; 88 * The variable [messageParameters] can be null, in which case the message
89 * function is called with no arguments. Otherwise, it is called with
90 * messageParameters as the single argument. If the message function
91 * requires more than one variable, then messageParameters can be a
92 * map with the values in it.
93 *
94 * This differs from format only in that the message is passed as an argument
95 */
Emily Fortuna 2012/06/07 21:33:36 add one more space please
Alan Knight 2012/06/07 21:58:53 Done.
96 String formatMessage(Function messageFunction,var messageParameters) {
97 if (messageParameters == null) {
98 return messageFunction();
Emily Fortuna 2012/06/07 21:33:36 there are three spaces indented here and it should
Alan Knight 2012/06/07 21:58:53 Done.
99 } else {
100 return messageFunction(messageParameters);
Emily Fortuna 2012/06/07 21:33:36 same here.
Alan Knight 2012/06/07 21:58:53 Done.
101 }
87 } 102 }
88 } 103 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698