Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 omitte d). |
|
Emily Fortuna
2012/06/02 01:19:26
add line breaks for 80 char here and below
Alan Knight
2012/06/02 02:19:44
Done.
| |
| 9 * | 10 * The function name serves as an identifier for the message, and is customarily |
| 10 * messageFormatPattern := string ( "{" messageFormatElement "}" string )* | 11 * given a unique prefix such as intl_ to distinguish it from application functi ons. |
| 11 * messageFormatElement := argumentIndex [ "," elementFormat ] | 12 * The function is expected to return a single interpolated string, based on the |
| 12 * elementFormat := "plural" "," pluralStyle | 13 * parameter (which can be a Map or a complex object). |
| 13 * | "select" "," selectStyle | 14 * |
| 14 * pluralStyle := pluralFormatPattern | 15 * For example |
| 15 * selectStyle := selectFormatPattern | 16 * intl_helloWorld () => "Hello world"; |
| 16 * pluralFormatPattern := \\[ "offset" ":" offsetIndex ] pluralForms* | 17 * intl_oneValue (waiting) => "There are $waiting jobs waiting"; |
| 17 * selectFormatPattern := pluralForms* | 18 * intl_dict (dict) => "Hello ${dict['name']}, your waiting time is ${dict['mi nutes']} minutes"; |
| 18 * pluralForms := stringKey "{" ( "{" messageFormatElement "}"|string )* "}" | 19 * |
| 20 * More complex formatting can make use of the Intl object and associated messag e format. | |
| 21 * Here is a more complete example of usage with complex formatting | |
| 22 * var intl = new Intl(); | |
| 23 * intl_howManyPeopleAreHere (NUM) | |
| 24 * //@desc Lists how many people are here | |
| 25 * => "There ${intl.plural(NUM,{'0': 'are', '1': 'is', 'other': 'are'})} " | |
| 26 *» "$NUM other ${intl.plural(NUM, {'1':'person', 'other': 'people'})} h ere."; | |
|
Emily Fortuna
2012/06/02 01:19:26
get rid of tabs in this line and below --> only sp
Alan Knight
2012/06/02 02:19:44
Done.
| |
| 27 * » var msg_format = new MessageFormat(intl_howManyPeopleAreHere); | |
| 28 * msg_format.format(2); | |
| 19 * | 29 * |
| 20 * | 30 * |
| 21 * Message example: | 31 * 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 */ | 32 */ |
| 35 | 33 |
| 36 #library('MessageFormat'); | 34 #library('MessageFormat'); |
| 35 #import('intl.dart'); | |
| 37 | 36 |
| 38 class MessageFormat { | 37 class MessageFormat { |
| 39 | 38 |
| 40 /** | 39 /** |
| 41 * Literal strings, including '', are replaced with \uFDDF_x_ for | 40 * The message which we are formatting. |
|
Emily Fortuna
2012/06/02 01:19:26
if it's a one line comment, we can shorten it to:
Alan Knight
2012/06/02 02:19:44
Done.
| |
| 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 */ | 41 */ |
| 46 final String _LITERAL_PLACEHOLDER = '\uFDDF_'; | 42 Function _messageFunction; |
| 47 | |
| 48 /** String describing the use case for this message.*/ | |
| 49 final String _messageDescription; | |
| 50 | 43 |
| 51 /** | 44 /** |
| 52 * String that is used to determin the particular case and gender needed to be | 45 * Constructors. 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 */ | 46 */ |
| 57 final String _messageFunction; | 47 MessageFormat.on(this._messageFunction); |
|
Emily Fortuna
2012/06/02 01:19:26
My impression so far is Dart is much more likely t
Alan Knight
2012/06/02 02:19:44
Changed to an optional parameter
| |
| 48 MessageFormat(); | |
| 58 | 49 |
| 59 /** | 50 /** |
| 60 * Constructor. Accepts a String [_messageDescription] describing the use case | 51 * Formats a message and returns the correctly formatted message in the desire d language. |
| 61 * for this string in the program, and a the String [_messageFunction] that is | 52 * There are two versions, one which assumes we were constructed with a partic ular message |
| 62 * used to determine the particular case and gender for the given instance. | 53 * and one where the message is passed in as an argument. |
|
Emily Fortuna
2012/06/02 01:19:26
comments please explaining what messageParameters
Alan Knight
2012/06/02 02:19:44
Done.
| |
| 63 */ | 54 */ |
| 64 const MessageFormat(this._messageDescription, this._messageFunction); | |
| 65 | 55 |
| 66 /** | 56 String format(var messageParameters) { |
| 67 * Formats a message. By default, we treat '#' with special meaning | 57 return _messageFunction(messageParameters); |
| 68 * representing the number (plural_variable - plural_offset). If [ignorePound] | |
| 69 * is true, then we do not treat '#' as a special character, and it is just | |
| 70 * treated literally. [namedParameters] is a map of String keys to String or | |
| 71 * int values to influence the formatting of the message or data in the | |
| 72 * message. For example, example, in the call to | |
| 73 * msg_formatter.format({'NUM_PEOPLE': 5, 'NAME': 'Angela'}), | |
| 74 * object \{'NUM_PEOPLE': 5, 'NAME': 'Angela'\} holds positional parameters. | |
| 75 * 1st parameter could mean 5 people, which could influence plural format, | |
| 76 * and 2nd parameter is just relevant data to be printed out in the proper | |
| 77 * position in the message. | |
| 78 * Returns the correctly formatted message in the desired language. | |
| 79 */ | |
| 80 String format(Map<String, dynamic> messageParameters, | |
| 81 [bool ignorePound=false]) { | |
| 82 // TODO(efortuna): actually perform the translation here. For now, I'm just | |
| 83 // returning a description of the message to be returned. | |
| 84 return _messageDescription; | |
| 85 } | 58 } |
| 59 | |
| 60 String formatMessage(Function messageFunction,var messageParameters) { | |
|
Emily Fortuna
2012/06/02 01:19:26
comments please?
| |
| 61 if (messageParameters == null) { | |
| 62 return messageFunction(); | |
| 63 } else { | |
| 64 return messageFunction(messageParameters); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 | |
| 86 } | 69 } |
| OLD | NEW |