Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow merging text domains without plural rules #5908

Closed
wants to merge 1 commit into from

3 participants

@DASPRiD
Collaborator

This pull request fixes the issue addressed by #5277 in a much cleaner way. Thus closes #5277.

@weierophinney weierophinney added this to the 2.3.0 milestone
@samsonasik samsonasik commented on the diff
library/Zend/I18n/Translator/TextDomain.php
((34 lines not shown))
{
- if ($this->pluralRule === null) {
- $this->setPluralRule(PluralRule::fromString('nplurals=2; plural=n != 1;'));
+ if (self::$defaultPluralRule === null) {
+ self::$defaultPluralRule = PluralRule::fromString('nplurals=2; plural=n != 1;');

should it be static:: instead of self:: ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@samsonasik samsonasik commented on the diff
library/Zend/I18n/Translator/TextDomain.php
@@ -40,17 +47,40 @@ public function setPluralRule(PluralRule $rule)
/**
* Get the plural rule.
*
- * Lazy loads a default rule if none already registered
+ * @param bool $fallbackToDefaultRule
+ * @return PluralRule|null
+ */
+ public function getPluralRule($fallbackToDefaultRule = true)
+ {
+ if ($this->pluralRule === null && $fallbackToDefaultRule) {
+ return self::getDefaultPluralRule();

should it be static:: instead of self:: ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#5908] s/self/static/
- in order to invoke LSB for extensions
3e003e0
@weierophinney weierophinney self-assigned this
@weierophinney

Merged to develop for release in 2.3.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 5, 2014
  1. @DASPRiD
This page is out of date. Refresh to see the latest.
View
48 library/Zend/I18n/Translator/TextDomain.php
@@ -26,6 +26,13 @@ class TextDomain extends ArrayObject
protected $pluralRule;
/**
+ * Default plural rule shared between instances.
+ *
+ * @var PluralRule
+ */
+ protected static $defaultPluralRule;
+
+ /**
* Set the plural rule
*
* @param PluralRule $rule
@@ -40,17 +47,40 @@ public function setPluralRule(PluralRule $rule)
/**
* Get the plural rule.
*
- * Lazy loads a default rule if none already registered
+ * @param bool $fallbackToDefaultRule
+ * @return PluralRule|null
+ */
+ public function getPluralRule($fallbackToDefaultRule = true)
+ {
+ if ($this->pluralRule === null && $fallbackToDefaultRule) {
+ return self::getDefaultPluralRule();

should it be static:: instead of self:: ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+
+ return $this->pluralRule;
+ }
+
+ /**
+ * Checks whether the text domain has a plural rule.
+ *
+ * @return bool
+ */
+ public function hasPluralRule()
+ {
+ return ($this->pluralRule !== null);
+ }
+
+ /**
+ * Returns a shared default plural rule.
*
* @return PluralRule
*/
- public function getPluralRule()
+ public static function getDefaultPluralRule()
{
- if ($this->pluralRule === null) {
- $this->setPluralRule(PluralRule::fromString('nplurals=2; plural=n != 1;'));
+ if (self::$defaultPluralRule === null) {
+ self::$defaultPluralRule = PluralRule::fromString('nplurals=2; plural=n != 1;');

should it be static:: instead of self:: ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
}
- return $this->pluralRule;
+ return self::$defaultPluralRule;
}
/**
@@ -66,8 +96,12 @@ public function getPluralRule()
*/
public function merge(TextDomain $textDomain)
{
- if ($this->getPluralRule()->getNumPlurals() !== $textDomain->getPluralRule()->getNumPlurals()) {
- throw new Exception\RuntimeException('Plural rule of merging text domain is not compatible with the current one');
+ if ($this->hasPluralRule() && $textDomain->hasPluralRule()) {
+ if ($this->getPluralRule()->getNumPlurals() !== $textDomain->getPluralRule()->getNumPlurals()) {
+ throw new Exception\RuntimeException('Plural rule of merging text domain is not compatible with the current one');
+ }
+ } elseif ($textDomain->hasPluralRule()) {
+ $this->setPluralRule($textDomain->getPluralRule());
}
$this->exchangeArray(
View
33 tests/ZendTest/I18n/Translator/TextDomainTest.php
@@ -60,8 +60,41 @@ public function testMergingIncompatibleTextDomains()
$domainA = new TextDomain();
$domainB = new TextDomain();
+ $domainA->setPluralRule(PluralRule::fromString('nplurals=3; plural=n'));
+ $domainB->setPluralRule(PluralRule::fromString('nplurals=2; plural=n'));
+
+ $domainA->merge($domainB);
+ }
+
+ public function testMergingTextDomainsWithPluralRules()
+ {
+ $domainA = new TextDomain();
+ $domainB = new TextDomain();
+
+ $domainA->merge($domainB);
+ $this->assertFalse($domainA->hasPluralRule());
+ $this->assertFalse($domainB->hasPluralRule());
+ }
+
+ public function testMergingTextDomainWithPluralRuleIntoTextDomainWithoutPluralRule()
+ {
+ $domainA = new TextDomain();
+ $domainB = new TextDomain();
$domainB->setPluralRule(PluralRule::fromString('nplurals=3; plural=n'));
$domainA->merge($domainB);
+ $this->assertEquals(3, $domainA->getPluralRule()->getNumPlurals());
+ $this->assertEquals(3, $domainB->getPluralRule()->getNumPlurals());
+ }
+
+ public function testMergingTextDomainWithoutPluralRuleIntoTextDomainWithPluralRule()
+ {
+ $domainA = new TextDomain();
+ $domainB = new TextDomain();
+ $domainA->setPluralRule(PluralRule::fromString('nplurals=3; plural=n'));
+
+ $domainA->merge($domainB);
+ $this->assertEquals(3, $domainA->getPluralRule()->getNumPlurals());
+ $this->assertFalse($domainB->hasPluralRule());
}
}
Something went wrong with that request. Please try again.