| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Message/plural format library with locale support. This can have different | 6 * Message/plural format library with locale support. This can have different |
| 7 * implementations based on the mechanism for finding the localized versions | 7 * implementations based on the mechanism for finding the localized versions |
| 8 * of messages. This version expects them to be in a library named e.g. | 8 * of messages. This version expects them to be in a library named e.g. |
| 9 * 'messages_en_US'. The prefix is set in the "initializeMessages" call, which | 9 * 'messages_en_US'. The prefix is set in the "initializeMessages" call, which |
| 10 * must be made for a locale before any lookups can be done. | 10 * must be made for a locale before any lookups can be done. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 * The expected usage of this is inside a function that takes as parameters | 90 * The expected usage of this is inside a function that takes as parameters |
| 91 * the variables used in the interpolated string. | 91 * the variables used in the interpolated string. |
| 92 * | 92 * |
| 93 * Ultimately, the information about the enclosing function and its arguments | 93 * Ultimately, the information about the enclosing function and its arguments |
| 94 * will be extracted automatically but for the time being it must be passed | 94 * will be extracted automatically but for the time being it must be passed |
| 95 * explicitly in the [name] and [args] arguments. | 95 * explicitly in the [name] and [args] arguments. |
| 96 */ | 96 */ |
| 97 String lookupMessage(String message_str, [final String desc='', | 97 String lookupMessage(String message_str, [final String desc='', |
| 98 final Map examples=const {}, String locale, | 98 final Map examples=const {}, String locale, |
| 99 String name, List<String> args]) { | 99 String name, List<String> args]) { |
| 100 // If we don't have a name, return the original, and if we have | 100 if (name == null) return message_str; |
| 101 // been recursively invoked, also just return message_str. This | 101 var function = this[name]; |
| 102 // happens because the replacement functions also call Intl.message, | 102 return function == null ? message_str : Function.apply(function, args); |
| 103 // so we assume that when _lookupInProgress is true that we're | |
| 104 // already translated. | |
| 105 if (name == null || _lookupInProgress) return message_str; | |
| 106 _lookupInProgress = true; | |
| 107 // Try to apply the function holding the translated version. If there | |
| 108 // is an exception, use the original [message_str] as the result. | |
| 109 var result = message_str; | |
| 110 try { | |
| 111 var function = this[name]; | |
| 112 if (function != null) result = Function.apply(function, args); | |
| 113 } finally { | |
| 114 _lookupInProgress = false; | |
| 115 } | |
| 116 return result; | |
| 117 } | 103 } |
| 118 | 104 |
| 119 /** Return our message with the given name */ | 105 /** Return our message with the given name */ |
| 120 operator [](String messageName) => messages[messageName]; | 106 operator [](String messageName) => messages[messageName]; |
| 121 | 107 |
| 122 /** | 108 /** |
| 123 * Subclasses should override this to return a list of their message | 109 * Subclasses should override this to return a list of their message |
| 124 * functions. | 110 * functions. |
| 125 */ | 111 */ |
| 126 Map<String, Function> get messages; | 112 Map<String, Function> get messages; |
| 127 | 113 |
| 128 /** Subclasses should override this to return their locale, e.g. 'en_US' */ | 114 /** Subclasses should override this to return their locale, e.g. 'en_US' */ |
| 129 String get localeName; | 115 String get localeName; |
| 130 | 116 |
| 131 toString() => localeName; | 117 toString() => localeName; |
| 132 } | 118 } |
| OLD | NEW |