Skip to content

Commit 7e5e157

Browse files
Make trans + %count% parameter resolve plurals
1 parent 0523647 commit 7e5e157

File tree

3 files changed

+517
-1
lines changed

3 files changed

+517
-1
lines changed

Tests/Translation/TranslatorTest.php

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ public function testTrans($expected, $id, $parameters)
4747
$this->assertEquals($expected, $translator->trans($id, $parameters));
4848
}
4949

50+
/**
51+
* @dataProvider getTransChoiceTests
52+
*/
53+
public function testTransChoiceWithExplicitLocale($expected, $id, $number)
54+
{
55+
$translator = $this->getTranslator();
56+
$translator->setLocale('en');
57+
58+
$this->assertEquals($expected, $translator->trans($id, array('%count%' => $number)));
59+
}
60+
61+
/**
62+
* @dataProvider getTransChoiceTests
63+
*/
64+
public function testTransChoiceWithDefaultLocale($expected, $id, $number)
65+
{
66+
\Locale::setDefault('en');
67+
68+
$translator = $this->getTranslator();
69+
70+
$this->assertEquals($expected, $translator->trans($id, array('%count%' => $number)));
71+
}
72+
5073
public function testGetSetLocale()
5174
{
5275
$translator = $this->getTranslator();
@@ -76,4 +99,255 @@ public function getTransTests()
7699
array('Symfony is awesome!', 'Symfony is %what%!', array('%what%' => 'awesome')),
77100
);
78101
}
102+
103+
public function getTransChoiceTests()
104+
{
105+
return array(
106+
array('There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
107+
array('There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1),
108+
array('There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10),
109+
array('There are 0 apples', 'There is 1 apple|There are %count% apples', 0),
110+
array('There is 1 apple', 'There is 1 apple|There are %count% apples', 1),
111+
array('There are 10 apples', 'There is 1 apple|There are %count% apples', 10),
112+
// custom validation messages may be coded with a fixed value
113+
array('There are 2 apples', 'There are 2 apples', 2),
114+
);
115+
}
116+
117+
/**
118+
* @dataProvider getInternal
119+
*/
120+
public function testInterval($expected, $number, $interval)
121+
{
122+
$translator = $this->getTranslator();
123+
124+
$this->assertEquals($expected, $translator->trans($interval.' foo|[1,Inf[ bar', array('%count%' => $number)));
125+
}
126+
127+
public function getInternal()
128+
{
129+
return array(
130+
array('foo', 3, '{1,2, 3 ,4}'),
131+
array('bar', 10, '{1,2, 3 ,4}'),
132+
array('bar', 3, '[1,2]'),
133+
array('foo', 1, '[1,2]'),
134+
array('foo', 2, '[1,2]'),
135+
array('bar', 1, ']1,2['),
136+
array('bar', 2, ']1,2['),
137+
array('foo', log(0), '[-Inf,2['),
138+
array('foo', -log(0), '[-2,+Inf]'),
139+
);
140+
}
141+
142+
/**
143+
* @dataProvider getChooseTests
144+
*/
145+
public function testChoose($expected, $id, $number)
146+
{
147+
$translator = $this->getTranslator();
148+
149+
$this->assertEquals($expected, $translator->trans($id, array('%count%' => $number)));
150+
}
151+
152+
public function testReturnMessageIfExactlyOneStandardRuleIsGiven()
153+
{
154+
$translator = $this->getTranslator();
155+
156+
$this->assertEquals('There are two apples', $translator->trans('There are two apples', array('%count%' => 2)));
157+
}
158+
159+
/**
160+
* @dataProvider getNonMatchingMessages
161+
* @expectedException \InvalidArgumentException
162+
*/
163+
public function testThrowExceptionIfMatchingMessageCannotBeFound($id, $number)
164+
{
165+
$translator = $this->getTranslator();
166+
167+
$translator->trans($id, array('%count%' => $number));
168+
}
169+
170+
public function getNonMatchingMessages()
171+
{
172+
return array(
173+
array('{0} There are no apples|{1} There is one apple', 2),
174+
array('{1} There is one apple|]1,Inf] There are %count% apples', 0),
175+
array('{1} There is one apple|]2,Inf] There are %count% apples', 2),
176+
array('{0} There are no apples|There is one apple', 2),
177+
);
178+
}
179+
180+
public function getChooseTests()
181+
{
182+
return array(
183+
array('There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
184+
array('There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
185+
array('There are no apples', '{0}There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
186+
187+
array('There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1),
188+
189+
array('There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10),
190+
array('There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf]There are %count% apples', 10),
191+
array('There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10),
192+
193+
array('There are 0 apples', 'There is one apple|There are %count% apples', 0),
194+
array('There is one apple', 'There is one apple|There are %count% apples', 1),
195+
array('There are 10 apples', 'There is one apple|There are %count% apples', 10),
196+
197+
array('There are 0 apples', 'one: There is one apple|more: There are %count% apples', 0),
198+
array('There is one apple', 'one: There is one apple|more: There are %count% apples', 1),
199+
array('There are 10 apples', 'one: There is one apple|more: There are %count% apples', 10),
200+
201+
array('There are no apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 0),
202+
array('There is one apple', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 1),
203+
array('There are 10 apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 10),
204+
205+
array('', '{0}|{1} There is one apple|]1,Inf] There are %count% apples', 0),
206+
array('', '{0} There are no apples|{1}|]1,Inf] There are %count% apples', 1),
207+
208+
// Indexed only tests which are Gettext PoFile* compatible strings.
209+
array('There are 0 apples', 'There is one apple|There are %count% apples', 0),
210+
array('There is one apple', 'There is one apple|There are %count% apples', 1),
211+
array('There are 2 apples', 'There is one apple|There are %count% apples', 2),
212+
213+
// Tests for float numbers
214+
array('There is almost one apple', '{0} There are no apples|]0,1[ There is almost one apple|{1} There is one apple|[1,Inf] There is more than one apple', 0.7),
215+
array('There is one apple', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 1),
216+
array('There is more than one apple', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 1.7),
217+
array('There are no apples', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0),
218+
array('There are no apples', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0.0),
219+
array('There are no apples', '{0.0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0),
220+
221+
// Test texts with new-lines
222+
// with double-quotes and \n in id & double-quotes and actual newlines in text
223+
array("This is a text with a\n new-line in it. Selector = 0.", '{0}This is a text with a
224+
new-line in it. Selector = 0.|{1}This is a text with a
225+
new-line in it. Selector = 1.|[1,Inf]This is a text with a
226+
new-line in it. Selector > 1.', 0),
227+
// with double-quotes and \n in id and single-quotes and actual newlines in text
228+
array("This is a text with a\n new-line in it. Selector = 1.", '{0}This is a text with a
229+
new-line in it. Selector = 0.|{1}This is a text with a
230+
new-line in it. Selector = 1.|[1,Inf]This is a text with a
231+
new-line in it. Selector > 1.', 1),
232+
array("This is a text with a\n new-line in it. Selector > 1.", '{0}This is a text with a
233+
new-line in it. Selector = 0.|{1}This is a text with a
234+
new-line in it. Selector = 1.|[1,Inf]This is a text with a
235+
new-line in it. Selector > 1.', 5),
236+
// with double-quotes and id split accros lines
237+
array('This is a text with a
238+
new-line in it. Selector = 1.', '{0}This is a text with a
239+
new-line in it. Selector = 0.|{1}This is a text with a
240+
new-line in it. Selector = 1.|[1,Inf]This is a text with a
241+
new-line in it. Selector > 1.', 1),
242+
// with single-quotes and id split accros lines
243+
array('This is a text with a
244+
new-line in it. Selector > 1.', '{0}This is a text with a
245+
new-line in it. Selector = 0.|{1}This is a text with a
246+
new-line in it. Selector = 1.|[1,Inf]This is a text with a
247+
new-line in it. Selector > 1.', 5),
248+
// with single-quotes and \n in text
249+
array('This is a text with a\nnew-line in it. Selector = 0.', '{0}This is a text with a\nnew-line in it. Selector = 0.|{1}This is a text with a\nnew-line in it. Selector = 1.|[1,Inf]This is a text with a\nnew-line in it. Selector > 1.', 0),
250+
// with double-quotes and id split accros lines
251+
array("This is a text with a\nnew-line in it. Selector = 1.", "{0}This is a text with a\nnew-line in it. Selector = 0.|{1}This is a text with a\nnew-line in it. Selector = 1.|[1,Inf]This is a text with a\nnew-line in it. Selector > 1.", 1),
252+
// esacape pipe
253+
array('This is a text with | in it. Selector = 0.', '{0}This is a text with || in it. Selector = 0.|{1}This is a text with || in it. Selector = 1.', 0),
254+
// Empty plural set (2 plural forms) from a .PO file
255+
array('', '|', 1),
256+
// Empty plural set (3 plural forms) from a .PO file
257+
array('', '||', 1),
258+
);
259+
}
260+
261+
/**
262+
* @dataProvider failingLangcodes
263+
*/
264+
public function testFailedLangcodes($nplural, $langCodes)
265+
{
266+
$matrix = $this->generateTestData($langCodes);
267+
$this->validateMatrix($nplural, $matrix, false);
268+
}
269+
270+
/**
271+
* @dataProvider successLangcodes
272+
*/
273+
public function testLangcodes($nplural, $langCodes)
274+
{
275+
$matrix = $this->generateTestData($langCodes);
276+
$this->validateMatrix($nplural, $matrix);
277+
}
278+
279+
/**
280+
* This array should contain all currently known langcodes.
281+
*
282+
* As it is impossible to have this ever complete we should try as hard as possible to have it almost complete.
283+
*
284+
* @return array
285+
*/
286+
public function successLangcodes()
287+
{
288+
return array(
289+
array('1', array('ay', 'bo', 'cgg', 'dz', 'id', 'ja', 'jbo', 'ka', 'kk', 'km', 'ko', 'ky')),
290+
array('2', array('nl', 'fr', 'en', 'de', 'de_GE', 'hy', 'hy_AM')),
291+
array('3', array('be', 'bs', 'cs', 'hr')),
292+
array('4', array('cy', 'mt', 'sl')),
293+
array('6', array('ar')),
294+
);
295+
}
296+
297+
/**
298+
* This array should be at least empty within the near future.
299+
*
300+
* This both depends on a complete list trying to add above as understanding
301+
* the plural rules of the current failing languages.
302+
*
303+
* @return array with nplural together with langcodes
304+
*/
305+
public function failingLangcodes()
306+
{
307+
return array(
308+
array('1', array('fa')),
309+
array('2', array('jbo')),
310+
array('3', array('cbs')),
311+
array('4', array('gd', 'kw')),
312+
array('5', array('ga')),
313+
);
314+
}
315+
316+
/**
317+
* We validate only on the plural coverage. Thus the real rules is not tested.
318+
*
319+
* @param string $nplural Plural expected
320+
* @param array $matrix Containing langcodes and their plural index values
321+
* @param bool $expectSuccess
322+
*/
323+
protected function validateMatrix($nplural, $matrix, $expectSuccess = true)
324+
{
325+
foreach ($matrix as $langCode => $data) {
326+
$indexes = array_flip($data);
327+
if ($expectSuccess) {
328+
$this->assertEquals($nplural, \count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
329+
} else {
330+
$this->assertNotEquals((int) $nplural, \count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
331+
}
332+
}
333+
}
334+
335+
protected function generateTestData($langCodes)
336+
{
337+
$translator = new class() {
338+
use TranslatorTrait {
339+
getPluralizationRule as public;
340+
}
341+
};
342+
343+
$matrix = array();
344+
foreach ($langCodes as $langCode) {
345+
for ($count = 0; $count < 200; ++$count) {
346+
$plural = $translator->getPluralizationRule($count, $langCode);
347+
$matrix[$langCode][$count] = $plural;
348+
}
349+
}
350+
351+
return $matrix;
352+
}
79353
}

Translation/TranslatorInterface.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,39 @@ interface TranslatorInterface
1919
/**
2020
* Translates the given message.
2121
*
22+
* When a number is provided as a parameter named "%count%", the message is parsed for plural
23+
* forms and a translation is chosen according to this number using the following rules:
24+
*
25+
* Given a message with different plural translations separated by a
26+
* pipe (|), this method returns the correct portion of the message based
27+
* on the given number, locale and the pluralization rules in the message
28+
* itself.
29+
*
30+
* The message supports two different types of pluralization rules:
31+
*
32+
* interval: {0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples
33+
* indexed: There is one apple|There are %count% apples
34+
*
35+
* The indexed solution can also contain labels (e.g. one: There is one apple).
36+
* This is purely for making the translations more clear - it does not
37+
* affect the functionality.
38+
*
39+
* The two methods can also be mixed:
40+
* {0} There are no apples|one: There is one apple|more: There are %count% apples
41+
*
42+
* An interval can represent a finite set of numbers:
43+
* {1,2,3,4}
44+
*
45+
* An interval can represent numbers between two numbers:
46+
* [1, +Inf]
47+
* ]-1,2[
48+
*
49+
* The left delimiter can be [ (inclusive) or ] (exclusive).
50+
* The right delimiter can be [ (exclusive) or ] (inclusive).
51+
* Beside numbers, you can use -Inf and +Inf for the infinite.
52+
*
53+
* @see https://en.wikipedia.org/wiki/ISO_31-11
54+
*
2255
* @param string $id The message id (may also be an object that can be cast to string)
2356
* @param array $parameters An array of parameters for the message
2457
* @param string|null $domain The domain for the message or null to use the default

0 commit comments

Comments
 (0)