| OLD | NEW |
| 1 part of angular.directive; | 1 part of angular.directive; |
| 2 | 2 |
| 3 /** | 3 /** |
| 4 * ## Overview | 4 * ## Overview |
| 5 * `ngPluralize` is a directive that displays messages according to locale rules
. | 5 * `ngPluralize` is a directive that displays messages according to locale rules
. |
| 6 * | 6 * |
| 7 * You configure ngPluralize directive by specifying the mappings between plural | 7 * You configure ngPluralize directive by specifying the mappings between plural |
| 8 * categories and the strings to be displayed. | 8 * categories and the strings to be displayed. |
| 9 * | 9 * |
| 10 * ## Plural categories and explicit number rules | 10 * ## Plural categories and explicit number rules |
| 11 * The available plural categories are: | 11 * The available plural categories are: |
| 12 * * "zero", | 12 * * "zero", |
| 13 * * "one", | 13 * * "one", |
| 14 * * "two", | 14 * * "two", |
| 15 * * "few", | 15 * * "few", |
| 16 * * "many", | 16 * * "many", |
| 17 * * "other". | 17 * * "other". |
| 18 * | 18 * |
| 19 * While a plural category may match many numbers, an explicit number rule can o
nly match | 19 * While a plural category may match many numbers, an explicit number rule can |
| 20 * one number. For example, the explicit number rule for "3" matches the number
3. There | 20 * only match one number. For example, the explicit number rule for "3" matches |
| 21 * are examples of plural categories and explicit number rules throughout the re
st of this | 21 * the number 3. There are examples of plural categories and explicit number |
| 22 * documentation. | 22 * rules throughout the rest of this documentation. |
| 23 * | 23 * |
| 24 * ## Configuring ngPluralize | 24 * ## Configuring ngPluralize |
| 25 * You configure ngPluralize by providing 2 attributes: `count` and `when`. | 25 * You configure ngPluralize by providing 2 attributes: `count` and `when`. |
| 26 * You can also provide an optional attribute, `offset`. | 26 * You can also provide an optional attribute, `offset`. |
| 27 * | 27 * |
| 28 * The value of the `count` attribute can be either a string or an expression; t
hese are | 28 * The value of the `count` attribute can be either a string or an expression; |
| 29 * evaluated on the current scope for its bound value. | 29 * these are evaluated on the current scope for its bound value. |
| 30 * | 30 * |
| 31 * The `when` attribute specifies the mappings between plural categories and the
actual | 31 * The `when` attribute specifies the mappings between plural categories and the |
| 32 * string to be displayed. The value of the attribute should be a JSON object. | 32 * actual string to be displayed. The value of the attribute should be a JSON |
| 33 * object. |
| 33 * | 34 * |
| 34 * The following example shows how to configure ngPluralize: | 35 * The following example shows how to configure ngPluralize: |
| 35 * | 36 * |
| 36 * <ng-pluralize count="personCount" | 37 * <ng-pluralize count="personCount" |
| 37 * when="{'0': 'Nobody is viewing.', | 38 * when="{'0': 'Nobody is viewing.', |
| 38 * 'one': '1 person is viewing.', | 39 * 'one': '1 person is viewing.', |
| 39 * 'other': '{} people are viewing.'}"> | 40 * 'other': '{} people are viewing.'}"> |
| 40 * </ng-pluralize> | 41 * </ng-pluralize> |
| 41 * | 42 * |
| 42 * In the example, `"0: Nobody is viewing."` is an explicit number rule. If you
did not | 43 * In the example, `"0: Nobody is viewing."` is an explicit number rule. If you |
| 43 * specify this rule, 0 would be matched to the "other" category and "0 people a
re viewing" | 44 * did not specify this rule, 0 would be matched to the "other" category and |
| 44 * would be shown instead of "Nobody is viewing". You can specify an explicit nu
mber rule for | 45 * "0 people are viewing" would be shown instead of "Nobody is viewing". You can |
| 45 * other numbers, for example 12, so that instead of showing "12 people are view
ing", you can | 46 * specify an explicit number rule for other numbers, for example 12, so that |
| 46 * show "a dozen people are viewing". | 47 * instead of showing "12 people are viewing", you can show "a dozen people are |
| 48 * viewing". |
| 47 * | 49 * |
| 48 * You can use a set of closed braces (`{}`) as a placeholder for the number tha
t you want substituted | 50 * You can use a set of closed braces (`{}`) as a placeholder for the number |
| 49 * into pluralized strings. In the previous example, Angular will replace `{}` w
ith | 51 * that you want substituted into pluralized strings. In the previous example, |
| 50 * `{{personCount}}`. The closed braces `{}` is a placeholder {{numberExpression
}}. | 52 * Angular will replace `{}` with `{{personCount}}`. The closed braces `{}` is a |
| 53 * placeholder {{numberExpression}}. |
| 51 * | 54 * |
| 52 * ## Configuring ngPluralize with offset | 55 * ## Configuring ngPluralize with offset |
| 53 * The `offset` attribute allows further customization of pluralized text, which
can result in | 56 * The `offset` attribute allows further customization of pluralized text, which |
| 54 * a better user experience. For example, instead of the message "4 people are v
iewing this document", | 57 * can result in a better user experience. For example, instead of the message |
| 55 * you might display "John, Kate and 2 others are viewing this document". | 58 * "4 people are viewing this document", you might display "John, Kate and 2 |
| 56 * The offset attribute allows you to offset a number by any desired value. | 59 * others are viewing this document". The offset attribute allows you to offset |
| 60 * a number by any desired value. |
| 61 * |
| 57 * Let's take a look at an example: | 62 * Let's take a look at an example: |
| 58 * | 63 * |
| 59 * <ng-pluralize count="personCount" offset=2 | 64 * <ng-pluralize count="personCount" offset=2 |
| 60 * when="{'0': 'Nobody is viewing.', | 65 * when="{'0': 'Nobody is viewing.', |
| 61 * '1': '{{person1}} is viewing.', | 66 * '1': '${person1} is viewing.', |
| 62 * '2': '{{person1}} and {{person2}} are viewing.', | 67 * '2': '${person1} and ${person2} are viewing.', |
| 63 * 'one': '{{person1}}, {{person2}} and one other person
are viewing.', | 68 * 'one': '${person1}, ${person2} and one other person a
re viewing.', |
| 64 * 'other': '{{person1}}, {{person2}} and {} other peopl
e are viewing.'}"> | 69 * 'other': '${person1}, ${person2} and {} other people
are viewing.'}"> |
| 65 * </ng-pluralize> | 70 * </ng-pluralize> |
| 66 * | 71 * |
| 67 * Notice that we are still using two plural categories(one, other), but we adde
d | 72 * Notice that we are still using two plural categories(one, other), but we adde
d |
| 68 * three explicit number rules 0, 1 and 2. | 73 * three explicit number rules 0, 1 and 2. |
| 69 * When one person, perhaps John, views the document, "John is viewing" will be
shown. | 74 * When one person, perhaps John, views the document, "John is viewing" will be |
| 70 * When three people view the document, no explicit number rule is found, so | 75 * shown. When three people view the document, no explicit number rule is found, |
| 71 * an offset of 2 is taken off 3, and Angular uses 1 to decide the plural catego
ry. | 76 * so an offset of 2 is taken off 3, and Angular uses 1 to decide the plural |
| 72 * In this case, plural category 'one' is matched and "John, Marry and one other
person are viewing" | 77 * category. In this case, plural category 'one' is matched and "John, Marry and |
| 73 * is shown. | 78 * one other person are viewing" is shown. |
| 74 * | 79 * |
| 75 * Note that when you specify offsets, you must provide explicit number rules fo
r | 80 * Note that when you specify offsets, you must provide explicit number rules |
| 76 * numbers from 0 up to and including the offset. If you use an offset of 3, for
example, | 81 * for numbers from 0 up to and including the offset. If you use an offset of 3, |
| 77 * you must provide explicit number rules for 0, 1, 2 and 3. You must also provi
de plural strings for | 82 * for example, you must provide explicit number rules for 0, 1, 2 and 3. You |
| 78 * at least the "other" plural category. | 83 * must also provide plural strings for at least the "other" plural category. |
| 79 */ | 84 */ |
| 80 @NgDirective( | 85 @NgDirective( |
| 81 selector: 'ng-pluralize', | 86 selector: 'ng-pluralize', |
| 82 map: const { 'count': '=>count' }) | 87 map: const { 'count': '=>count' }) |
| 83 @NgDirective( | 88 @NgDirective( |
| 84 selector: '[ng-pluralize]', | 89 selector: '[ng-pluralize]', |
| 85 map: const { 'count': '=>count' }) | 90 map: const { 'count': '=>count' }) |
| 86 class NgPluralizeDirective { | 91 class NgPluralizeDirective { |
| 87 | |
| 88 final dom.Element element; | 92 final dom.Element element; |
| 89 final Scope scope; | 93 final Scope scope; |
| 90 final Interpolate interpolate; | 94 final Interpolate interpolate; |
| 95 final AstParser parser; |
| 91 int offset; | 96 int offset; |
| 92 Map<String, String> discreteRules = new Map(); | 97 var discreteRules = <String, String>{}; |
| 93 Map<Symbol, String> categoryRules = new Map(); | 98 var categoryRules = <Symbol, String>{}; |
| 99 |
| 94 static final RegExp IS_WHEN = new RegExp(r'^when-(minus-)?.'); | 100 static final RegExp IS_WHEN = new RegExp(r'^when-(minus-)?.'); |
| 101 static const Map<String, Symbol> SYMBOLS = const { |
| 102 'zero' : #zero, |
| 103 'one' : #one, |
| 104 'two' : #two, |
| 105 'few' : #few, |
| 106 'many' : #many, |
| 107 'other' : #other, |
| 108 }; |
| 95 | 109 |
| 96 NgPluralizeDirective(this.scope, this.element, this.interpolate, NodeAttrs att
ributes) { | 110 NgPluralizeDirective(this.scope, this.element, this.interpolate, |
| 97 Map<String, String> whens = attributes['when'] == null ? {} : scope.$eval(at
tributes['when']); | 111 NodeAttrs attributes, this.parser) { |
| 112 Map<String, String> whens = attributes['when'] == null |
| 113 ? {} |
| 114 : scope.eval(attributes['when']); |
| 98 offset = attributes['offset'] == null ? 0 : int.parse(attributes['offset']); | 115 offset = attributes['offset'] == null ? 0 : int.parse(attributes['offset']); |
| 99 | 116 |
| 100 element.attributes.keys.where((k) => IS_WHEN.hasMatch(k)).forEach((k) { | 117 element.attributes.keys.where((k) => IS_WHEN.hasMatch(k)).forEach((k) { |
| 101 var ruleName = k.replaceFirst('when-', '').replaceFirst('minus-', '-'); | 118 var ruleName = k.replaceFirst('when-', '').replaceFirst('minus-', '-'); |
| 102 whens[ruleName] = element.attributes[k]; | 119 whens[ruleName] = element.attributes[k]; |
| 103 }); | 120 }); |
| 104 | 121 |
| 105 if (whens['other'] == null) { | 122 if (whens['other'] == null) { |
| 106 throw "ngPluralize error! The 'other' plural category must always be speci
fied"; | 123 throw "ngPluralize error! The 'other' plural category must always be " |
| 124 "specified"; |
| 107 } | 125 } |
| 108 | 126 |
| 109 whens.forEach((k, v) { | 127 whens.forEach((k, v) { |
| 110 if (['zero', 'one', 'two', 'few', 'many', 'other'].contains(k)) { | 128 Symbol symbol = SYMBOLS[k]; |
| 111 this.categoryRules[new Symbol(k.toString())] = v; | 129 if (symbol != null) { |
| 130 this.categoryRules[symbol] = v; |
| 112 } else { | 131 } else { |
| 113 this.discreteRules[k.toString()] = v; | 132 this.discreteRules[k] = v; |
| 114 } | 133 } |
| 115 }); | 134 }); |
| 116 } | 135 } |
| 117 | 136 |
| 118 set count(value) { | 137 set count(value) { |
| 119 if (value is! num) { | 138 if (value is! num) { |
| 120 try { | 139 try { |
| 121 value = int.parse(value); | 140 value = int.parse(value); |
| 122 } catch(e) { | 141 } catch(e) { |
| 123 try { | 142 try { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 138 intValue -= offset; | 157 intValue -= offset; |
| 139 var exp = Function.apply(Intl.plural, [intValue], categoryRules); | 158 var exp = Function.apply(Intl.plural, [intValue], categoryRules); |
| 140 if (exp != null) { | 159 if (exp != null) { |
| 141 exp = exp.replaceAll(r'{}', (value - offset).toString()); | 160 exp = exp.replaceAll(r'{}', (value - offset).toString()); |
| 142 _setAndWatch(exp); | 161 _setAndWatch(exp); |
| 143 } | 162 } |
| 144 } | 163 } |
| 145 } | 164 } |
| 146 | 165 |
| 147 _setAndWatch(expression) { | 166 _setAndWatch(expression) { |
| 148 Interpolation interpolation = interpolate(expression); | 167 var interpolation = interpolate(expression, false, '\${', '}'); |
| 149 interpolation.setter = (text) => element.text = text; | 168 interpolation.setter = (text) => element.text = text; |
| 150 interpolation.setter(expression); | 169 interpolation.setter(expression); |
| 151 scope.$watchSet(interpolation.watchExpressions, interpolation.call); | 170 var items = interpolation.expressions.map((exp) => parser(exp)).toList(); |
| 171 AST ast = new PureFunctionAST(expression, new ArrayFn(), items); |
| 172 scope.watch(ast, interpolation.call); |
| 152 } | 173 } |
| 153 } | 174 } |
| OLD | NEW |