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

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: And a few more cleanups. 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 21 *
16 * pluralFormatPattern := \\[ "offset" ":" offsetIndex ] pluralForms* 22 * More complex formatting can make use of the Intl object and associated
17 * selectFormatPattern := pluralForms* 23 * message format.
18 * pluralForms := stringKey "{" ( "{" messageFormatElement "}"|string )* "}" 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
Emily Fortuna 2012/06/06 22:57:33 This syntax isn't the latest anymore. :-(
Alan Knight 2012/06/06 23:08:09 Neither is any of the rest of it. Isn't that somet
Emily Fortuna 2012/06/06 23:13:16 Yes. I was expecting you to upload a merged versio
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);
19 * 32 *
20 * 33 *
21 * Message example: 34 * 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 */ 35 */
35 36
36 #library('MessageFormat'); 37 #library('MessageFormat');
38 #import('intl.dart');
37 39
38 class MessageFormat { 40 class MessageFormat {
39 41
40 /** 42 /** The message which we are formatting. */
41 * Literal strings, including '', are replaced with \uFDDF_x_ for 43 Function _messageFunction;
42 * parsing purposes, and recovered during format phase.
43 * \uFDDF is a Unicode nonprinting character, not expected to be found in the
44 * typical message.
45 */
46 final String _LITERAL_PLACEHOLDER = '\uFDDF_';
47
48 /** String describing the use case for this message.*/
49 final String _messageDescription;
50 44
51 /** 45 /**
52 * String that is used to determin the particular case and gender needed to be 46 * Constructor. Can take a particular message, or have that left open.
53 * returned. The format of this string follows the same pattern as in Closure,
54 * Java, and C++. This pattern is described at the beginning of this class
55 * definition. See tests/message_format_test.dart for more examples.
56 */ 47 */
57 final String _messageFunction; 48 MessageFormat([this._messageFunction]);
58 49
59 /** 50 /**
60 * Constructor. Accepts a String [_messageDescription] describing the use case 51 * Formats the _messageFunction message and returns the correctly
61 * for this string in the program, and a the String [_messageFunction] that is 52 * formatted message in the language of the current locale.
62 * used to determine the particular case and gender for the given instance. 53 *
54 * The variable [messageParameters] can be null, in which case the message
55 * function is called with no arguments. Otherwise, it is called with
56 * [messageParameters] as the single argument. If the message function
57 * requires more than one variable, then messageParameters can be a
58 * map with the values in it.
63 */ 59 */
64 const MessageFormat(this._messageDescription, this._messageFunction); 60
Emily Fortuna 2012/06/06 22:57:33 extra whitespace.
Alan Knight 2012/06/06 23:08:09 Done.
61 String format(var messageParameters) {
62 return _messageFunction(messageParameters);
63 }
65 64
66 /** 65 /**
67 * Formats a message. By default, we treat '#' with special meaning 66 * Formats the [messageFunction] argument and returns the correctly
68 * representing the number (plural_variable - plural_offset). If [ignorePound] 67 * formatted message in the language of the current locale.
69 * is true, then we do not treat '#' as a special character, and it is just 68 *
70 * treated literally. [namedParameters] is a map of String keys to String or 69 * The variable [messageParameters] can be null, in which case the message
71 * int values to influence the formatting of the message or data in the 70 * function is called with no arguments. Otherwise, it is called with
72 * message. For example, example, in the call to 71 * messageParameters as the single argument. If the message function
73 * msg_formatter.format({'NUM_PEOPLE': 5, 'NAME': 'Angela'}), 72 * requires more than one variable, then messageParameters can be a
74 * object \{'NUM_PEOPLE': 5, 'NAME': 'Angela'\} holds positional parameters. 73 * map with the values in it.
75 * 1st parameter could mean 5 people, which could influence plural format, 74 *
76 * and 2nd parameter is just relevant data to be printed out in the proper 75 * This differs from format only in that the message is passed as an argument
77 * position in the message.
78 * Returns the correctly formatted message in the desired language.
79 */ 76 */
80 String format(Map<String, dynamic> messageParameters, 77
Emily Fortuna 2012/06/06 22:57:33 extra whitespace.
Alan Knight 2012/06/06 23:08:09 Done.
81 [bool ignorePound=false]) { 78 String formatMessage(Function messageFunction,var messageParameters) {
82 // TODO(efortuna): actually perform the translation here. For now, I'm just 79 if (messageParameters == null) {
83 // returning a description of the message to be returned. 80 return messageFunction();
84 return _messageDescription; 81 } else {
82 return messageFunction(messageParameters);
83 }
85 } 84 }
85
86
Emily Fortuna 2012/06/06 22:57:33 2 lines of extra whitespace.
Alan Knight 2012/06/06 23:08:09 Done.
86 } 87 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698