Skip to content

Commit 981617e

Browse files
committed
Added a few fixes to parsing of plurality operators.
Added the fix and tests for lesser than, greater than, and modulus.
1 parent f029ab0 commit 981617e

File tree

2 files changed

+249
-3
lines changed

2 files changed

+249
-3
lines changed

l10n.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
return operand1 >= operand2;
136136
},
137137
opMod = function (operand1, operand2) {
138-
return operand1 % operand2;
138+
return (operand1 % operand2) === 0;
139139
},
140140
opAnd = function (operand1, operand2) {
141141
return operand1 && operand2;
@@ -174,7 +174,7 @@
174174
* and second cell should be "cats".
175175
*/
176176
parsePlural = function (pluralForms, cardinality) {
177-
var re = /^nplurals=[0-9];\s*plural=\(([n!=0-9]*)\)/i,
177+
var re = /^nplurals=[0-9];\s*plural=\(([n!=><%0-9]*)\)/i,
178178
plural,
179179
result = re.exec(pluralForms);
180180
//get the part of the evaluation and determine
@@ -222,7 +222,8 @@
222222
if (localizations[locale]) {
223223
pluralForms = localizations[locale]['&plural-forms'];
224224
if (pluralForms) {
225-
if (!cardinality || pluralForms.indexOf('nplurals=1') !== -1) {
225+
if (cardinality === null || cardinality === undefined ||
226+
pluralForms.indexOf('nplurals=1') !== -1) {
226227
//i.e. nplurals=1, use [0]
227228
plural = getPlural(localizations[locale], 0, this_val);
228229
return plural.replace('__n__', cardinality);

tests/plural-operator.html

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>One Locale Test</title>
6+
<link rel="stylesheet" href="qunit.css">
7+
</head>
8+
<body>
9+
<div id="qunit"></div>
10+
<div id="qunit-fixture"></div>
11+
<script src="../l10n.js"></script>
12+
<script src="qunit.js"></script>
13+
</body>
14+
<script>
15+
var phrase1 = "%phrase1", //in non-plural form only
16+
e1singular = 'There is a bear in the zoo.',
17+
e1plural = 'There are many bears in the zoo.',
18+
e1few = 'There are a few bears in the zoo.',
19+
e1many = 'There are many, many bears in the zoo.',
20+
e1dozen = 'There are dozens of bears in the zoo.',
21+
e1quite = 'There are quite a number of bears in the zoo.';
22+
23+
test('Inequality test', function () {
24+
//initiate 1 locale
25+
String.toLocaleString({
26+
'en': {
27+
'&plural-forms': 'nplurals=2; plural=(n!=1)',
28+
'&plurals': [
29+
{
30+
'%phrase1': 'There are many bears in the zoo.'
31+
},
32+
{
33+
'%phrase1': 'There is a bear in the zoo.'
34+
}
35+
]
36+
}
37+
});
38+
String.defaultLocale = 'en';
39+
40+
equal(phrase1.toLocaleString(), e1plural,
41+
'NULL: Translated as "' + e1plural + '" because plural form is default.');
42+
equal(phrase1.toLocaleString(0), e1plural,
43+
'0: Translated as "' + e1plural + '".');
44+
equal(phrase1.toLocaleString(1), e1singular,
45+
'1: Translated as "' + e1singular + '".');
46+
equal(phrase1.toLocaleString(2), e1plural,
47+
'2: Translated as "' + e1plural + '".');
48+
});
49+
50+
test('Equality test - singular form is default', function () {
51+
//initiate 1 locale
52+
String.toLocaleString({
53+
'en': {
54+
'&plural-forms': 'nplurals=2; plural=(n==1)',
55+
'&plurals': [
56+
{
57+
'%phrase1': 'There is a bear in the zoo.'
58+
},
59+
{
60+
'%phrase1': 'There are many bears in the zoo.'
61+
}
62+
]
63+
}
64+
});
65+
String.defaultLocale = 'en';
66+
67+
equal(phrase1.toLocaleString(), e1singular,
68+
'NULL: Translated as "' + e1singular + '" because singular form is default.');
69+
equal(phrase1.toLocaleString(0), e1plural,
70+
'0: Translated as "' + e1plural + '".');
71+
equal(phrase1.toLocaleString(1), e1singular,
72+
'1: Translated as "' + e1singular + '".');
73+
equal(phrase1.toLocaleString(2), e1plural,
74+
'2: Translated as "' + e1plural + '".');
75+
});
76+
77+
test('Lesser than test', function () {
78+
//initiate 1 locale
79+
String.toLocaleString({
80+
'en': {
81+
'&plural-forms': 'nplurals=2; plural=(n<5)',
82+
'&plurals': [
83+
{
84+
'%phrase1': 'There are a few bears in the zoo.'
85+
},
86+
{
87+
'%phrase1': 'There are many, many bears in the zoo.'
88+
}
89+
]
90+
}
91+
});
92+
String.defaultLocale = 'en';
93+
94+
equal(phrase1.toLocaleString(), e1few,
95+
'NULL: Translated as "' + e1few + '" because of default.');
96+
equal(phrase1.toLocaleString(0), e1few,
97+
'0: Translated as "' + e1few + '".');
98+
equal(phrase1.toLocaleString(1), e1few,
99+
'1: Translated as "' + e1few + '".');
100+
equal(phrase1.toLocaleString(2), e1few,
101+
'2: Translated as "' + e1few + '".');
102+
equal(phrase1.toLocaleString(4), e1few,
103+
'4: Translated as "' + e1few + '".');
104+
equal(phrase1.toLocaleString(5), e1many,
105+
'5: Translated as "' + e1many + '".');
106+
equal(phrase1.toLocaleString(6), e1many,
107+
'6: Translated as "' + e1many + '".');
108+
});
109+
110+
test('Lesser than, equal to test', function () {
111+
//initiate 1 locale
112+
String.toLocaleString({
113+
'en': {
114+
'&plural-forms': 'nplurals=2; plural=(n<=5)',
115+
'&plurals': [
116+
{
117+
'%phrase1': 'There are a few bears in the zoo.'
118+
},
119+
{
120+
'%phrase1': 'There are many, many bears in the zoo.'
121+
}
122+
]
123+
}
124+
});
125+
String.defaultLocale = 'en';
126+
127+
equal(phrase1.toLocaleString(), e1few,
128+
'NULL: Translated as "' + e1few + '" because of default.');
129+
equal(phrase1.toLocaleString(0), e1few,
130+
'0: Translated as "' + e1few + '".');
131+
equal(phrase1.toLocaleString(1), e1few,
132+
'1: Translated as "' + e1few + '".');
133+
equal(phrase1.toLocaleString(2), e1few,
134+
'2: Translated as "' + e1few + '".');
135+
equal(phrase1.toLocaleString(4), e1few,
136+
'4: Translated as "' + e1few + '".');
137+
equal(phrase1.toLocaleString(5), e1few,
138+
'5: Translated as "' + e1few + '".');
139+
equal(phrase1.toLocaleString(6), e1many,
140+
'6: Translated as "' + e1many + '".');
141+
});
142+
143+
test('More than test', function () {
144+
//initiate 1 locale
145+
String.toLocaleString({
146+
'en': {
147+
'&plural-forms': 'nplurals=2; plural=(n>5)',
148+
'&plurals': [
149+
{
150+
'%phrase1': 'There are many, many bears in the zoo.'
151+
},
152+
{
153+
'%phrase1': 'There are a few bears in the zoo.'
154+
}
155+
]
156+
}
157+
});
158+
String.defaultLocale = 'en';
159+
160+
equal(phrase1.toLocaleString(), e1many,
161+
'NULL: Translated as "' + e1many + '" because of default.');
162+
equal(phrase1.toLocaleString(0), e1few,
163+
'0: Translated as "' + e1few + '".');
164+
equal(phrase1.toLocaleString(1), e1few,
165+
'1: Translated as "' + e1few + '".');
166+
equal(phrase1.toLocaleString(2), e1few,
167+
'2: Translated as "' + e1few + '".');
168+
equal(phrase1.toLocaleString(4), e1few,
169+
'4: Translated as "' + e1few + '".');
170+
equal(phrase1.toLocaleString(5), e1few,
171+
'5: Translated as "' + e1few + '".');
172+
equal(phrase1.toLocaleString(6), e1many,
173+
'6: Translated as "' + e1many + '".');
174+
});
175+
176+
test('Lesser than, equal to test', function () {
177+
//initiate 1 locale
178+
String.toLocaleString({
179+
'en': {
180+
'&plural-forms': 'nplurals=2; plural=(n>=5)',
181+
'&plurals': [
182+
{
183+
'%phrase1': 'There are many, many bears in the zoo.'
184+
},
185+
{
186+
'%phrase1': 'There are a few bears in the zoo.'
187+
}
188+
]
189+
}
190+
});
191+
String.defaultLocale = 'en';
192+
193+
equal(phrase1.toLocaleString(), e1many,
194+
'NULL: Translated as "' + e1many + '" because of default.');
195+
equal(phrase1.toLocaleString(0), e1few,
196+
'0: Translated as "' + e1few + '".');
197+
equal(phrase1.toLocaleString(1), e1few,
198+
'1: Translated as "' + e1few + '".');
199+
equal(phrase1.toLocaleString(2), e1few,
200+
'2: Translated as "' + e1few + '".');
201+
equal(phrase1.toLocaleString(4), e1few,
202+
'4: Translated as "' + e1few + '".');
203+
equal(phrase1.toLocaleString(5), e1many,
204+
'5: Translated as "' + e1many + '".');
205+
equal(phrase1.toLocaleString(6), e1many,
206+
'6: Translated as "' + e1many + '".');
207+
});
208+
209+
test('Mod test', function () {
210+
//initiate 1 locale
211+
String.toLocaleString({
212+
'en': {
213+
'&plural-forms': 'nplurals=2; plural=(n%12)',
214+
'&plurals': [
215+
{
216+
'%phrase1': 'There are dozens of bears in the zoo.'
217+
},
218+
{
219+
'%phrase1': 'There are quite a number of bears in the zoo.'
220+
}
221+
]
222+
}
223+
});
224+
String.defaultLocale = 'en';
225+
226+
equal(phrase1.toLocaleString(), e1dozen,
227+
'NULL: Translated as "' + e1dozen + '" because of default.');
228+
equal(phrase1.toLocaleString(0), e1dozen,
229+
'0: Translated as "' + e1dozen + '".');
230+
equal(phrase1.toLocaleString(1), e1quite,
231+
'1: Translated as "' + e1quite + '".');
232+
equal(phrase1.toLocaleString(2), e1quite,
233+
'2: Translated as "' + e1quite + '".');
234+
equal(phrase1.toLocaleString(11), e1quite,
235+
'11: Translated as "' + e1quite + '".');
236+
equal(phrase1.toLocaleString(12), e1dozen,
237+
'12: Translated as "' + e1dozen + '".');
238+
equal(phrase1.toLocaleString(13), e1quite,
239+
'13: Translated as "' + e1quite + '".');
240+
equal(phrase1.toLocaleString(24), e1dozen,
241+
'24: Translated as "' + e1dozen + '".');
242+
});
243+
</script>
244+
</html>
245+

0 commit comments

Comments
 (0)