/** * @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))); }
/** * returns html page count results as "Results 1-10 of x" or if raw returns array of results as array( from, to, total ) * @param bool $raw * @return array|string */ function getPagesCounter($raw = false) { $fromResult = $this->limitstart + 1; if ($this->limitstart + $this->limit < $this->total) { $toResult = $this->limitstart + $this->limit; } else { $toResult = $this->total; } if ($raw) { $return = $this->total > 0 ? array(0, 0, (int) $this->total) : array((int) $fromResult, (int) $toResult, (int) $this->total); } else { $return = '<span class="' . htmlspecialchars($this->classes['cbPaginationCounter']) . '">'; if ($this->total > 0) { // Formatted that way to not be picked-up by translation grabber, as used for pluralization, not for translation: $return .= strtr(Pluralization::pluralize('{1} ([NUMFIRST]/%%TOTALRESULTS%%)|]1,Inf] ([NUMFIRST]-[NUMLAST]/%%TOTALRESULTS%%)', array('%%TOTALRESULTS%%' => (int) $this->total), 'en'), array('[NUMFIRST]' => (int) $fromResult, '[NUMLAST]' => (int) $toResult, '%%TOTALRESULTS%%' => (int) $this->total)); } else { $return .= CBTxt::Th('PAGENAV_NO_RESULTS UE_NO_RESULTS', 'No results'); } $return .= '</span>'; } return $return; }