|
9 | 9 | */
|
10 | 10 |
|
11 | 11 | /*global XMLHttpRequest, setTimeout, document, navigator, ActiveXObject*/
|
12 |
| -/*jslint plusplus: true, continue: true*/ |
| 12 | +/*jslint plusplus: true, continue: true, evil: true*/ |
13 | 13 | (function () {
|
14 | 14 | "use strict";
|
15 | 15 |
|
|
115 | 115 | delete load_queues[locale];
|
116 | 116 | },
|
117 | 117 | use_default,
|
118 |
| - opDetect = /n(==|!=|<|>|<=|>=|%|&&|\|\|)([0-9]+)/i, |
119 |
| - opEqual = function (operand1, operand2) { |
120 |
| - return operand1 === operand2; |
121 |
| - }, |
122 |
| - opNotEqual = function (operand1, operand2) { |
123 |
| - return operand1 !== operand2; |
124 |
| - }, |
125 |
| - opLesser = function (operand1, operand2) { |
126 |
| - return operand1 < operand2; |
127 |
| - }, |
128 |
| - opGreater = function (operand1, operand2) { |
129 |
| - return operand1 > operand2; |
130 |
| - }, |
131 |
| - opLesserEqual = function (operand1, operand2) { |
132 |
| - return operand1 <= operand2; |
133 |
| - }, |
134 |
| - opGreaterEqual = function (operand1, operand2) { |
135 |
| - return operand1 >= operand2; |
136 |
| - }, |
137 |
| - opMod = function (operand1, operand2) { |
138 |
| - return (operand1 % operand2) === 0; |
139 |
| - }, |
140 |
| - opAnd = function (operand1, operand2) { |
141 |
| - return operand1 && operand2; |
142 |
| - }, |
143 |
| - opOr = function (operand1, operand2) { |
144 |
| - return operand1 || operand2; |
145 |
| - }, |
146 |
| - operate = function (operand1, operator, operand2) { |
147 |
| - switch (operator) { |
148 |
| - case '==': |
149 |
| - return opEqual(operand1, operand2); |
150 |
| - case '!=': |
151 |
| - return opNotEqual(operand1, operand2); |
152 |
| - case '<': |
153 |
| - return opLesser(operand1, operand2); |
154 |
| - case '>': |
155 |
| - return opGreater(operand1, operand2); |
156 |
| - case '<=': |
157 |
| - return opLesserEqual(operand1, operand2); |
158 |
| - case '>=': |
159 |
| - return opGreaterEqual(operand1, operand2); |
160 |
| - case '%': |
161 |
| - return opMod(operand1, operand2); |
162 |
| - case '&&': |
163 |
| - return opAnd(operand1, operand2); |
164 |
| - case '||': |
165 |
| - return opOr(operand1, operand2); |
166 |
| - } |
167 |
| - }, |
168 | 118 | /**
|
169 | 119 | * Evaluates the plural statements to get the correct index in the array.
|
170 | 120 | * The plurality evaluation statement has to be coordinated with
|
|
173 | 123 | * On the other hand, if plural=(n==1), first cell should be "cat"
|
174 | 124 | * and second cell should be "cats".
|
175 | 125 | */
|
176 |
| - parsePlural = function (pluralForms, cardinality) { |
177 |
| - var re = /^nplurals=[0-9];\s*plural=\(([n!=><%0-9]*)\)/i, |
178 |
| - plural, |
| 126 | + parsePlural = function (pluralForms, n) { |
| 127 | + //n is used in eval() |
| 128 | + var re = /^nplurals=[0-9];\s*plural=\(([n!=><(?:\s+\|\|\s+)(?:\s+&&\s+)%0-9]{3,})\)/i, |
| 129 | + evalResult, |
179 | 130 | result = re.exec(pluralForms);
|
180 |
| - //get the part of the evaluation and determine |
181 |
| - //the operation to execute |
182 |
| - result = opDetect.exec(result[1]); |
183 |
| - plural = operate(cardinality, result[1], parseInt(result[2], 10)); |
184 |
| - return plural ? 0 : 1; |
| 131 | + if (result && result[1]) { |
| 132 | + evalResult = eval(result[1]); |
| 133 | + if (evalResult === true) { |
| 134 | + return 0; |
| 135 | + } |
| 136 | + if (evalResult === false) { |
| 137 | + return 1; |
| 138 | + } |
| 139 | + } |
| 140 | + return 0; |
185 | 141 | },
|
186 | 142 | getPlural = function (localization, position, this_val) {
|
187 | 143 | if (localization['&plurals']) {
|
|
233 | 189 | plural = getPlural(localizations[locale], position, this_val);
|
234 | 190 | return plural.replace('__n__', cardinality);
|
235 | 191 | }
|
236 |
| - //TODO check for more forms |
237 | 192 | }
|
238 | 193 | if (localizations[locale][this_val]) {
|
239 | 194 | return localizations[locale][this_val];
|
|
0 commit comments