3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\I18n\View\Helper
;
12 use Zend\I18n\Exception
;
13 use Zend\I18n\Translator\Plural\Rule
as PluralRule
;
14 use Zend\View\Helper\AbstractHelper
;
17 * Helper for rendering text based on a count number (like the I18n plural translation helper, but when translation
20 * Please note that we did not write any hard-coded rules for languages, as languages can evolve, we prefered to
21 * let the developer define the rules himself, instead of potentially break applications if we change rules in the
24 * However, you can find most of the up-to-date plural rules for most languages in those links:
25 * - http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
26 * - https://developer.mozilla.org/en-US/docs/Localization_and_Plurals
28 class Plural
extends AbstractHelper
38 * @throws Exception\ExtensionNotLoadedException if ext/intl is not present
40 public function __construct()
42 if (!extension_loaded('intl')) {
43 throw new Exception\
ExtensionNotLoadedException(sprintf(
44 '%s component requires the intl PHP extension',
51 * Given an array of strings, a number and, if wanted, an optional locale (the default one is used
52 * otherwise), this picks the right string according to plural rules of the locale
54 * @param array|string $strings
56 * @throws Exception\InvalidArgumentException
59 public function __invoke($strings, $number)
61 if (null === $this->getPluralRule()) {
62 throw new Exception\
InvalidArgumentException(sprintf(
63 'No plural rule was set'
67 if (!is_array($strings)) {
68 $strings = (array) $strings;
71 $pluralIndex = $this->getPluralRule()->evaluate($number);
73 return $strings[$pluralIndex];
77 * Set the plural rule to use
79 * @param PluralRule|string $pluralRule
82 public function setPluralRule($pluralRule)
84 if (!$pluralRule instanceof PluralRule
) {
85 $pluralRule = PluralRule
::fromString($pluralRule);
88 $this->rule
= $pluralRule;
94 * Get the plural rule to use
98 public function getPluralRule()