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

Skip to content

Falling back to first translation if no choice available #6619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Symfony/Component/Translation/MessageSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class MessageSelector
*
* @return string
*
* @throws \InvalidArgumentException
*
* @api
*/
public function choose($message, $number, $locale)
Expand Down Expand Up @@ -74,7 +72,7 @@ public function choose($message, $number, $locale)

$position = PluralizationRules::get($number, $locale);
if (!isset($standardRules[$position])) {
throw new \InvalidArgumentException(sprintf('Unable to choose a translation for "%s" with locale "%s". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %%count%% apples").', $message, $locale));
return current($standardRules);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to remove the phpdoc exception too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks!

return $standardRules[$position];
Expand Down
13 changes: 3 additions & 10 deletions src/Symfony/Component/Translation/Tests/MessageSelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ public function testChoose($expected, $id, $number)
$this->assertEquals($expected, $selector->choose($id, $number, 'en'));
}

/**
* @expectedException InvalidArgumentException
*/
public function testChooseWhenNoEnoughChoices()
{
$selector = new MessageSelector();

$selector->choose('foo', 10, 'en');
}

public function getChooseTests()
{
return array(
Expand Down Expand Up @@ -75,6 +65,9 @@ public function getChooseTests()
array('There is no apples', '{0} There is no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0),
array('There is no apples', '{0} There is no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0.0),
array('There is no apples', '{0.0} There is no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0),

// #4228
array('There are %count% apples', 'There are %count% apples', 10),
);
}
}
5 changes: 1 addition & 4 deletions src/Symfony/Component/Translation/Tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,13 @@ public function testTransChoiceFallbackBis()
$this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testTransChoiceFallbackWithNoTranslation()
{
$translator = new Translator('ru', new MessageSelector());
$translator->setFallbackLocale('en');
$translator->addLoader('array', new ArrayLoader());

$this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
$this->assertEquals('some_message2', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
}
}

Expand Down