You are here

public function DefaultTranslator::transChoice in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/DefaultTranslator.php \Symfony\Component\Validator\DefaultTranslator::transChoice()

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 \Symfony\Component\Translation\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.

Parameters

string $id The message id:

int $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:

Return value

string The translated string

Throws

InvalidArgumentException If the message id does not have the format "singular|plural".

Overrides TranslatorInterface::transChoice

File

vendor/symfony/validator/DefaultTranslator.php, line 135

Class

DefaultTranslator
Simple translator implementation that simply replaces the parameters in the message IDs.

Namespace

Symfony\Component\Validator

Code

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);
}