From dc708dbd7e5c7d7a689cc0b1e1e084120811fde2 Mon Sep 17 00:00:00 2001 From: Ben Scholzen Date: Sat, 23 Jun 2012 19:09:39 +0200 Subject: [PATCH] [zen-18] refactor TextDomain and update documentation --- src/Translator/Loader/Gettext.php | 2 +- src/Translator/Loader/PhpArray.php | 2 +- src/Translator/TextDomain.php | 21 ++++++--------------- src/Translator/Translator.php | 2 +- 4 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/Translator/Loader/Gettext.php b/src/Translator/Loader/Gettext.php index 0b2bac0b..af53fce0 100644 --- a/src/Translator/Loader/Gettext.php +++ b/src/Translator/Loader/Gettext.php @@ -146,7 +146,7 @@ public function load($filename, $locale) list($header, $content) = explode(':', $rawHeader, 1); if (trim(strtolower($header)) === 'plural-forms') { - $textDomain->setPluralRule( + $textDomain->pluralRule( PluralRule::fromString($content) ); } diff --git a/src/Translator/Loader/PhpArray.php b/src/Translator/Loader/PhpArray.php index b4439726..2a5457ac 100644 --- a/src/Translator/Loader/PhpArray.php +++ b/src/Translator/Loader/PhpArray.php @@ -64,7 +64,7 @@ public function load($filename, $locale) if (array_key_exists('', $textDomain)) { if (isset($textDomain['']['plural_forms'])) { - $textDomain->setPluralRule( + $textDomain->pluralRule( PluralRule::fromString($textDomain['']['plural_forms']) ); } diff --git a/src/Translator/TextDomain.php b/src/Translator/TextDomain.php index 555396f5..e71ad6df 100644 --- a/src/Translator/TextDomain.php +++ b/src/Translator/TextDomain.php @@ -43,28 +43,19 @@ class TextDomain extends ArrayObject protected $pluralRule; /** - * Get the plural rule. + * Get or set the plural rule. * + * @param PluralRule $rule * @return PluralRule */ - public function getPluralRule() + public function pluralRule(PluralRule $rule = null) { - if ($this->pluralRule === null) { + if ($rule !== null) { + $this->pluralRule = $rule; + } elseif ($this->pluralRule === null) { $this->pluralRule = PluralRule::fromString('nplurals=2; plural=n==1'); } return $this->pluralRule; } - - /** - * Set the plural rule. - * - * @param PluralRule $rule - * @return TextDomain - */ - public function setPluralRule(PluralRule $rule) - { - $this->pluralRule = $rule; - return $this; - } } diff --git a/src/Translator/Translator.php b/src/Translator/Translator.php index 058aff74..56b965ad 100644 --- a/src/Translator/Translator.php +++ b/src/Translator/Translator.php @@ -232,7 +232,7 @@ public function translatePlural( return ($number != 1 ? $singular : $plural); } - $index = $this->messages[$textDomain][$locale]['']->evaluate($number); + $index = $this->messages[$textDomain][$locale]->pluralRule()->evaluate($number); if (!isset($translation[$index])) { throw new Exception\OutOfBoundsException(sprintf(