2 namespace wcf\system\template\plugin
;
3 use wcf\system\exception\SystemException
;
4 use wcf\system\language\I18nPlural
;
5 use wcf\system\template\TemplateEngine
;
6 use wcf\util\StringUtil
;
9 * Template function plugin which generate plural phrases.
11 * Languages vary in how they handle plurals of nouns or unit expressions.
12 * Some languages have two forms, like English; some languages have only a
13 * single form; and some languages have multiple forms.
15 * Supported parameters:
16 * value (number|array|Countable - required)
17 * other (string - required)
18 * zero (string), one (string), two (string), few (string), many (string)
21 * {plural value=$number zero='0' one='1' two='2' few='few' many='many' other='#'}
22 * There {plural value=$worlds one='is one world' other='are # worlds'}
23 * Updated {plural value=$minutes 0='just now' 1='one minute ago' other='# minutes ago'}
26 * @copyright 2001-2020 WoltLab GmbH
27 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
28 * @package WoltLabSuite\Core\System\Template\Plugin
31 class PluralFunctionTemplatePlugin
implements IFunctionTemplatePlugin
{
35 public function execute($tagArgs, TemplateEngine
$tplObj) {
36 if (!isset($tagArgs['value'])) {
37 throw new SystemException("Missing attribute 'value'");
39 if (!isset($tagArgs['other'])) {
40 throw new SystemException("Missing attribute 'other'");
43 $value = $tagArgs['value'];
44 if (is_countable($value)) {
45 $value = count($value);
48 // handle numeric attributes
49 foreach ($tagArgs as $key => $_value) {
50 if (is_numeric($key)) {
51 if ($key == $value) return $_value;
55 $category = I18nPlural
::getCategory($value);
56 if (!isset($tagArgs[$category])) {
57 $category = I18nPlural
::PLURAL_OTHER
;
60 $string = $tagArgs[$category];
61 if (strpos($string, '#') !== false) {
62 return str_replace('#', StringUtil
::formatNumeric($value), $string);