|
18 | 18 | * For more information, see:
|
19 | 19 | * https://docs.google.com/a/google.com/document/d/1pbtW2yvtmFBikfRrJd8VAsabiFkKezmYZ_PbgdjQOVU/edit
|
20 | 20 | *
|
21 |
| - * ## Example |
| 21 | + * @example |
| 22 | + * ## Gender |
22 | 23 | *
|
23 |
| - * <example name="ngMessageFormat-example" module="msgFmtExample" deps="angular-message-format.min.js"> |
| 24 | + * This example uses the "select" keyword to specify the message based on gender. |
| 25 | + * |
| 26 | + * <example name="ngMessageFormat-example-gender" module="msgFmtExample" deps="angular-message-format.min.js"> |
| 27 | + * <file name="index.html"> |
| 28 | + * <div ng-controller="AppController"> |
| 29 | + * Select Recipient:<br> |
| 30 | + <select ng-model="recipient" ng-options="person as person.name for person in recipients"> |
| 31 | + </select> |
| 32 | + <p>{{recipient.gender, select, |
| 33 | + male {{{recipient.name}} unwrapped his gift. } |
| 34 | + female {{{recipient.name}} unwrapped her gift. } |
| 35 | + other {{{recipient.name}} unwrapped their gift. } |
| 36 | + }}</p> |
| 37 | + * </div> |
| 38 | + * </file> |
| 39 | + * <file name="script.js"> |
| 40 | + * function Person(name, gender) { |
| 41 | + * this.name = name; |
| 42 | + * this.gender = gender; |
| 43 | + * } |
| 44 | + * |
| 45 | + * var alice = new Person("Alice", "female"), |
| 46 | + * bob = new Person("Bob", "male"), |
| 47 | + * ashley = new Person("Ashley", ""); |
| 48 | + * |
| 49 | + * angular.module('msgFmtExample', ['ngMessageFormat']) |
| 50 | + * .controller('AppController', ['$scope', function($scope) { |
| 51 | + * $scope.recipients = [alice, bob, ashley]; |
| 52 | + * $scope.recipient = $scope.recipients[0]; |
| 53 | + * }]); |
| 54 | + * </file> |
| 55 | + * </example> |
| 56 | + * |
| 57 | + * @example |
| 58 | + * ## Plural |
| 59 | + * |
| 60 | + * This example shows how the "plural" keyword is used to account for a variable number of entities. |
| 61 | + * The "#" variable holds the current number and can be embedded in the message. |
| 62 | + * |
| 63 | + * Note that "=1" takes precedence over "one". |
| 64 | + * |
| 65 | + * The example also shows the "offset" keyword, which allows you to offset the value of the "#" variable. |
| 66 | + * |
| 67 | + * <example name="ngMessageFormat-example-plural" module="msgFmtExample" deps="angular-message-format.min.js"> |
24 | 68 | * <file name="index.html">
|
25 | 69 | * <div ng-controller="AppController">
|
26 |
| - * <button ng-click="decreaseRecipients()" id="decreaseRecipients">decreaseRecipients</button><br> |
27 |
| - * <span>{{recipients.length, plural, offset:1 |
| 70 | + Select recipients:<br> |
| 71 | + <select multiple size=5 ng-model="recipients" ng-options="person as person.name for person in people"> |
| 72 | + </select><br> |
| 73 | + * <p>{{recipients.length, plural, offset:1 |
28 | 74 | * =0 {{{sender.name}} gave no gifts (\#=#)}
|
29 |
| - * =1 {{{sender.name}} gave one gift to {{recipients[0].name}} (\#=#)} |
| 75 | + * =1 {{{sender.name}} gave a gift to {{recipients[0].name}} (\#=#)} |
30 | 76 | * one {{{sender.name}} gave {{recipients[0].name}} and one other person a gift (\#=#)}
|
31 | 77 | * other {{{sender.name}} gave {{recipients[0].name}} and # other people a gift (\#=#)}
|
32 |
| - * }}</span> |
| 78 | + * }}</p> |
33 | 79 | * </div>
|
34 | 80 | * </file>
|
35 | 81 | *
|
|
41 | 87 | *
|
42 | 88 | * var alice = new Person("Alice", "female"),
|
43 | 89 | * bob = new Person("Bob", "male"),
|
44 |
| - * charlie = new Person("Charlie", "male"), |
45 |
| - * harry = new Person("Harry Potter", "male"); |
| 90 | + * sarah = new Person("Sarah", "female"), |
| 91 | + * harry = new Person("Harry Potter", "male"), |
| 92 | + * ashley = new Person("Ashley", ""); |
46 | 93 | *
|
47 | 94 | * angular.module('msgFmtExample', ['ngMessageFormat'])
|
48 | 95 | * .controller('AppController', ['$scope', function($scope) {
|
49 |
| - * $scope.recipients = [alice, bob, charlie]; |
| 96 | + * $scope.people = [alice, bob, sarah, ashley]; |
| 97 | + * $scope.recipients = [alice, bob, sarah]; |
50 | 98 | * $scope.sender = harry;
|
51 |
| - * $scope.decreaseRecipients = function() { |
52 |
| - * --$scope.recipients.length; |
53 |
| - * }; |
54 | 99 | * }]);
|
55 | 100 | * </file>
|
56 | 101 | *
|
57 | 102 | * <file name="protractor.js" type="protractor">
|
58 | 103 | * describe('MessageFormat plural', function() {
|
| 104 | + * function clickOptionCtrl(select, index) { |
| 105 | + * element.all(by.css('select option')).then(function(arr) { |
| 106 | + browser.actions() |
| 107 | + .mouseMove(arr[index]) |
| 108 | + .keyDown(protractor.Key.CONTROL) |
| 109 | + .click() |
| 110 | + .keyUp(protractor.Key.CONTROL) |
| 111 | + .perform(); |
| 112 | + }); |
| 113 | + * } |
| 114 | + * |
59 | 115 | * it('should pluralize initial values', function() {
|
60 |
| - * var messageElem = element(by.binding('recipients.length')), decreaseRecipientsBtn = element(by.id('decreaseRecipients')); |
| 116 | + * var messageElem = element(by.binding('recipients.length')), |
| 117 | + * select = element(by.css('select')); |
| 118 | + * |
61 | 119 | * expect(messageElem.getText()).toEqual('Harry Potter gave Alice and 2 other people a gift (#=2)');
|
62 |
| - * decreaseRecipientsBtn.click(); |
| 120 | + clickOptionCtrl(select, 2); |
63 | 121 | * expect(messageElem.getText()).toEqual('Harry Potter gave Alice and one other person a gift (#=1)');
|
64 |
| - * decreaseRecipientsBtn.click(); |
65 |
| - * expect(messageElem.getText()).toEqual('Harry Potter gave one gift to Alice (#=0)'); |
66 |
| - * decreaseRecipientsBtn.click(); |
| 122 | + clickOptionCtrl(select, 1); |
| 123 | + * expect(messageElem.getText()).toEqual('Harry Potter gave a gift to Alice (#=0)'); |
| 124 | + clickOptionCtrl(select, 0); |
67 | 125 | * expect(messageElem.getText()).toEqual('Harry Potter gave no gifts (#=-1)');
|
68 | 126 | * });
|
69 | 127 | * });
|
70 | 128 | * </file>
|
71 | 129 | * </example>
|
| 130 | + * |
| 131 | + * @example |
| 132 | + * ## Plural and Gender |
| 133 | + * |
| 134 | + * This example shows how you can specify gender rules for specific plural matches - in this case, |
| 135 | + * =1 is special cased for gender. |
| 136 | + * <example name="ngMessageFormat-example-plural-gender" module="msgFmtExample" deps="angular-message-format.min.js"> |
| 137 | + * <file name="index.html"> |
| 138 | + * <div ng-controller="AppController"> |
| 139 | + Select recipients:<br> |
| 140 | + <select multiple size=5 ng-model="recipients" ng-options="person as person.name for person in people"> |
| 141 | + </select><br> |
| 142 | + <p>{{recipients.length, plural, |
| 143 | + =0 {{{sender.name}} has not given any gifts to anyone.} |
| 144 | + =1 { {{recipients[0].gender, select, |
| 145 | + female { {{sender.name}} gave {{recipients[0].name}} her gift.} |
| 146 | + male { {{sender.name}} gave {{recipients[0].name}} his gift.} |
| 147 | + other { {{sender.name}} gave {{recipients[0].name}} their gift.} |
| 148 | + }} |
| 149 | + } |
| 150 | + other {{{sender.name}} gave {{recipients.length}} people gifts.} |
| 151 | + }}</p> |
| 152 | + </file> |
| 153 | + * <file name="script.js"> |
| 154 | + * function Person(name, gender) { |
| 155 | + * this.name = name; |
| 156 | + * this.gender = gender; |
| 157 | + * } |
| 158 | + * |
| 159 | + * var alice = new Person("Alice", "female"), |
| 160 | + * bob = new Person("Bob", "male"), |
| 161 | + * harry = new Person("Harry Potter", "male"), |
| 162 | + * ashley = new Person("Ashley", ""); |
| 163 | + * |
| 164 | + * angular.module('msgFmtExample', ['ngMessageFormat']) |
| 165 | + * .controller('AppController', ['$scope', function($scope) { |
| 166 | + * $scope.people = [alice, bob, ashley]; |
| 167 | + * $scope.recipients = [alice]; |
| 168 | + * $scope.sender = harry; |
| 169 | + * }]); |
| 170 | + * </file> |
| 171 | + </example> |
72 | 172 | */
|
73 | 173 | var $$MessageFormatFactory = ['$parse', '$locale', '$sce', '$exceptionHandler', function $$messageFormat(
|
74 | 174 | $parse, $locale, $sce, $exceptionHandler) {
|
|
0 commit comments