/**
  * Tests getting plurals from lowered locale
  */
 public function testGettingPluralsUsingOwnRule()
 {
     $lang = new Zend_Translate(Zend_Translate::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');
     Zend_Translate_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));
 }
Example #2
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    (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 (!Zend_Locale::isLocale($locale, true, false)) {
         if (!Zend_Locale::isLocale($locale, false, false)) {
             // language does not exist, return original string
             $this->_log($messageId, $locale);
             if ($plural === null) {
                 return $messageId;
             }
             $rule = Zend_Translate_Plural::getPlural($number, $plocale);
             if (!isset($plural[$rule])) {
                 $rule = 0;
             }
             return $plural[$rule];
         }
         $locale = new Zend_Locale($locale);
     }
     $locale = (string) $locale;
     if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
         // return original translation
         if ($plural === null) {
             return $this->_translate[$locale][$messageId];
         }
         $rule = Zend_Translate_Plural::getPlural($number, $locale);
         if (isset($this->_translate[$locale][$plural[0]][$rule])) {
             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) {
                     return $this->_translate[$locale][$messageId];
                 }
                 $rule = Zend_Translate_Plural::getPlural($number, $locale);
                 if (isset($this->_translate[$locale][$plural[0]][$rule])) {
                     return $this->_translate[$locale][$plural[0]][$rule];
                 }
             }
         }
     }
     $this->_log($messageId, $locale);
     if ($plural === null) {
         return $messageId;
     }
     $rule = Zend_Translate_Plural::getPlural($number, $plocale);
     if (!isset($plural[$rule])) {
         $rule = 0;
     }
     return $plural[$rule];
 }
 public function editAction()
 {
     $this->view->locale = $locale = $this->_getParam('locale');
     $this->view->page = $page = $this->_getParam('page');
     $translate = Zend_Registry::get('Zend_Translate');
     try {
         if (!$locale || !Zend_Locale::findLocale($locale)) {
             throw new Exception('missing locale ' . $locale);
         }
     } catch (Exception $e) {
         return $this->_helper->redirector->gotoRoute(array('action' => 'index', 'controller' => 'language'), 'admin_default', true);
     }
     // Process filter form
     $this->view->filterForm = $filterForm = new Core_Form_Admin_Language_Filter();
     $filterForm->isValid($this->_getAllParams());
     $filterValues = $filterForm->getValues();
     extract($filterValues);
     // search, show
     // Make query
     $filterValues = array_filter($filterValues);
     $this->view->values = $filterValues;
     $this->view->query = empty($filterValues) ? '' : '?' . http_build_query($filterValues);
     // Assign basic locale info
     $this->view->localeObject = $localeObject = new Zend_Locale($locale);
     $this->view->locale = $locale = $localeObject->toString();
     // Get locale translation info
     $localeLanguage = $localeObject->getLanguage();
     $localeRegion = $localeObject->getRegion();
     $this->view->localeLanguageTranslation = $localeLanguageTranslation = Zend_Locale::getTranslation($localeLanguage, 'language', Zend_Registry::get('Locale'));
     $this->view->localeRegionTranslation = $localeRegionTranslation = Zend_Locale::getTranslation($localeRegion, 'territory', Zend_Registry::get('Locale'));
     $translate = Zend_Registry::get('Zend_Translate');
     if ($localeLanguageTranslation && $localeRegionTranslation) {
         $this->view->localeTranslation = $localeLanguageTranslation . ' ' . sprintf($translate->translate('(%s)'), $localeRegionTranslation) . ' ' . sprintf($translate->translate('[%s]'), $locale);
     } else {
         if ($localeLanguageTranslation) {
             $this->view->localeTranslation = $localeLanguageTranslation . ' ' . sprintf($translate->translate('[%s]'), $locale);
         } else {
             $this->view->localeTranslation = sprintf($translate->translate('[%s]'), $locale);
         }
     }
     // Query plural system for max and sample space
     $sample = array();
     $max = 0;
     for ($i = 0; $i <= 1000; $i++) {
         $form = Zend_Translate_Plural::getPlural($i, $locale);
         $max = max($max, $form);
         if (@count($sample[$form]) < 3) {
             $sample[$form][] = $i;
         }
     }
     $this->view->pluralFormCount = $max + 1;
     $this->view->pluralFormSample = $sample;
     // Get initial and default values
     $baseMessages = $translate->getMessages('en');
     if ($translate->isAvailable($locale)) {
         $currentMessages = $translate->getMessages($locale);
     } else {
         $currentMessages = array();
         // @todo this should redirect or smth
     }
     // Get phrases that are not in the english pack?
     if (!empty($currentMessages) && $locale != 'en') {
         $missingBasePhrases = array_diff_key($currentMessages, $baseMessages);
         $missingBasePhrases = array_combine(array_keys($missingBasePhrases), array_keys($missingBasePhrases));
         $baseMessages = array_merge($baseMessages, $missingBasePhrases);
     }
     // Build the fancy array
     $resultantMessages = array();
     $missing = 0;
     $index = 0;
     foreach ($baseMessages as $key => $value) {
         // Build
         $composite = array('uid' => ++$index, 'key' => $key, 'original' => $value, 'plural' => (bool) is_array($value));
         // filters, plurals, and missing, oh my.
         if (isset($currentMessages[$key])) {
             if ('missing' == $show) {
                 continue;
             }
             if (is_array($value) && !is_array($currentMessages[$key])) {
                 $composite['current'] = array($currentMessages[$key]);
             } else {
                 if (!is_array($value) && is_array($currentMessages[$key])) {
                     $composite['current'] = current($currentMessages[$key]);
                 } else {
                     $composite['current'] = $currentMessages[$key];
                 }
             }
         } else {
             if ('translated' == $show) {
                 continue;
             }
             if (is_array($value)) {
                 $composite['current'] = array();
             } else {
                 $composite['current'] = '';
             }
             $missing++;
         }
         // Do search
         if ($search && !$this->_searchArrayRecursive($search, $composite)) {
             continue;
         }
         // Add
         $resultantMessages[] = $composite;
     }
     // Build the paginator
     $this->view->paginator = $paginator = Zend_Paginator::factory($resultantMessages);
     $paginator->setItemCountPerPage(50);
     $paginator->setCurrentPageNumber($page);
     // Process form POST
     if ($this->getRequest()->isPost()) {
         $keys = $this->_getParam('keys');
         $values = $this->_getParam('values');
         // Try to combine the values and keys arrays
         $combined = array();
         foreach ($values as $index => $value) {
             if (is_string($value)) {
                 if (empty($value)) {
                     continue;
                 }
                 $key = $keys[$index];
                 $combined[$key] = $value;
             } else {
                 if (is_array($value)) {
                     if (empty($value) || array_filter($value) === array()) {
                         continue;
                     }
                     $key = $keys[$index][0];
                     $combined[$key] = $value;
                 }
             }
         }
         // Try to write to a file
         $targetFile = APPLICATION_PATH . '/application/languages/' . $locale . '/custom.csv';
         if (!file_exists($targetFile)) {
             touch($targetFile);
             chmod($targetFile, 0777);
         }
         $writer = new Engine_Translate_Writer_Csv($targetFile);
         $writer->setTranslations($combined);
         $writer->write();
         // flush cached language vars
         @Zend_Registry::get('Zend_Cache')->clean();
         // redirect to this same page to get the new values
         return $this->_redirect($_SERVER['REQUEST_URI'], array('prependBase' => false));
     }
 }
Example #4
0
 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];
         }
     }
     $result = preg_split('`(?<!\\\\)\\@`', $messageId, 2, PREG_SPLIT_DELIM_CAPTURE);
     if (count($result) == 2) {
         list($messageId, $tags) = $result;
         $uidId = $this->getUidId($messageId);
         $tags = explode(',', $tags);
         foreach ($tags as $tag) {
             $tag = trim($tag);
             $tagId = $this->getTagId($tag);
             $this->_tagUidTable->getOrCreate(array('uid_id' => $uidId, 'tag_id' => $tagId));
         }
     } else {
         $uidId = $this->getUidId($messageId);
     }
     if (!Zend_Locale::isLocale($locale, true, false)) {
         if (!Zend_Locale::isLocale($locale, false, false)) {
             // language does not exist, return original string
             //TODO: save untranslated language
             $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 = Zend_Translate_Plural::getPlural($number, $plocale);
             if (!isset($plural[$rule])) {
                 $rule = 0;
             }
             return $plural[$rule];
         }
         $locale = new Zend_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 = Zend_Translate_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 = Zend_Translate_Plural::getPlural($number, $locale);
                 if (isset($this->_translate[$locale][$plural[0]][$rule])) {
                     $this->_routed = array();
                     return $this->_translate[$locale][$plural[0]][$rule];
                 }
             }
         }
     }
     //TODO: log unexistant message
     $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 = Zend_Translate_Plural::getPlural($number, $plocale);
     if (!isset($plural[$rule])) {
         $rule = 0;
     }
     return $plural[$rule];
 }
Example #5
0
 /**
  * Get translated message
  *
  * @param string  $messageId
  * @param string  $locale
  * @param string  $module
  * @param string  $plural
  * @param integer $number
  * @return string|false
  */
 protected function _getTranslatedMessage($messageId, $locale, $module, $plural, $number)
 {
     if ($plural === null) {
         $this->_routed = array();
         if (isset($this->_translate[$locale][$module][$messageId])) {
             return $this->_translate[$locale][$module][$messageId];
         }
         return $this->_translate[$locale]['default'][$messageId];
     }
     $rule = Zend_Translate_Plural::getPlural($number, $locale);
     if (isset($this->_translate[$locale][$module][$plural[0]][$rule])) {
         $this->_routed = array();
         return $this->_translate[$locale][$module][$plural[0]][$rule];
     }
     if (isset($this->_translate[$locale]['default'][$plural[0]][$rule])) {
         $this->_routed = array();
         return $this->_translate[$locale]['default'][$plural[0]][$rule];
     }
     return false;
 }
Example #6
0
 /**
  * Translates the given string
  * Returns the translation
  *
  * If the second parameter is a string then a plural translation will be done
  * If the second parameter is a locale then a singular translation will be done
  * When the pural translation is used the plural message and the plural number must be given
  *
  * @param  string              $messageId  Translation string
  * @param  string              $messagePlural  OPTIONAL Translation string for Plural
  * @param  integer             $plural     OPTIONAL Plural number
  *                                         if suppressed $locale can be used as second parameter 
  * @param  string|Zend_Locale  $locale     OPTIONAL Locale/Language to use, identical with locale identifier,
  *                                         see Zend_Locale for more information
  * @return string
  */
 public function translate($messageId, $messagePlural = null, $plural = null, $locale = null)
 {
     if ($messagePlural !== null and !is_int($plural)) {
         $locale = $messagePlural;
         $messagePlural = null;
         $plural = null;
     }
     if ($locale === null) {
         $locale = $this->_options['locale'];
     } else {
         if (!($locale = Zend_Locale::isLocale($locale))) {
             // language does not exist, return original string
             return $messageId;
         }
     }
     if (is_int($plural) and $plural > 0) {
         require_once 'Zend/Translate/Plural.php';
         $plural = Zend_Translate_Plural::getPlural($plural, $locale);
         if (array_key_exists($locale, $this->_translate) and array_key_exists($messageId, $this->_translate[$locale]) and array_key_exists($plural, $this->_translate[$locale][$messageId])) {
             // return original translation
             return $this->_translate[$locale][$messageId][$plural];
         } else {
             if (strlen($locale) > 3) {
                 // faster than creating a new locale and separate the leading part
                 $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
                 if (array_key_exists($locale, $this->_translate) and array_key_exists($messageId, $this->_translate[$locale]) and array_key_exists($plural, $this->_translate[$locale][$messageId])) {
                     // return regionless translation (en_US -> en)
                     return $this->_translate[$locale][$messageId][$plural];
                 }
             }
         }
         return $messageId[$plural];
     }
     if (array_key_exists($locale, $this->_translate) and array_key_exists($messageId, $this->_translate[$locale])) {
         // return original translation
         return $this->_translate[$locale][$messageId];
     } else {
         if (strlen($locale) > 3) {
             // faster than creating a new locale and separate the leading part
             $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
             if (array_key_exists($locale, $this->_translate) and array_key_exists($messageId, $this->_translate[$locale])) {
                 // return regionless translation (en_US -> en)
                 return $this->_translate[$locale][$messageId];
             }
         }
     }
     // no translation found, return original
     return $messageId;
 }
Example #7
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    (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];
         }
     }
     // CUSTOM ischommer: Skip locale checks, too computationally expensive.
     // Assume correct locale value is passed in.
     // if (!Zend_Locale::isLocale($locale, true, false)) {
     //     if (!Zend_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 = Zend_Translate_Plural::getPlural($number, $plocale);
     //         if (!isset($plural[$rule])) {
     //             $rule = 0;
     //         }
     //         return $plural[$rule];
     //     }
     //     $locale = new Zend_Locale($locale);
     // }
     // CUSTOM END
     $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 = Zend_Translate_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 = Zend_Translate_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 = Zend_Translate_Plural::getPlural($number, $plocale);
     if (!isset($plural[$rule])) {
         $rule = 0;
     }
     return $plural[$rule];
 }