Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add plural helper to I18n #2415

Closed
wants to merge 9 commits into from

4 participants

@bakura10

This view helper is meant to work like the i18n plural view helper, but when translation is not needed.

It now takes advantage of PluralRules.

@DASPRid, @b-durand is it better ?

@DASPRiD DASPRiD was assigned
@b-durand

It sounds good.

Just fix Coding Standards (EOF in the new files).

@DASPRiD
Collaborator

@bakura10 Can you please fix the travis build and change the @return self to @return Plural so the PR can be merged? Thank you.

@bakura10

Done.

@DASPRiD
Collaborator

@bakura10 Travis build still failing.

@bakura10

It passes now :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 19, 2012
  1. @bakura10

    Add plural view helper

    bakura10 authored
  2. @bakura10
  3. @bakura10
Commits on Sep 20, 2012
  1. @bakura10

    Updated to handle every cases

    bakura10 authored
Commits on Sep 25, 2012
  1. @bakura10
  2. @bakura10
  3. @bakura10

    Change condition

    bakura10 authored
  4. @bakura10

    Updated docblock

    bakura10 authored
Commits on Sep 28, 2012
  1. @bakura10

    try to make tests pass

    bakura10 authored
This page is out of date. Refresh to see the latest.
View
84 library/Zend/I18n/View/Helper/Plural.php
@@ -0,0 +1,84 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_View
+ */
+
+namespace Zend\I18n\View\Helper;
+
+use Zend\I18n\Exception;
+use Zend\I18n\Translator\Plural\Rule as PluralRule;
+use Zend\View\Helper\AbstractHelper;
+
+/**
+ * Helper for rendering text based on a count number (like the I18n plural translation helper, but when translation
+ * is not needed).
+ *
+ * Please note that we did not write any hard-coded rules for languages, as languages can evolve, we prefered to
+ * let the developer define the rules himself, instead of potentially break applications if we change rules in the
+ * future.
+ *
+ * However, you can find most of the up-to-date plural rules for most languages in those links:
+ * - http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
+ * - https://developer.mozilla.org/en-US/docs/Localization_and_Plurals
+ *
+ * @category Zend
+ * @package Zend_I18n
+ * @subpackage View
+ */
+class Plural extends AbstractHelper
+{
+ /**
+ * Rule to use
+ *
+ * @var PluralRule
+ */
+ protected $rule;
+
+ /**
+ * Set the plural rule to use
+ *
+ * @param PluralRule|string $pluralRule
+ * @return Plural
+ */
+ public function setPluralRule($pluralRule)
+ {
+ if (!$pluralRule instanceof PluralRule) {
+ $pluralRule = PluralRule::fromString($pluralRule);
+ }
+
+ $this->rule = $pluralRule;
+
+ return $this;
+ }
+
+ /**
+ * Given an array of strings, a number and, if wanted, an optional locale (the default one is used
+ * otherwise), this picks the right string according to plural rules of the locale
+ *
+ * @param array|string $strings
+ * @param int $number
+ * @throws Exception\InvalidArgumentException
+ * @return string
+ */
+ public function __invoke($strings, $number)
+ {
+ if ($this->rule === null) {
+ throw new Exception\InvalidArgumentException(sprintf(
+ 'No plural rule was set'
+ ));
+ }
+
+ if (!is_array($strings)) {
+ $strings = (array) $strings;
+ }
+
+ $pluralIndex = $this->rule->evaluate($number);
+
+ return $strings[$pluralIndex];
+ }
+}
View
1  library/Zend/I18n/View/HelperConfig.php
@@ -29,6 +29,7 @@ class HelperConfig implements ConfigInterface
'currencyformat' => 'Zend\I18n\View\Helper\CurrencyFormat',
'dateformat' => 'Zend\I18n\View\Helper\DateFormat',
'numberformat' => 'Zend\I18n\View\Helper\NumberFormat',
+ 'plural' => 'Zend\I18n\View\Helper\Plural',
'translate' => 'Zend\I18n\View\Helper\Translate',
'translateplural' => 'Zend\I18n\View\Helper\TranslatePlural',
);
View
68 tests/ZendTest/I18n/View/Helper/PluralTest.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_I18n
+ */
+
+namespace ZendTest\I18n\View\Helper;
+
+use Zend\I18n\Translator\Plural\Rule as PluralRule;
+use Zend\I18n\View\Helper\Plural as PluralHelper;
+
+/**
+ * @category Zend
+ * @package Zend_View
+ * @subpackage UnitTests
+ * @group Zend_View
+ * @group Zend_View_Helper
+ */
+class PluralTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PluralHelper
+ */
+ public $helper;
+
+ /**
+ * Sets up the fixture
+ *
+ * @return void
+ */
+ public function setUp()
+ {
+ $this->helper = new PluralHelper();
+ }
+
+ /**
+ * @return array
+ */
+ public function pluralsTestProvider()
+ {
+ return array(
+ array('nplurals=1; plural=0', 'かさ', 0, 'かさ'),
+ array('nplurals=1; plural=0', 'かさ', 10, 'かさ'),
+
+ array('nplurals=2; plural=(n==1 ? 0 : 1)', array('umbrella', 'umbrellas'), 0, 'umbrellas'),
+ array('nplurals=2; plural=(n==1 ? 0 : 1)', array('umbrella', 'umbrellas'), 1, 'umbrella'),
+ array('nplurals=2; plural=(n==1 ? 0 : 1)', array('umbrella', 'umbrellas'), 2, 'umbrellas'),
+
+ array('nplurals=2; plural=(n==0 || n==1 ? 0 : 1)', array('parapluie', 'parapluies'), 0, 'parapluie'),
+ array('nplurals=2; plural=(n==0 || n==1 ? 0 : 1)', array('parapluie', 'parapluies'), 1, 'parapluie'),
+ array('nplurals=2; plural=(n==0 || n==1 ? 0 : 1)', array('parapluie', 'parapluies'), 2, 'parapluies'),
+ );
+ }
+
+ /**
+ * @dataProvider pluralsTestProvider
+ */
+ public function testGetCorrectPlurals($pluralRule, $strings, $number, $expected)
+ {
+ $this->helper->setPluralRule($pluralRule);
+ $result = $this->helper->__invoke($strings, $number);
+ $this->assertEquals($expected, $result);
+ }
+}
Something went wrong with that request. Please try again.