Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c3b6404

Browse files
committed
[Validation] Fixed IdentityTranslator to pass correct Locale to MessageSelector
1 parent 32a1531 commit c3b6404

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

IdentityTranslator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
class IdentityTranslator implements TranslatorInterface
2222
{
2323
private $selector;
24+
private $locale;
2425

2526
/**
2627
* Constructor.
@@ -41,6 +42,7 @@ public function __construct(MessageSelector $selector)
4142
*/
4243
public function setLocale($locale)
4344
{
45+
$this->locale = $locale;
4446
}
4547

4648
/**
@@ -50,6 +52,7 @@ public function setLocale($locale)
5052
*/
5153
public function getLocale()
5254
{
55+
return $this->locale ?: \Locale::getDefault();
5356
}
5457

5558
/**
@@ -69,6 +72,6 @@ public function trans($id, array $parameters = array(), $domain = 'messages', $l
6972
*/
7073
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
7174
{
72-
return strtr($this->selector->choose((string) $id, (int) $number, $locale), $parameters);
75+
return strtr($this->selector->choose((string) $id, (int) $number, $locale ?: $this->getLocale()), $parameters);
7376
}
7477
}

Tests/IdentityTranslatorTest.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,43 @@ public function testTrans($expected, $id, $parameters)
2929
/**
3030
* @dataProvider getTransChoiceTests
3131
*/
32-
public function testTransChoice($expected, $id, $number, $parameters)
32+
public function testTransChoiceWithExplicitLocale($expected, $id, $number, $parameters)
3333
{
34+
$translator = new IdentityTranslator(new MessageSelector());
35+
$translator->setLocale('en');
36+
37+
$this->assertEquals($expected, $translator->transChoice($id, $number, $parameters));
38+
}
39+
40+
/**
41+
* @dataProvider getTransChoiceTests
42+
*/
43+
public function testTransChoiceWithDefaultLocale($expected, $id, $number, $parameters)
44+
{
45+
\Locale::setDefault('en');
46+
3447
$translator = new IdentityTranslator(new MessageSelector());
3548

3649
$this->assertEquals($expected, $translator->transChoice($id, $number, $parameters));
3750
}
3851

39-
// noop
4052
public function testGetSetLocale()
4153
{
4254
$translator = new IdentityTranslator(new MessageSelector());
4355
$translator->setLocale('en');
44-
$translator->getLocale();
56+
57+
$this->assertEquals('en', $translator->getLocale());
58+
}
59+
60+
public function testGetLocaleReturnsDefaultLocaleIfNotSet()
61+
{
62+
$translator = new IdentityTranslator(new MessageSelector());
63+
64+
\Locale::setDefault('en');
65+
$this->assertEquals('en', $translator->getLocale());
66+
67+
\Locale::setDefault('pt_BR');
68+
$this->assertEquals('pt_BR', $translator->getLocale());
4569
}
4670

4771
public function getTransTests()
@@ -55,7 +79,12 @@ public function getTransTests()
5579
public function getTransChoiceTests()
5680
{
5781
return array(
58-
array('There is 10 apples', '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples', 10, array('%count%' => 10)),
82+
array('There is no apple', '{0} There is no apple|{1} There is one apple|]1,Inf] There are %count% apples', 0, array('%count%' => 0)),
83+
array('There is one apple', '{0} There is no apple|{1} There is one apple|]1,Inf] There are %count% apples', 1, array('%count%' => 1)),
84+
array('There are 10 apples', '{0} There is no apple|{1} There is one apple|]1,Inf] There are %count% apples', 10, array('%count%' => 10)),
85+
array('There are 0 apples', 'There is 1 apple|There are %count% apples', 0, array('%count%' => 0)),
86+
array('There is 1 apple', 'There is 1 apple|There are %count% apples', 1, array('%count%' => 1)),
87+
array('There are 10 apples', 'There is 1 apple|There are %count% apples', 10, array('%count%' => 10)),
5988
);
6089
}
6190
}

0 commit comments

Comments
 (0)