| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 *The [source] parameter defines the prefix that is used to find | 51 *The [source] parameter defines the prefix that is used to find |
| 52 * libraries that contain localized messages. So with the default value of | 52 * libraries that contain localized messages. So with the default value of |
| 53 * 'messages_', we would look for messages for the locale 'pt_BR' in a library | 53 * 'messages_', we would look for messages for the locale 'pt_BR' in a library |
| 54 * named 'messages_pt_BR'. | 54 * named 'messages_pt_BR'. |
| 55 */ | 55 */ |
| 56 MessageLookupLocal(String localeName, this._sourcePrefix) { | 56 MessageLookupLocal(String localeName, this._sourcePrefix) { |
| 57 _libraries = currentMirrorSystem().libraries; | 57 _libraries = currentMirrorSystem().libraries; |
| 58 } | 58 } |
| 59 | 59 |
| 60 /** | 60 /** |
| 61 * Return true if the locale exists, or if it is null. The null case |
| 62 * is interpreted to mean that we use the default locale. |
| 63 */ |
| 64 bool localeExists(localeName) { |
| 65 if (localeName == null) return false; |
| 66 return _libraries['$_sourcePrefix$localeName'] != null; |
| 67 } |
| 68 |
| 69 /** |
| 61 * Return the localized version of a message. We are passed the original | 70 * Return the localized version of a message. We are passed the original |
| 62 * version of the message, which consists of a | 71 * version of the message, which consists of a |
| 63 * [message_str] that will be translated, and which may be interpolated | 72 * [message_str] that will be translated, and which may be interpolated |
| 64 * based on one or more variables, a [desc] providing a description of usage | 73 * based on one or more variables, a [desc] providing a description of usage |
| 65 * for the [message_str], and a map of [examples] for each data element to be | 74 * for the [message_str], and a map of [examples] for each data element to be |
| 66 * substituted into the message. | 75 * substituted into the message. |
| 67 * | 76 * |
| 68 * For example, if message="Hello, $name", then | 77 * For example, if message="Hello, $name", then |
| 69 * examples = {'name': 'Sparky'}. If not using the user's default locale, or | 78 * examples = {'name': 'Sparky'}. If not using the user's default locale, or |
| 70 * if the locale is not easily detectable, explicitly pass [locale]. | 79 * if the locale is not easily detectable, explicitly pass [locale]. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 82 String lookupMessage(String message_str, [final String desc='', | 91 String lookupMessage(String message_str, [final String desc='', |
| 83 final Map examples=const {}, String locale, | 92 final Map examples=const {}, String locale, |
| 84 String name, List<String> args]) { | 93 String name, List<String> args]) { |
| 85 if (name == null) return message_str; | 94 if (name == null) return message_str; |
| 86 // The translations also make use of Intl.message, so we need to not | 95 // The translations also make use of Intl.message, so we need to not |
| 87 // recurse and just stop when we find the first substitution. | 96 // recurse and just stop when we find the first substitution. |
| 88 if (_lookupInProgress) return message_str; | 97 if (_lookupInProgress) return message_str; |
| 89 _lookupInProgress = true; | 98 _lookupInProgress = true; |
| 90 var result; | 99 var result; |
| 91 try { | 100 try { |
| 92 // TODO(alanknight): Look up alternate forms, e.g. en for en_US | |
| 93 var actualLocale = (locale == null) ? Intl.getCurrentLocale() : locale; | 101 var actualLocale = (locale == null) ? Intl.getCurrentLocale() : locale; |
| 102 // For this usage, if the locale doesn't exist for messages, just return |
| 103 // it and we'll fall back to the original version. |
| 104 var verifiedLocale = |
| 105 Intl.verifiedLocale( |
| 106 actualLocale, |
| 107 localeExists, |
| 108 onFailure: (locale)=>locale); |
| 94 LibraryMirror messagesForThisLocale = | 109 LibraryMirror messagesForThisLocale = |
| 95 _libraries['$_sourcePrefix$actualLocale']; | 110 _libraries['$_sourcePrefix$verifiedLocale']; |
| 96 if (messagesForThisLocale == null) return message_str; | 111 if (messagesForThisLocale == null) return message_str; |
| 97 MethodMirror localized = messagesForThisLocale.functions[name]; | 112 MethodMirror localized = messagesForThisLocale.functions[name]; |
| 98 if (localized == null) return message_str; | 113 if (localized == null) return message_str; |
| 99 result = messagesForThisLocale.invoke(localized.simpleName, args); | 114 result = messagesForThisLocale.invoke(localized.simpleName, args); |
| 100 } | 115 } |
| 101 finally { | 116 finally { |
| 102 _lookupInProgress = false; | 117 _lookupInProgress = false; |
| 103 } | 118 } |
| 104 return result.value.reflectee; | 119 return result.value.reflectee; |
| 105 } | 120 } |
| 106 } | 121 } |
| OLD | NEW |