Exemplo n.º 1
0
 /**
  * Translates the given string
  * returns the translation
  *
  * @see Zend_Locale
  * @param  string|array       $messageId Translation string, or Array for plural translations
  * @param  string|\Zend\Locale\Locale $locale    (optional) Locale/Language to use, identical with
  *                                       locale identifier, @see Zend_Locale for more information
  * @return string
  */
 public function translate($messageId, $locale = null)
 {
     if ($locale === null) {
         $locale = $this->_options['locale'];
     }
     $plural = null;
     if (is_array($messageId)) {
         if (count($messageId) > 2) {
             $number = array_pop($messageId);
             if (!is_numeric($number)) {
                 $plocale = $number;
                 $number = array_pop($messageId);
             } else {
                 $plocale = 'en';
             }
             $plural = $messageId;
             $messageId = $messageId[0];
         } else {
             $messageId = $messageId[0];
         }
     }
     if (!Locale\Locale::isLocale($locale, true, false)) {
         if (!Locale\Locale::isLocale($locale, false, false)) {
             // language does not exist, return original string
             $this->_log($messageId, $locale);
             // use rerouting when enabled
             if (!empty($this->_options['route'])) {
                 if (array_key_exists($locale, $this->_options['route']) && !array_key_exists($locale, $this->_routed)) {
                     $this->_routed[$locale] = true;
                     return $this->translate($messageId, $this->_options['route'][$locale]);
                 }
             }
             $this->_routed = array();
             if ($plural === null) {
                 return $messageId;
             }
             $rule = Plural::getPlural($number, $plocale);
             if (!isset($plural[$rule])) {
                 $rule = 0;
             }
             return $plural[$rule];
         }
         $locale = new Locale\Locale($locale);
     }
     $locale = (string) $locale;
     if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
         // return original translation
         if ($plural === null) {
             $this->_routed = array();
             return $this->_translate[$locale][$messageId];
         }
         $rule = Plural::getPlural($number, $locale);
         if (isset($this->_translate[$locale][$plural[0]][$rule])) {
             $this->_routed = array();
             return $this->_translate[$locale][$plural[0]][$rule];
         }
     } else {
         if (strlen($locale) != 2) {
             // faster than creating a new locale and separate the leading part
             $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
             if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
                 // return regionless translation (en_US -> en)
                 if ($plural === null) {
                     $this->_routed = array();
                     return $this->_translate[$locale][$messageId];
                 }
                 $rule = Plural::getPlural($number, $locale);
                 if (isset($this->_translate[$locale][$plural[0]][$rule])) {
                     $this->_routed = array();
                     return $this->_translate[$locale][$plural[0]][$rule];
                 }
             }
         }
     }
     $this->_log($messageId, $locale);
     // use rerouting when enabled
     if (!empty($this->_options['route'])) {
         if (array_key_exists($locale, $this->_options['route']) && !array_key_exists($locale, $this->_routed)) {
             $this->_routed[$locale] = true;
             return $this->translate($messageId, $this->_options['route'][$locale]);
         }
     }
     $this->_routed = array();
     if ($plural === null) {
         return $messageId;
     }
     $rule = Plural::getPlural($number, $plocale);
     if (!isset($plural[$rule])) {
         $rule = 0;
     }
     return $plural[$rule];
 }
Exemplo n.º 2
0
 /**
  * Tests getting plurals from lowered locale
  */
 public function testGettingPluralsUsingOwnRule()
 {
     $lang = new Translator\Translator(Translator\Translator::AN_ARRAY, array('singular' => array('plural_0 (en)', 'plural_1 (en)', 'plural_2 (en)', 'plural_3 (en)'), 'plural' => ''), 'en');
     $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'en_US');
     $lang->setLocale('en_US');
     Translator\Plural::setPlural(array($this, 'customPlural'), 'en_US');
     $this->assertEquals('plural_1 (en)', $lang->translate(array('singular', 'plural', 1)));
     $this->assertEquals('plural_1 (en)', $lang->plural('singular', 'plural', 1));
     $this->assertEquals('plural_1 (en)', $lang->translate(array('singular', 'plural', 0)));
     $this->assertEquals('plural_1 (en)', $lang->plural('singular', 'plural', 0));
 }