| 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 * This library provides internationalization and localization. This includes | 6 * This library provides internationalization and localization. This includes |
| 7 * message formatting and replacement, date and number formatting and parsing, | 7 * message formatting and replacement, date and number formatting and parsing, |
| 8 * and utilities for working with Bidirectional text. | 8 * and utilities for working with Bidirectional text. |
| 9 * | 9 * |
| 10 * This is part of the [intl package] | 10 * This is part of the [intl package] |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 */ | 122 */ |
| 123 Intl([String aLocale]) { | 123 Intl([String aLocale]) { |
| 124 if (aLocale != null) { | 124 if (aLocale != null) { |
| 125 _locale = aLocale; | 125 _locale = aLocale; |
| 126 } else { | 126 } else { |
| 127 _locale = getCurrentLocale(); | 127 _locale = getCurrentLocale(); |
| 128 } | 128 } |
| 129 } | 129 } |
| 130 | 130 |
| 131 /** | 131 /** |
| 132 * Returns a message that can be internationalized. It takes a | 132 * Use this for a message that will be translated for different locales. The |
| 133 * [message_str] that will be translated, which may be interpolated | 133 * expected usage is that this is inside an enclosing function that only |
| 134 * based on one or more variables, a [desc] providing a description of usage | 134 * returns the value of this call and provides a scope for the variables that |
| 135 * for the [message_str], and a map of [examples] for each data element to be | 135 * will be substituted in the message. |
| 136 * substituted into the message. For example, if message="Hello, $name", then | 136 * |
| 137 * examples = {'name': 'Sparky'}. If not using the user's default locale, or | 137 * The parameters are a |
| 138 * if the locale is not easily detectable, explicitly pass [locale]. | 138 * [message_str] to be translated, which may be interpolated |
| 139 * The values of [desc] and [examples] are not used at run-time but are only | 139 * based on one or more variables, the [name] of the message, which should |
| 140 * made available to the translators, so they MUST be simple Strings available | 140 * match the enclosing function name, the [args] of the enclosing |
| 141 * at compile time: no String interpolation or concatenation. | 141 * function, a [desc] providing a description of usage |
| 142 * The expected usage of this is inside a function that takes as parameters | 142 * and a map of [examples] for each interpolated variable. For example |
| 143 * the variables used in the interpolated string, and additionally also a | 143 * hello(yourName) => Intl.message( |
| 144 * locale (optional). | 144 * "Hello, $yourName", |
| 145 * Ultimately, the information about the enclosing function and its arguments | 145 * name: "hello", |
| 146 * will be extracted automatically but for the time being it must be passed | 146 * args: [name], |
| 147 * explicitly in the [name] and [args] arguments. | 147 * desc: "Say hello", |
| 148 * examples = {"yourName": "Sparky"}. |
| 149 * The source code will be processed via the analyzer to extract out the |
| 150 * message data, so only a subset of valid Dart code is accepted. In |
| 151 * particular, everything must be literal and cannot refer to variables |
| 152 * outside the scope of the enclosing function. The [examples] map must |
| 153 * be a valid const literal map. Similarly, the [desc] argument must |
| 154 * be a single, simple string. These two arguments will not be used at runtime |
| 155 * but will be extracted from |
| 156 * the source code and used as additional data for translators. |
| 157 * |
| 158 * The [name] and [args] arguments are required, and are used at runtime |
| 159 * to look up the localized version and pass the appropriate arguments to it. |
| 160 * We may in the future modify the code during compilation to make manually |
| 161 * passing those arguments unnecessary. |
| 148 */ | 162 */ |
| 149 static String message(String message_str, {final String desc: '', | 163 static String message(String message_str, {final String desc: '', |
| 150 final Map examples: const {}, String locale, String name, | 164 final Map examples: const {}, String locale, String name, |
| 151 List<String> args}) { | 165 List<String> args}) { |
| 152 return messageLookup.lookupMessage( | 166 return messageLookup.lookupMessage( |
| 153 message_str, desc, examples, locale, name, args); | 167 message_str, desc, examples, locale, name, args); |
| 154 } | 168 } |
| 155 | 169 |
| 156 /** | 170 /** |
| 157 * Return the locale for this instance. If none was set, the locale will | 171 * Return the locale for this instance. If none was set, the locale will |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 * unless for some reason this gets called inside a message that resets the | 366 * unless for some reason this gets called inside a message that resets the |
| 353 * locale. | 367 * locale. |
| 354 */ | 368 */ |
| 355 static String getCurrentLocale() { | 369 static String getCurrentLocale() { |
| 356 if (defaultLocale == null) defaultLocale = systemLocale; | 370 if (defaultLocale == null) defaultLocale = systemLocale; |
| 357 return defaultLocale; | 371 return defaultLocale; |
| 358 } | 372 } |
| 359 | 373 |
| 360 toString() => "Intl($locale)"; | 374 toString() => "Intl($locale)"; |
| 361 } | 375 } |
| OLD | NEW |