Interpolates the given choice message by choosing a variant according to a number.
The variants are passed in the message ID using the format "<singular>|<plural>". "<singular>" is chosen if the passed $number is exactly 1. "<plural>" is chosen otherwise.
This format is consistent with the format supported by {@link \Symfony\Component\Translation\Translator}, but it does not have the same expressiveness. While Translator supports intervals in message translations, which are needed for languages other than English, this translator does not. You should use Translator or a custom implementation of {@link TranslatorInterface} if you need this or similar functionality.
Example usage:
echo $translator->transChoice( 'This is {{ count }} donkey.|These are {{ count }} donkeys.', 0, array('{{ count }}' => 0) );
// -> These are 0 donkeys.
echo $translator->transChoice( 'This is {{ count }} donkey.|These are {{ count }} donkeys.', 1, array('{{ count }}' => 1) );
// -> This is 1 donkey.
echo $translator->transChoice( 'This is {{ count }} donkey.|These are {{ count }} donkeys.', 3, array('{{ count }}' => 3) );
// -> These are 3 donkeys.
string $id The message id:
integer $number The number to use to find the index of the message:
array $parameters An array of parameters for the message:
string $domain Ignored:
string $locale Ignored:
string The translated string
InvalidArgumentException If the message id does not have the format "singular|plural".
Overrides TranslatorInterface::transChoice
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) {
$ids = explode('|', $id);
if (1 == $number) {
return strtr($ids[0], $parameters);
}
if (!isset($ids[1])) {
throw new InvalidArgumentException(sprintf('The message "%s" cannot be pluralized, because it is missing a plural (e.g. "There is one apple|There are %%count%% apples").', $id));
}
return strtr($ids[1], $parameters);
}