| 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: | 8 * Message format grammar: |
| 9 * | 9 * |
| 10 * messageFormatPattern := string ( "{" messageFormatElement "}" string )* | 10 * messageFormatPattern := string ( "{" messageFormatElement "}" string )* |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 * produce "I see Mark and one other person in Athens." as output. | 31 * produce "I see Mark and one other person in Athens." as output. |
| 32 * | 32 * |
| 33 * See tests/message_format_test.dart for more examples. | 33 * See tests/message_format_test.dart for more examples. |
| 34 */ | 34 */ |
| 35 | 35 |
| 36 #library('MessageFormat'); | 36 #library('MessageFormat'); |
| 37 | 37 |
| 38 class MessageFormat { | 38 class MessageFormat { |
| 39 | 39 |
| 40 /** | 40 /** |
| 41 * Literal strings, including '', are replaced with \uFDDF_x_ for | |
| 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 | |
| 51 /** | |
| 52 * String that is used to determin the particular case and gender needed to be | 41 * String that is used to determin the particular case and gender needed to be |
| 53 * returned. The format of this string follows the same pattern as in Closure, | 42 * 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 | 43 * Java, and C++. This pattern is described at the beginning of this class |
| 55 * definition. See tests/message_format_test.dart for more examples. | 44 * definition. See tests/message_format_test.dart for more examples. |
| 56 */ | 45 */ |
| 57 final String _messageFunction; | 46 final String _messageFunction; |
| 58 | 47 |
| 59 /** | 48 /** |
| 60 * Constructor. Accepts a String [_messageDescription] describing the use case | 49 * String indicating a language code with which the message is to be |
| 61 * for this string in the program, and a the String [_messageFunction] that is | 50 * formatted (such as en-US). |
| 62 * used to determine the particular case and gender for the given instance. | |
| 63 */ | 51 */ |
| 64 const MessageFormat(this._messageDescription, this._messageFunction); | 52 final String _locale; |
| 53 |
| 54 /** |
| 55 * Constructor. The constructor expects you do provide annotations in comments |
| 56 * 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 |
| 58 * examples of inputs for each particular argument that the [_messageFunction] |
| 59 * accepts. The String [_messageFunction] is used to determine the particular |
| 60 * 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 |
| 62 * will attempt to infer it (acceptable if Dart is running on the client, we |
| 63 * can infer from the browser). |
| 64 */ |
| 65 //TODO(efortuna): _locale is not currently inferred. |
| 66 const MessageFormat(this._messageFunction, [this._locale = 'en-US']); |
| 65 | 67 |
| 66 /** | 68 /** |
| 67 * Formats a message. By default, we treat '#' with special meaning | 69 * Formats a message. By default, we treat '#' with special meaning |
| 68 * representing the number (plural_variable - plural_offset). If [ignorePound] | 70 * 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 | 71 * 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 | 72 * 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 | 73 * int values to influence the formatting of the message or data in the |
| 72 * message. For example, example, in the call to | 74 * message. For example, example, in the call to |
| 73 * msg_formatter.format({'NUM_PEOPLE': 5, 'NAME': 'Angela'}), | 75 * msg_formatter.format({'NUM_PEOPLE': 5, 'NAME': 'Angela'}), |
| 74 * object \{'NUM_PEOPLE': 5, 'NAME': 'Angela'\} holds positional parameters. | 76 * object \{'NUM_PEOPLE': 5, 'NAME': 'Angela'\} holds positional parameters. |
| 75 * 1st parameter could mean 5 people, which could influence plural format, | 77 * 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 | 78 * and 2nd parameter is just relevant data to be printed out in the proper |
| 77 * position in the message. | 79 * position in the message. |
| 78 * Returns the correctly formatted message in the desired language. | 80 * Returns the correctly formatted message in the desired language. |
| 79 */ | 81 */ |
| 80 String format(Map<String, dynamic> messageParameters, | 82 String format(Map<String, dynamic> messageParameters, |
| 81 [bool ignorePound=false]) { | 83 [bool ignorePound=false]) { |
| 82 // TODO(efortuna): actually perform the translation here. For now, I'm just | 84 // TODO(efortuna): actually perform the translation here. For now, I'm just |
| 83 // returning a description of the message to be returned. | 85 // returning a description of the message to be returned. |
| 84 return _messageDescription; | 86 return _messageDescription; |
| 85 } | 87 } |
| 86 } | 88 } |
| OLD | NEW |