public DrupalTranslator::transChoice($id, $number, array $parameters = array(), $domain = NULL, $locale = NULL)
Translates the given choice message by choosing a translation according to a number.
Parameters
string $id The message id (may also be an object that can be cast to string):
int $number The number to use to find the indice of the message:
array $parameters An array of parameters for the message:
string|null $domain The domain for the message or null to use the default:
string|null $locale The locale or null to use the default:
Return value
string The translated string
Throws
\InvalidArgumentException If the locale contains invalid characters
Overrides TranslatorInterface::transChoice
File
- core/lib/Drupal/Core/Validation/DrupalTranslator.php, line 34
Class
- DrupalTranslator
- Translates strings using Drupal's translation system.
Namespace
Drupal\Core\Validation
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public function transChoice( $id , $number , array $parameters = array (), $domain = NULL, $locale = NULL) { // Violation messages can separated singular and plural versions by "|". $ids = explode ( '|' , $id ); 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 )); } // Normally, calls to formatPlural() need to use literal strings, like // formatPlural($count, '1 item', '@count items') // so that the Drupal project POTX string extractor will correctly // extract the strings for translation and save them in a format that // formatPlural() can work with. However, this is a special case, because // Drupal is supporting a constraint message format from Symfony. So // although $id looks like a variable here, it is actually coming from a // static string in a constraint class that the POTX extractor knows about // and has processed to work with formatPlural(), so this specific call to // formatPlural() will work correctly. return \Drupal::translation()->formatPlural( $number , $ids [0], $ids [1], $this ->processParameters( $parameters ), $this ->getOptions( $domain , $locale )); } |
Please login to continue.