Skip to content

Commit 0523647

Browse files
Nyholmnicolas-grekas
authored andcommitted
[Translator] Deprecated transChoice and moved it away from contracts
1 parent 070488a commit 0523647

File tree

3 files changed

+0
-531
lines changed

3 files changed

+0
-531
lines changed

Tests/Translation/TranslatorTest.php

Lines changed: 0 additions & 274 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,6 @@ 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, $parameters)
54-
{
55-
$translator = $this->getTranslator();
56-
$translator->setLocale('en');
57-
58-
$this->assertEquals($expected, $translator->transChoice($id, $number, $parameters));
59-
}
60-
61-
/**
62-
* @dataProvider getTransChoiceTests
63-
*/
64-
public function testTransChoiceWithDefaultLocale($expected, $id, $number, $parameters)
65-
{
66-
\Locale::setDefault('en');
67-
68-
$translator = $this->getTranslator();
69-
70-
$this->assertEquals($expected, $translator->transChoice($id, $number, $parameters));
71-
}
72-
7350
public function testGetSetLocale()
7451
{
7552
$translator = $this->getTranslator();
@@ -99,255 +76,4 @@ public function getTransTests()
9976
array('Symfony is awesome!', 'Symfony is %what%!', array('%what%' => 'awesome')),
10077
);
10178
}
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, array('%count%' => 0)),
107-
array('There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1, array('%count%' => 1)),
108-
array('There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10, array('%count%' => 10)),
109-
array('There are 0 apples', 'There is 1 apple|There are %count% apples', 0, array('%count%' => 0)),
110-
array('There is 1 apple', 'There is 1 apple|There are %count% apples', 1, array('%count%' => 1)),
111-
array('There are 10 apples', 'There is 1 apple|There are %count% apples', 10, array('%count%' => 10)),
112-
// custom validation messages may be coded with a fixed value
113-
array('There are 2 apples', 'There are 2 apples', 2, array('%count%' => 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->transChoice($interval.' foo|[1,Inf[ bar', $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->transChoice($id, $number));
150-
}
151-
152-
public function testReturnMessageIfExactlyOneStandardRuleIsGiven()
153-
{
154-
$translator = $this->getTranslator();
155-
156-
$this->assertEquals('There are two apples', $translator->transChoice('There are two apples', 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->transChoice($id, $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 %count% apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10),
190-
array('There are %count% apples', '{0} There are no apples|{1} There is one apple|]1,Inf]There are %count% apples', 10),
191-
array('There are %count% apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10),
192-
193-
array('There are %count% 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 %count% apples', 'There is one apple|There are %count% apples', 10),
196-
197-
array('There are %count% 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 %count% 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 %count% 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 %count% 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 %count% 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-
}
35379
}

Translation/TranslatorInterface.php

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,51 +30,6 @@ interface TranslatorInterface
3030
*/
3131
public function trans($id, array $parameters = array(), $domain = null, $locale = null);
3232

33-
/**
34-
* Translates the given choice message by choosing a translation according to a number.
35-
*
36-
* Given a message with different plural translations separated by a
37-
* pipe (|), this method returns the correct portion of the message based
38-
* on the given number, locale and the pluralization rules in the message
39-
* itself.
40-
*
41-
* The message supports two different types of pluralization rules:
42-
*
43-
* interval: {0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples
44-
* indexed: There is one apple|There are %count% apples
45-
*
46-
* The indexed solution can also contain labels (e.g. one: There is one apple).
47-
* This is purely for making the translations more clear - it does not
48-
* affect the functionality.
49-
*
50-
* The two methods can also be mixed:
51-
* {0} There are no apples|one: There is one apple|more: There are %count% apples
52-
*
53-
* An interval can represent a finite set of numbers:
54-
* {1,2,3,4}
55-
*
56-
* An interval can represent numbers between two numbers:
57-
* [1, +Inf]
58-
* ]-1,2[
59-
*
60-
* The left delimiter can be [ (inclusive) or ] (exclusive).
61-
* The right delimiter can be [ (exclusive) or ] (inclusive).
62-
* Beside numbers, you can use -Inf and +Inf for the infinite.
63-
*
64-
* @see https://en.wikipedia.org/wiki/ISO_31-11
65-
*
66-
* @param string $id The message id (may also be an object that can be cast to string)
67-
* @param int $number The number to use to find the indice of the message
68-
* @param array $parameters An array of parameters for the message
69-
* @param string|null $domain The domain for the message or null to use the default
70-
* @param string|null $locale The locale or null to use the default
71-
*
72-
* @return string The translated string
73-
*
74-
* @throws \InvalidArgumentException If the locale contains invalid characters
75-
*/
76-
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null);
77-
7833
/**
7934
* Sets the current locale.
8035
*

0 commit comments

Comments
 (0)