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 * Message/plural format library with locale support. |
| 7 * | 7 * |
| 8 * Message format grammar: | |
| 9 * | |
| 10 * messageFormatPattern := string ( "{" messageFormatElement "}" string )* | |
| 11 * messageFormatElement := argumentIndex [ "," elementFormat ] | |
| 12 * elementFormat := "plural" "," pluralStyle | |
| 13 * | "select" "," selectStyle | |
| 14 * pluralStyle := pluralFormatPattern | |
| 15 * selectStyle := selectFormatPattern | |
| 16 * pluralFormatPattern := \\[ "offset" ":" offsetIndex ] pluralForms* | |
| 17 * selectFormatPattern := pluralForms* | |
| 18 * pluralForms := stringKey "{" ( "{" messageFormatElement "}"|string )* "}" | |
| 19 * | |
| 20 * | 8 * |
| 21 * Message example: | 9 * Message example: |
| 10 * '''I see ${intl.plural(NUM_PEOPLE, | |
|
Alan Knight
2012/06/06 22:21:01
Why are the triple quotes here needed?
Also, I thi
Bob Nystrom
2012/06/06 22:22:48
Do we expect the params to be SCREAMING_CAPS like
Emily Fortuna
2012/06/06 22:51:43
Triple quotes so I don't have to quote and unquote
| |
| 11 * {'0': 'no one at all', | |
| 12 * '1': 'one other person', | |
| 13 * 'other': '$NUM_PEOPLE other people'})} in $PLACE. | |
| 22 * | 14 * |
| 23 * I see {NUM_PEOPLE, plural, offset:1 | 15 * Calling format({'NUM_PEOPLE': 2, 'PLACE': 'Athens'}) would |
|
Bob Nystrom
2012/06/06 22:22:48
Use [: :] or backticks (markdown is backticks) to
Emily Fortuna
2012/06/06 22:51:43
Done.
| |
| 24 * =0 {no one at all} | 16 * produce "I see 2 other people in Athens." as output. |
| 25 * =1 {{WHO}} | 17 * //TODO(efortuna): documentation example involving the offset parameter. |
| 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 * | 18 * |
| 33 * See tests/message_format_test.dart for more examples. | 19 * See tests/message_format_test.dart for more examples. |
| 34 */ | 20 */ |
| 35 | 21 |
| 36 #library('MessageFormat'); | 22 #library('IntlMessage'); |
|
Bob Nystrom
2012/06/06 22:22:48
Our naming convention for libraries is lowercase_w
Emily Fortuna
2012/06/06 22:51:43
Done.
| |
| 37 | 23 |
| 38 class MessageFormat { | 24 class IntlMessage { |
| 25 | |
| 26 /** String describing the use case for this message. */ | |
| 27 String _messageDescription; | |
| 39 | 28 |
| 40 /** | 29 /** |
| 41 * String that is used to determin the particular case and gender needed to be | 30 * String that contains the message to be displayed. It may contain |
| 42 * returned. The format of this string follows the same pattern as in Closure, | 31 * placeholders in the form of string interpolated items (ie 'hello$foo') that |
| 43 * Java, and C++. This pattern is described at the beginning of this class | 32 * will be substituted in to complete the message. |
| 44 * definition. See tests/message_format_test.dart for more examples. | 33 * See tests/message_format_test.dart for more examples. |
| 45 */ | 34 */ |
| 46 final String _messageFunction; | 35 String _message; |
| 47 | 36 |
| 48 /** | 37 /** |
| 49 * String indicating a language code with which the message is to be | 38 * String indicating the locale code with which the message is to be |
| 50 * formatted (such as en-US). | 39 * formatted (such as en-CA). |
| 51 */ | 40 */ |
| 52 final String _locale; | 41 String _languageCode; |
| 42 | |
| 43 /** Examples of the placeholders used in the message string. */ | |
| 44 Map _messageExamples; | |
| 53 | 45 |
| 54 /** | 46 /** |
| 55 * Constructor. The constructor expects you do provide annotations in comments | 47 * Constructor. Accepts a String [_message] that contains the message (that |
|
Bob Nystrom
2012/06/06 22:22:48
Remove "Constructor."
Emily Fortuna
2012/06/06 22:51:43
Done.
| |
| 56 * above your declaration of this constructor with an @desc describing the | 48 * will be internationalized). A String [_messageDescription] describes the |
| 57 * context for the useage of the function. You may also specify @ex to specify | 49 * use case for this string in the program, and [examples] provides a map of |
| 58 * examples of inputs for each particular argument that the [_messageFunction] | 50 * examples for the items (if any) to be subsituted into [_message]. |
| 59 * accepts. The String [_messageFunction] is used to determine the particular | 51 * It also optionally accepts a [_languageCode] to specify the particular |
| 60 * case and gender for the given instance. An optional [_locale] can be | 52 * language to return content in (necessary if the Dart code is not running in |
| 61 * provided for specifics of the language locale to be used, otherwise, we | 53 * a browser). The values of [desc] and [examples] MUST be |
|
Bob Nystrom
2012/06/06 22:22:48
Use emphasis instead of all caps: "MUST" -> "*must
Emily Fortuna
2012/06/06 22:51:43
Done.
| |
| 62 * will attempt to infer it (acceptable if Dart is running on the client, we | 54 * simple Strings available at compile time: no String interpolation or |
| 63 * can infer from the browser). | 55 * concatenation. |
| 64 */ | 56 */ |
| 65 //TODO(efortuna): _locale is not currently inferred. | 57 IntlMessage(this._message, [final String desc='', final Map examples=const {}, |
| 66 const MessageFormat(this._messageFunction, [this._locale = 'en-US']); | 58 final String locale='']) { |
| 59 this._messageDescription = desc; | |
| 60 this._messageExamples = examples; | |
| 61 this._languageCode = locale; | |
| 62 } | |
| 67 | 63 |
| 68 /** | |
| 69 * Formats a message. By default, we treat '#' with special meaning | |
| 70 * representing the number (plural_variable - plural_offset). If [ignorePound] | |
| 71 * is true, then we do not treat '#' as a special character, and it is just | |
| 72 * treated literally. [namedParameters] is a map of String keys to String or | |
| 73 * int values to influence the formatting of the message or data in the | |
| 74 * message. For example, example, in the call to | |
| 75 * msg_formatter.format({'NUM_PEOPLE': 5, 'NAME': 'Angela'}), | |
| 76 * object \{'NUM_PEOPLE': 5, 'NAME': 'Angela'\} holds positional parameters. | |
| 77 * 1st parameter could mean 5 people, which could influence plural format, | |
| 78 * and 2nd parameter is just relevant data to be printed out in the proper | |
| 79 * position in the message. | |
| 80 * Returns the correctly formatted message in the desired language. | |
| 81 */ | |
| 82 String format(Map<String, dynamic> messageParameters, | |
| 83 [bool ignorePound=false]) { | |
| 84 // TODO(efortuna): actually perform the translation here. For now, I'm just | |
| 85 // returning a description of the message to be returned. | |
| 86 return _messageDescription; | |
| 87 } | |
| 88 } | 64 } |
| OLD | NEW |