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 * Internationalization object providing access to message formatting objects, | 6 * Internationalization object providing access to message formatting objects, |
| 7 * date formatting, parsing, bidirectional text relative to a specific locale. | 7 * date formatting, parsing, bidirectional text relative to a specific locale. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 #library('Intl'); | 10 #library('Intl'); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 intlMsg = new IntlMessage(_locale); | 34 intlMsg = new IntlMessage(_locale); |
| 35 date = new DateFormat(_locale); | 35 date = new DateFormat(_locale); |
| 36 } | 36 } |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * Create a message that can be internationalized. It takes a | 39 * Create a message that can be internationalized. It takes a |
| 40 * [message_str] that will be translated, which may be interpolated | 40 * [message_str] that will be translated, which may be interpolated |
| 41 * based on one or more variables, a [desc] providing a description of usage | 41 * based on one or more variables, a [desc] providing a description of usage |
| 42 * for the [message_str], and a map of [examples] for each data element to be | 42 * for the [message_str], and a map of [examples] for each data element to be |
| 43 * substituted into the message. For example, if message="Hello, $name", then | 43 * substituted into the message. For example, if message="Hello, $name", then |
| 44 * examples = {'name': 'Sparky'}. The values of [desc] and [examples] are | 44 * examples = {'name': 'Sparky'}. If not using the user's default locale, or |
| 45 * not used at run-time but are only made available to the translators, so | 45 * if the locale is not easily detectable, explicitly pass [locale]. |
| 46 * they MUST be simple Strings available at compile time: no String | 46 * The values of [desc] and [examples] are not used at run-time but are only |
| 47 * interpolation or concatenation. | 47 * made available to the translators, so they MUST be simple Strings available |
| 48 * at compile time: no String interpolation or concatenation. | |
| 48 * The expected usage of this is inside a function that takes as parameters | 49 * The expected usage of this is inside a function that takes as parameters |
| 49 * the variables used in the interpolated string. | 50 * the variables used in the interpolated string. |
| 50 */ | 51 */ |
| 51 String message(String message_str, [final String desc='', | 52 static String message(String message_str, [final String desc='', |
| 52 final Map examples=const {}]) { | 53 final Map examples=const {}, |
| 54 String locale='']) { | |
| 55 if (locale == '') locale = _getDefaultLocale(); | |
|
Alan Knight
2012/06/11 23:07:01
This is bothering me a bit. If we're going to use
| |
| 53 return message_str; | 56 return message_str; |
| 54 } | 57 } |
| 55 | 58 |
| 56 /** | 59 /** |
| 57 * Support method for message formatting. Select the correct plural form from | 60 * Support method for message formatting. Select the correct plural form from |
| 58 * [cases] given [howMany]. | 61 * [cases] given [howMany]. |
| 59 */ | 62 */ |
| 60 static String plural(var howMany, Map cases, [num offset=0]) { | 63 static String plural(var howMany, Map cases, [num offset=0]) { |
| 61 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! | 64 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! |
| 62 // TODO(alanknight): Should we have instance methods instead/as well? | |
| 63 // Or have the others as statics? | |
| 64 return select(howMany.toString(), cases); | 65 return select(howMany.toString(), cases); |
| 65 } | 66 } |
| 66 | 67 |
| 67 /** | 68 /** |
| 68 * Support method for message formatting. Select the correct exact (gender, | 69 * Support method for message formatting. Select the correct exact (gender, |
| 69 * usually) form from [cases] given the user [choice]. | 70 * usually) form from [cases] given the user [choice]. |
| 70 */ | 71 */ |
| 71 static String select(String choice, Map cases) { | 72 static String select(String choice, Map cases) { |
| 72 if (cases.containsKey(choice)) { | 73 if (cases.containsKey(choice)) { |
| 73 return cases[choice]; | 74 return cases[choice]; |
| 74 } else if (cases.containsKey('other')){ | 75 } else if (cases.containsKey('other')){ |
| 75 return cases['other']; | 76 return cases['other']; |
| 76 } else { | 77 } else { |
| 77 return ''; | 78 return ''; |
| 78 } | 79 } |
| 79 } | 80 } |
| 81 | |
| 82 /** | |
| 83 * Helper to detect the locale as defined at runtime. | |
| 84 */ | |
| 85 static String _getDefaultLocale() { | |
| 86 // TODO(efortuna): Detect the default locale given the user preferences. | |
| 87 return 'en-US'; | |
| 88 } | |
| 80 } | 89 } |
| OLD | NEW |