Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /modules/zend.i18n.view.helper.plural.html

Size:14348
Storage flags:no_autoload,compress/gzip (28%)

Plural Helper — Zend Framework 2 2.4.2 documentation

Plural Helper

Most languages have specific rules for handling plurals. For instance, in English, we say “0 cars” and “2 cars” (plural) while we say “1 car” (singular). On the other hand, French uses the singular form for 0 and 1 (“0 voiture” and “1 voiture”) and uses the plural form otherwise (“3 voitures”).

Therefore, we often need to handle those plural cases even without using translation (mono-lingual application). The Plural helper was created for this. Please remember that, if you need to both handle translation and plural, you must use the TranslatePlural helper for that. Plural does not deal with translation.

Internally, the Plural helper uses the Zend\I18n\Translator\Plural\Rule class to handle rules.

Setup

In Zend Framework 1, there was a similar helper. However, this helper hardcoded rules for mostly every languages. The problem with this approach is that languages are alive and can evolve over time. Therefore, we would need to change the rules and hence break current applications that may (or may not) want those new rules.

That’s why defining rules is now up to the developer. To help you with this process, here are some links with up-to-date plural rules for tons of languages:

Basic Usage

The first thing to do is to defining rule. You may want to add this in your Module.php file, for example:

1
2
3
4
5
6
7
// Get the ViewHelperPlugin Manager from Service manager, so we can fetch the ``Plural``
// helper and add the plural rule for the application's language
$viewHelperManager = $serviceManager->get('ViewHelperManager');
$pluralHelper      = $viewHelperManager->get('Plural');

// Here is the rule for French
$pluralHelper->setPluralRule('nplurals=2; plural=(n==0 || n==1 ? 0 : 1)');

The string reads like that:

  1. First, we specify how many plurals forms we have. For French, only two (singular/plural).
  2. Then, we specify the rule. Here, if the count is 0 or 1, this is rule n°0 (singular) while it’s rule n°1 otherwise.

As we said earlier, English consider “1” as singular, and “0/other” as plural. Here is such a rule:

1
2
// Here is the rule for English
$pluralHelper->setPluralRule('nplurals=2; plural=(n==1 ? 0 : 1)');

Now that we have defined the rule, we can use it in our views:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php
   // If the rule defined in Module.php is the English one:

   echo $this->plural(array('car', 'cars'), 0); // prints "cars"
   echo $this->plural(array('car', 'cars'), 1); // prints "car"

   // If the rule defined in Module.php is the French one:
   echo $this->plural(array('voiture', 'voitures'), 0); // prints "voiture"
   echo $this->plural(array('voiture', 'voitures'), 1); // prints "voiture"
   echo $this->plural(array('voiture', 'voitures'), 2); // prints "voitures"
?>

Table Of Contents

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Plural Helper on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.

For more information about the PHK package format: http://phk.tekwire.net