Exemple #1
0
 /**
  * @param  string  $message  The translated message string to pluralizations
  * @param  string  $args     The arguments for the strtr
  * @param  string  $locale   The language in ISO format
  * @return string
  */
 public static function pluralize($message, $args, $locale)
 {
     // Gets the value of the first array element, or FALSE if the array is empty:
     $firstArg = reset($args);
     if (count($args) <= 1) {
         if (is_int($firstArg) || is_numeric($firstArg)) {
             // Simple case with one value:
             if (strpos($message, '|')) {
                 // Has plural choices ('|' exists and not at first position):
                 return static::choose($message, $firstArg, $locale);
             }
         }
         return $message;
     }
     // Case with multiple $args and possibly multiple parts of sentence to pluralize, each separated by '||':
     return implode(preg_replace_callback('/^.*(%%[^%]+%%).*$/', function (&$matches) use($args, $locale) {
         if (isset($args[$matches[1]])) {
             return Pluralization::choose($matches[0], $args[$matches[1]], $locale);
         }
         // Variable needed to evaluate choice not available: log and return untouched message:
         //TODO here log translation problem:
         //sprintf('Unable to choose a translation for "%s" with locale "%s". Double check that this translation has the correct plural options including correct %%-delimited variable (e.g. "There is one apple|There are %%count%% apples"). Here "%s" is not found in the available variables (%s).', $matches[0], $locale, $matches[1], implode( ',', array_keys( $args ) ) );
         return $matches[0];
     }, explode('||', $message)));
 }