public function indexAction()
 {
     if ($this->getRequest()->getParam('garbage')) {
         $this->redirect('');
     }
     $translator = Zend_Registry::get('Zend_Translate');
     if (!$this->getRequest()->isPost()) {
         if (Zend_Session::sessionExists()) {
             $namespace = $this->_session->getNamespace();
             if (isset($_SESSION[$namespace])) {
                 unset($_SESSION[$namespace]);
             }
             $translator->setLocale('en');
             Zend_Registry::set('Zend_Translate', $translator);
             Zend_Session::regenerateId();
         }
     } else {
         $lang = $this->getRequest()->getParam('lang');
         if ($lang && Zend_Locale::isLocale($lang)) {
             $this->_session->locale->setLocale($lang);
             if ($translator->getLocale() !== $lang) {
                 $translator->setLocale($lang);
                 Zend_Registry::set('Zend_Translate', $translator);
             }
             $this->_session->nextStep = 1;
         }
         if ($this->_session->nextStep !== null) {
             return $this->forward('step' . $this->_session->nextStep);
         }
     }
     $this->forward('step1');
 }
示例#2
0
 /**
  * Translate a message
  * You can give multiple params or an array of params.
  * If you want to output another locale just set it as last single parameter
  * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
  * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale); 
  *
  * @param string           $messageid
  * @return string  Translated message
  */
 public function translate($messageid = null)
 {
     if ($this->translate === null) {
         require_once 'Zend/Registry.php';
         if (!Zend_Registry::isRegistered('Zend_Translate')) {
             if (empty($messageid)) {
                 return $this;
             } else {
                 return $messageid;
             }
         } else {
             $this->translate = Zend_Registry::get('Zend_Translate');
         }
     }
     $options = func_get_args();
     array_shift($options);
     $count = count($options);
     $locale = null;
     if ($count > 0) {
         if (Zend_Locale::isLocale($options[$count - 1])) {
             $locale = array_pop($options);
         }
     }
     if (count($options) == 1 and is_array($options[0])) {
         $options = $options[0];
     }
     $message = $this->translate->translate($messageid, $locale);
     return vsprintf($message, $options);
 }
示例#3
0
 public function routeShutdown(Zend_Controller_Request_Abstract $request)
 {
     $translate = Zend_Registry::get('Zend_Translate');
     $locale = Zend_Registry::get('Zend_Locale');
     $languages = Zend_Registry::get('languages');
     $front = Zend_Controller_Front::getInstance();
     $router = $front->getRouter();
     $view = Zend_Layout::getMvcInstance()->getView();
     //$view->route = '';
     if ($language = strtolower($request->getParam('language'))) {
         if ($language != $locale->getLanguage($translate->getLocale()) && Zend_Locale::isLocale($language)) {
             try {
                 $translate->addTranslation(APPLICATION_PATH . '/../data/locales/' . $language . '.php', $language);
                 if ($translate->isAvailable($language)) {
                     $locale->setLocale($languages[$language]);
                     $router->setGlobalParam('language', $language);
                     Zend_Registry::set('Zend_Translate', $translate);
                     Zend_Registry::set('Zend_Locale', $locale);
                     //$view->route .= '_language';
                 }
             } catch (Exception $e) {
             }
         }
     }
     $view->route = $router->getCurrentRouteName();
     /*        
     echo $router->getCurrentRouteName();
     echo '
       <h3>Request Parameters:</h3>
       <pre> ' . var_export($request->getParams(), true) . '
       </pre>
     '; 
     //die();
     */
 }
示例#4
0
 /**
  * Translate a message
  * You can give multiple params or an array of params.
  * If you want to output another locale just set it as last single parameter
  * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
  * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
  *
  * @param  string $messageid Id of the message to be translated
  * @return string|Zend_View_Helper_Translate Translated message
  */
 public function translate($messageid = null)
 {
     if ($messageid === null) {
         return $this;
     }
     $translate = $this->getTranslator();
     $options = func_get_args();
     array_shift($options);
     $count = count($options);
     $locale = null;
     if ($count > 0) {
         if (Zend_Locale::isLocale($options[$count - 1], null, false) !== false) {
             $locale = array_pop($options);
         }
     }
     if (count($options) === 1 and is_array($options[0]) === true) {
         $options = $options[0];
     }
     if ($translate !== null) {
         $messageid = $translate->translate($messageid, $locale);
     }
     if (count($options) === 0) {
         return $messageid;
     }
     return vsprintf($messageid, $options);
 }
示例#5
0
 protected function startLocale()
 {
     require_once "Zend/Translate.php";
     // silenciando strict até arrumar zend_locale
     date_default_timezone_set("America/Sao_Paulo");
     $i18n = new Zend_Translate('gettext', $this->config->system->path->base . '/lang/pt_BR.mo', 'pt_BR');
     Zend_Registry::set('i18n', $i18n);
     $translation_files = $this->config->system->path->base . "/lang/";
     foreach (scandir($translation_files) as $filename) {
         // Todos os arquivos .php devem ser classes de descrição de modulos
         if (preg_match("/.*\\.mo\$/", $filename)) {
             $translation_id = basename($filename, '.mo');
             if ($translation_id != "pt_BR") {
                 $i18n->addTranslation($translation_files . "/{$filename}", $translation_id);
             }
         }
     }
     require_once "Zend/Locale.php";
     if (Zend_Locale::isLocale($this->config->system->locale)) {
         $locale = $this->config->system->locale;
     } else {
         $locale = "pt_BR";
     }
     Zend_Registry::set('Zend_Locale', new Zend_Locale($locale));
     Zend_Locale::setDefault($locale);
     Zend_Locale_Format::setOptions(array("locale" => $locale));
     $i18n->setLocale($locale);
     Zend_Registry::set("Zend_Translate", $i18n);
     $zend_validate_translator = new Zend_Translate_Adapter_Array($this->config->system->path->base . "/lang/Zend_Validate/{$locale}/Zend_Validate.php", $locale);
     Zend_Validate_Abstract::setDefaultTranslator($zend_validate_translator);
 }
示例#6
0
文件: Number.php 项目: adnanali/munch
 /**
  * Zend_Measure_Abstract is an abstract class for the different measurement types
  *
  * @param  $value  mixed  - Value as string, integer, real or float
  * @param  $type   type   - OPTIONAL a Zend_Measure_Area Type
  * @param  $locale locale - OPTIONAL a Zend_Locale Type
  * @throws Zend_Measure_Exception
  */
 public function __construct($value, $type, $locale = null)
 {
     if (Zend_Locale::isLocale($type)) {
         $locale = $type;
         $type = null;
     }
     if ($locale === null) {
         $locale = new Zend_Locale();
     }
     if ($locale instanceof Zend_Locale) {
         $locale = $locale->toString();
     }
     if (!($this->_Locale = Zend_Locale::isLocale($locale, true))) {
         require_once 'Zend/Measure/Exception.php';
         throw new Zend_Measure_Exception("Language ({$locale}) is unknown");
     }
     $this->_Locale = $locale;
     if ($type === null) {
         $type = $this->_UNITS['STANDARD'];
     }
     if (!array_key_exists($type, $this->_UNITS)) {
         require_once 'Zend/Measure/Exception.php';
         throw new Zend_Measure_Exception("Type ({$type}) is unknown");
     }
     $this->setValue($value, $type, $this->_Locale);
 }
示例#7
0
 /**
  * Translate a message
  * You can give multiple params or an array of params.
  * If you want to output another locale just set it as last single parameter
  * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
  * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
  *
  * @param  string $messageid Id of the message to be translated
  * @return string|Zend_View_Helper_Translate Translated message
  */
 public function t($messageid)
 {
     if ($messageid === null) {
         return $this;
     }
     if (Zend_Registry::isRegistered('Zend_Translate')) {
         $translate = Zend_Registry::get('Zend_Translate');
     } else {
         Zend_Registry::get('Bootstrap')->bootstrap('Translator');
     }
     $options = func_get_args();
     array_shift($options);
     $count = count($options);
     $locale = null;
     if ($count > 0) {
         if (Zend_Locale::isLocale($options[$count - 1], null, false) !== false) {
             $locale = array_pop($options);
         }
     }
     if (count($options) === 1 and is_array($options[0]) === true) {
         $options = $options[0];
     }
     if ($translate !== null) {
         $messageid = $translate->translate($messageid, $locale);
     }
     if (count($options) === 0) {
         return $messageid;
     }
     return vsprintf($messageid, $options);
 }
示例#8
0
 /**
  * Translate a message
  * You can give an array of params.
  * If you want to output another locale just set it as last single parameter
  * Example 1: translate('Some text', $locale);
  * Example 2: translate('%key1% + %key2%', array('key1' => $value1, 'key2' => $value2), $locale);
  *
  * @param  string $messageid Id of the message to be translated
  * @param  array  $values Values for translation placeholders as assoc array
  * @return string|Zend_View_Helper_Translate Translated message
  */
 public function translate($messageid = null, $values = array())
 {
     if ($messageid === null) {
         return $this;
     }
     $translate = $this->getTranslator();
     $options = func_get_args();
     array_shift($options);
     $count = count($options);
     $locale = null;
     if ($count > 0) {
         if (Zend_Locale::isLocale($options[$count - 1], null, false) !== false) {
             $locale = array_pop($options);
         }
     }
     if ($translate !== null) {
         $messageid = $translate->translate($messageid, $locale);
     }
     if (count($values) === 0) {
         return $messageid;
     }
     if (count($values)) {
         foreach ($values as $key => $value) {
             $messageid = str_replace('%' . $key . '%', $value, $messageid);
         }
     }
     return $messageid;
 }
示例#9
0
 /**
  * Tests if the given language is really a language
  */
 public function testIsLocale()
 {
     foreach ($this->_languages as $lang) {
         if (!Zend_Locale::isLocale($lang, true, false)) {
             $this->fail("Language directory '{$lang}' not a valid locale");
         }
     }
 }
示例#10
0
 /**
  * Constructor
  * @param array $options
  */
 public function __construct($options = array())
 {
     if (Zend_Locale::isLocale($options)) {
         $this->_locale = $options;
     } else {
         if (Zend_Registry::isRegistered('Zend_Locale')) {
             $this->_locale = Zend_Registry::get('Zend_Locale');
         }
     }
 }
示例#11
0
 /**
  * returns list of all available translations
  * 
  * NOTE available are those, having a Tinebase translation
  * 
  * @return array list of all available translation
  *
  * @todo add test
  */
 public static function getAvailableTranslations()
 {
     $availableTranslations = array();
     // look for po files in Tinebase
     $officialTranslationsDir = dirname(__FILE__) . '/translations';
     foreach (scandir($officialTranslationsDir) as $poFile) {
         list($localestring, $suffix) = explode('.', $poFile);
         if ($suffix == 'po') {
             $availableTranslations[$localestring] = array('path' => "{$officialTranslationsDir}/{$poFile}");
         }
     }
     // lookup/merge custom translations
     if (Tinebase_Config::isReady() === TRUE) {
         $customTranslationsDir = Tinebase_Config::getInstance()->translations;
         if ($customTranslationsDir) {
             foreach ((array) scandir($customTranslationsDir) as $dir) {
                 $poFile = "{$customTranslationsDir}/{$dir}/Tinebase/translations/{$dir}.po";
                 if (is_readable($poFile)) {
                     $availableTranslations[$dir] = array('path' => $poFile);
                 }
             }
         }
     }
     // compute information
     foreach ($availableTranslations as $localestring => $info) {
         if (!Zend_Locale::isLocale($localestring, TRUE, FALSE)) {
             $logger = Tinebase_Core::getLogger();
             if ($logger) {
                 $logger->WARN(__METHOD__ . '::' . __LINE__ . " {$localestring} is not supported, removing translation form list");
             }
             unset($availableTranslations[$localestring]);
             continue;
         }
         // fetch header grep for X-Poedit-Language, X-Poedit-Country
         $fh = fopen($info['path'], 'r');
         $header = fread($fh, 1024);
         fclose($fh);
         preg_match('/X-Tine20-Language: (.+)(?:\\\\n?)(?:"?)/', $header, $language);
         preg_match('/X-Tine20-Country: (.+)(?:\\\\n?)(?:"?)/', $header, $region);
         $locale = new Zend_Locale($localestring);
         $availableTranslations[$localestring]['locale'] = $localestring;
         $availableTranslations[$localestring]['language'] = isset($language[1]) ? $language[1] : Zend_Locale::getTranslation($locale->getLanguage(), 'language', $locale);
         $availableTranslations[$localestring]['region'] = isset($region[1]) ? $region[1] : Zend_Locale::getTranslation($locale->getRegion(), 'country', $locale);
     }
     ksort($availableTranslations);
     return $availableTranslations;
 }
示例#12
0
 /**
  * Referência:
  * http://www.codeforest.net/multilanguage-support-in-zend-framework
  */
 protected function _initTranslate()
 {
     $locale = new Zend_Locale();
     if (!Zend_Locale::isLocale($locale, TRUE, FALSE)) {
         if (!Zend_Locale::isLocale($locale, FALSE, FALSE)) {
             throw new Zend_Locale_Exception("The locale '{$locale}' is no known locale");
         }
         $locale = new Zend_Locale($locale);
     }
     //$locale = "pt_BR";
     $translatorArray = new Zend_Translate(array('adapter' => 'array', 'content' => APPLICATION_PATH . '/../resources/languages', 'locale' => $locale, 'scan' => Zend_Translate::LOCALE_DIRECTORY));
     $translate = new Zend_Translate('gettext', APPLICATION_PATH . "/langs/", $locale, array('scan' => Zend_Translate::LOCALE_DIRECTORY));
     $translate->addTranslation($translatorArray);
     $registry = Zend_Registry::getInstance();
     $registry->set('Zend_Translate', $translate);
     Zend_Validate_Abstract::setDefaultTranslator($translate);
     Zend_Form::setDefaultTranslator($translate);
 }
示例#13
0
 public function setLanguage($selectedLanguage)
 {
     $sessionHelper = Zend_Controller_Action_HelperBroker::getExistingHelper('session');
     $cacheHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('cache');
     $locale = $sessionHelper->locale;
     $newLocale = Zend_Locale::getLocaleToTerritory($selectedLanguage);
     if ($newLocale !== null) {
         $locale->setLocale($newLocale);
     } else {
         if (Zend_Locale::isLocale($selectedLanguage)) {
             $locale->setLocale($selectedLanguage);
         }
     }
     $sessionHelper->locale = $locale;
     Zend_Registry::get('Zend_Translate')->setLocale($locale);
     $cacheHelper->clean(false, false, array('locale', 'language'));
     return $locale->getLanguage();
 }
示例#14
0
 /**
  * Output a formatted currency
  *
  * @param  integer|float                    $value    Currency value to output
  * @param  string|Zend_Locale|Zend_Currency $currency OPTIONAL Currency to use for this call
  * @return string Formatted currency
  */
 public function currency($value = null, $currency = null)
 {
     if ($value === null) {
         return $this;
     }
     if (is_string($currency) || $currency instanceof Zend_Locale) {
         if (Zend_Locale::isLocale($currency)) {
             $currency = array('locale' => $currency);
         }
     }
     if (is_string($currency)) {
         $currency = array('currency' => $currency);
     }
     if (is_array($currency)) {
         return $this->_currency->toCurrency($value, $currency);
     }
     return $this->_currency->toCurrency($value);
 }
示例#15
0
 /**
  * Sets a new adapter
  *
  * @param string $adapter - adapter to use
  * @param mixed  $options - Adapter options
  * @param mixed  $locale  - OPTIONAL locale to use
  * @return timestamp
  */
 public function setAdapter($adapter, $options, $locale = null)
 {
     if (!($locale = Zend_Locale::isLocale($locale))) {
         throw new Zend_Translate_Exception("language ({$locale}) is a unknown language", $locale);
     }
     switch (strtolower($adapter)) {
         case 'array':
             /** Zend_Translate_Adapter_Array */
             require_once 'Zend/Translate/Adapter/Array.php';
             $this->_adapter = new Zend_Translate_Adapter_Array($options, $locale);
             break;
         case 'cvs':
             throw new Zend_Translate_Exception('not supported for now');
             break;
         case 'gettext':
             /** Zend_Translate_Adapter_Gettext */
             require_once 'Zend/Translate/Adapter/Gettext.php';
             $this->_adapter = new Zend_Translate_Adapter_Gettext($options, $locale);
             break;
         case 'qt':
             throw new Zend_Translate_Exception('not supported for now');
             break;
         case 'sql':
             throw new Zend_Translate_Exception('not supported for now');
             break;
         case 'tbx':
             throw new Zend_Translate_Exception('not supported for now');
             break;
         case 'tmx':
             throw new Zend_Translate_Exception('not supported for now');
             break;
         case 'xliff':
             throw new Zend_Translate_Exception('not supported for now');
             break;
         case 'xmltm':
             throw new Zend_Translate_Exception('not supported for now');
             break;
         default:
             throw new Zend_Translate_Exception('no adapter selected');
             break;
     }
 }
示例#16
0
 /**
  * finds installed languages
  *
  * @static
  * @return array
  */
 public static function getLanguages()
 {
     $languages = array();
     $languageDirs = array(PIMCORE_PATH . "/config/texts/", PIMCORE_CONFIGURATION_DIRECTORY . "/texts/");
     foreach ($languageDirs as $filesDir) {
         if (is_dir($filesDir)) {
             $files = scandir($filesDir);
             foreach ($files as $file) {
                 if (is_file($filesDir . $file)) {
                     $parts = explode(".", $file);
                     if ($parts[1] == "json") {
                         if (\Zend_Locale::isLocale($parts[0])) {
                             $languages[] = $parts[0];
                         }
                     }
                 }
             }
         }
     }
     return $languages;
 }
示例#17
0
文件: Admin.php 项目: ngocanh/pimcore
 /**
  * finds installed languages
  *
  * @static
  * @return array
  */
 public static function getLanguages()
 {
     $languages = array();
     $languageDirs = array(PIMCORE_PATH . "/config/texts/", PIMCORE_WEBSITE_PATH . "/var/config/texts/");
     foreach ($languageDirs as $filesDir) {
         if (is_dir($filesDir)) {
             $files = scandir($filesDir);
             foreach ($files as $file) {
                 if (is_file($filesDir . $file)) {
                     $parts = explode(".", $file);
                     if ($parts[1] == "csv") {
                         if (Zend_Locale::isLocale($parts[0])) {
                             $languages[] = $parts[0];
                         }
                     }
                 }
             }
         }
     }
     return $languages;
 }
示例#18
0
 /**
  * Class constructor
  *
  * @return	void
  */
 public function __construct()
 {
     parent::__construct();
     log_message('debug', 'XtraUpload Zend Locale Class Initialized');
     try {
         $this->_default_locale = new Zend_Locale();
     } catch (Exception $e) {
         $this->_default_locale = new Zend_Locale('en_US');
     }
     if (!Zend_Locale::isLocale($this->_default_locale, true, false)) {
         if (!Zend_Locale::isLocale($this->_default_locale, false, false)) {
             throw new Zend_Locale_Exception("The locale '{$locale}' is no known locale");
         }
     }
     $locale = APPPATH . "/language/{$this->_default_locale}/xtraupload.mo";
     if (!is_file($locale)) {
         $this->_default_locale->setLocale('en_US');
     }
     $this->_default_locale = new Zend_Locale($this->_default_locale);
     $this->_translate = new Zend_Translate('gettext', APPPATH . "/language/{$this->_default_locale}/xtraupload.mo", $this->_default_locale->getLanguage());
     $this->_dbtypes = array('db_invalid_connection_str' => $this->lang('Unable to determine the database settings based on the connection string you submitted.'), 'db_unable_to_connect' => $this->lang('Unable to connect to your database server using the provided settings.'), 'db_invalid_query' => $this->lang('The query you submitted is not valid.'), 'db_must_set_table' => $this->lang('You must set the database table to be used with your query.'), 'db_must_use_set' => $this->lang('You must use the "set" method to update an entry.'), 'db_must_use_index' => $this->lang('You must specify an index to match on for batch updates.'), 'db_batch_missing_index' => $this->lang('One or more rows submitted for batch updating is missing the specified index.'), 'db_must_use_where' => $this->lang('Updates are not allowed unless they contain a "where" clause.'), 'db_del_must_use_where' => $this->lang('Deletes are not allowed unless they contain a "where" or "like" clause.'), 'db_field_param_missing' => $this->lang('To fetch fields requires the name of the table as a parameter.'), 'db_unsupported_function' => $this->lang('This feature is not available for the database you are using.'), 'db_transaction_failure' => $this->lang('Transaction failure: Rollback performed.'), 'db_unable_to_drop' => $this->lang('Unable to drop the specified database.'), 'db_unsuported_feature' => $this->lang('Unsupported feature of the database platform you are using.'), 'db_unsuported_compression' => $this->lang('The file compression format you chose is not supported by your server.'), 'db_filepath_error' => $this->lang('Unable to write data to the file path you have submitted.'), 'db_invalid_cache_path' => $this->lang('The cache path you submitted is not valid or writable.'), 'db_table_name_required' => $this->lang('A table name is required for that operation.'), 'db_column_name_required' => $this->lang('A column name is required for that operation.'), 'db_column_definition_required' => $this->lang('A column definition is required for that operation.'), 'db_error_heading' => $this->lang('A Database Error Occurred'));
 }
 public function init()
 {
     parent::init();
     if ($this->_getParam("language") && \Zend_Locale::isLocale($this->_getParam("language"))) {
         $locale = new \Zend_Locale($this->_getParam("language"));
         \Zend_Registry::set("Zend_Locale", $locale);
     }
     if (\Zend_Auth::getInstance()->hasIdentity()) {
         $ident = \Zend_Auth::getInstance()->getIdentity();
         $member = User::getById($ident['oid']);
         $this->currentMember = $member;
         $this->view->currentMember = $member;
     }
     if (\Zend_Registry::isRegistered("Zend_Locale")) {
         $locale = \Zend_Registry::get("Zend_Locale");
     } else {
         $locale = new \Zend_Locale("en");
         \Zend_Registry::set("Zend_Locale", $locale);
     }
     $this->view->language = (string) $locale;
     $this->language = (string) $locale;
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $source = "http://unicode.org/repos/cldr/trunk/common/supplemental/supplementalData.xml";
     $data = file_get_contents($source);
     $xml = simplexml_load_string($data, null, LIBXML_NOCDATA);
     $languageRawData = [];
     foreach ($xml->territoryInfo->territory as $territory) {
         foreach ($territory->languagePopulation as $language) {
             $languageCode = (string) $language["type"];
             if (\Zend_Locale::isLocale($languageCode)) {
                 $populationAbsolute = $territory["population"] * $language["populationPercent"] / 100;
                 if (!isset($languageRawData[$languageCode])) {
                     $languageRawData[$languageCode] = [];
                 }
                 if (\Zend_Locale::isLocale($languageCode . "_" . $territory["type"], true)) {
                     $languageRawData[$languageCode][] = ["country" => (string) $territory["type"], "population" => $populationAbsolute];
                 }
             }
         }
     }
     $finalData = [];
     foreach ($languageRawData as $languageCode => $rawLanguage) {
         usort($rawLanguage, function ($a, $b) {
             if ($a["population"] == $b["population"]) {
                 return 0;
             }
             return $a["population"] > $b["population"] ? -1 : 1;
         });
         $finalData[$languageCode] = [];
         foreach ($rawLanguage as $territory) {
             $finalData[$languageCode][] = $territory["country"];
         }
     }
     $contents = to_php_data_file_format($finalData);
     $dataFile = PIMCORE_PATH . "/config/data/cldr-language-territory-mapping.php";
     File::putPhpFile($dataFile, $contents);
     $this->output->writeln("Updated mappings in " . $dataFile);
 }
示例#21
0
 /**
  * Translate a message
  * You can give multiple params or an array of params.
  * If you want to output another locale just set it as last single parameter
  * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
  * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
  *
  * @param string           $messageid
  * @return string  Translated message
  */
 public function translate($messageid = null)
 {
     if (null === $messageid) {
         return $this;
     }
     if (null === ($translate = $this->getTranslator())) {
         return $messageid;
     }
     $options = func_get_args();
     array_shift($options);
     $count = count($options);
     $locale = null;
     if ($count > 0) {
         if (Zend_Locale::isLocale($options[$count - 1])) {
             $locale = array_pop($options);
         }
     }
     if (count($options) == 1 and is_array($options[0])) {
         $options = $options[0];
     }
     $message = $translate->translate($messageid, $locale);
     return vsprintf($message, $options);
 }
示例#22
0
 /**
  * Internal method for checking the options array
  *
  * @param  array $options Options to check
  * @throws Zend_Currency_Exception On unknown position
  * @throws Zend_Currency_Exception On unknown locale
  * @throws Zend_Currency_Exception On unknown display
  * @throws Zend_Currency_Exception On precision not between -1 and 30
  * @throws Zend_Currency_Exception On problem with script conversion
  * @throws Zend_Currency_Exception On unknown options
  * @return array
  */
 protected function _checkOptions(array $options = array())
 {
     if (count($options) === 0) {
         return $this->_options;
     }
     foreach ($options as $name => $value) {
         $name = strtolower($name);
         if ($name !== 'format') {
             if (gettype($value) === 'string') {
                 $value = strtolower($value);
             }
         }
         switch ($name) {
             case 'position':
                 if ($value !== self::STANDARD and $value !== self::RIGHT and $value !== self::LEFT) {
                     require_once 'Zend/Currency/Exception.php';
                     throw new Zend_Currency_Exception("Unknown position '" . $value . "'");
                 }
                 break;
             case 'format':
                 if (empty($value) === false and Zend_Locale::isLocale($value, null, false) === false) {
                     if (!is_string($value) || strpos($value, '0') === false) {
                         require_once 'Zend/Currency/Exception.php';
                         throw new Zend_Currency_Exception("'" . (gettype($value) === 'object' ? get_class($value) : $value) . "' is no format token");
                     }
                 }
                 break;
             case 'display':
                 if (is_numeric($value) and $value !== self::NO_SYMBOL and $value !== self::USE_SYMBOL and $value !== self::USE_SHORTNAME and $value !== self::USE_NAME) {
                     require_once 'Zend/Currency/Exception.php';
                     throw new Zend_Currency_Exception("Unknown display '{$value}'");
                 }
                 break;
             case 'precision':
                 if ($value === null) {
                     $value = -1;
                 }
                 if ($value < -1 or $value > 30) {
                     require_once 'Zend/Currency/Exception.php';
                     throw new Zend_Currency_Exception("'{$value}' precision has to be between -1 and 30.");
                 }
                 break;
             case 'script':
                 try {
                     Zend_Locale_Format::convertNumerals(0, $options['script']);
                 } catch (Zend_Locale_Exception $e) {
                     require_once 'Zend/Currency/Exception.php';
                     throw new Zend_Currency_Exception($e->getMessage());
                 }
                 break;
             default:
                 break;
         }
     }
     return $options;
 }
示例#23
0
文件: Locale.php 项目: bizanto/Hooked
 /**
  * Finds the proper locale based on the input
  * Checks if it exists, degrades it when necessary
  * Detects registry locale and when all fails tries to detect a automatic locale
  * Returns the found locale as string
  *
  * @param string $locale
  * @throws Zend_Locale_Exception When the given locale is no locale or the autodetection fails
  * @return string
  */
 public static function findLocale($locale = null)
 {
     if ($locale === null) {
         require_once 'Zend/Registry.php';
         if (Zend_Registry::isRegistered('Zend_Locale')) {
             $locale = Zend_Registry::get('Zend_Locale');
         }
     }
     if ($locale === null) {
         $locale = new Zend_Locale();
     }
     if (!Zend_Locale::isLocale($locale, true, false)) {
         if (!Zend_Locale::isLocale($locale, false, false)) {
             $locale = Zend_Locale::getLocaleToTerritory($locale);
             if (empty($locale)) {
                 require_once 'Zend/Locale/Exception.php';
                 throw new Zend_Locale_Exception("The locale '{$locale}' is no known locale");
             }
         } else {
             $locale = new Zend_Locale($locale);
         }
     }
     $locale = self::_prepareLocale($locale);
     return $locale;
 }
示例#24
0
 /**
  * Checks if a string is translated within the source or not
  * returns boolean
  *
  * @param  string             $messageId Translation string
  * @param  boolean            $original  (optional) Allow translation only for original language
  *                                       when true, a translation for 'en_US' would give false when it can
  *                                       be translated with 'en' only
  * @param  string|Zend_Locale $locale    (optional) Locale/Language to use, identical with locale identifier,
  *                                       see Zend_Locale for more information
  * @return boolean
  */
 public function isTranslated($messageId, $original = false, $locale = null)
 {
     if ($original !== false and $original !== true) {
         $locale = $original;
         $original = false;
     }
     if ($locale === null) {
         $locale = $this->_options['locale'];
     }
     if (!Zend_Locale::isLocale($locale, true, false)) {
         if (!Zend_Locale::isLocale($locale, false, false)) {
             // language does not exist, return original string
             return false;
         }
         $locale = new Zend_Locale($locale);
     }
     $locale = (string) $locale;
     if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
         // return original translation
         return true;
     } else {
         if (strlen($locale) != 2 and $original === false) {
             // 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)
                 return true;
             }
         }
     }
     // No translation found, return original
     return false;
 }
 /**
  * Checks if the given date is a real date or datepart.
  * Returns false if a expected datepart is missing or a datepart exceeds its possible border.
  * But the check will only be done for the expected dateparts which are given by format.
  * If no format is given the standard dateformat for the actual locale is used.
  * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
  *
  * @param  string|array|Zend_Date $date   Date to parse for correctness
  * @param  string                 $format (Optional) Format for parsing the date string
  * @param  string|Zend_Locale     $locale (Optional) Locale for parsing date parts
  * @return boolean                True when all date parts are correct
  */
 public static function isDate($date, $format = null, $locale = null)
 {
     if (!is_string($date) && !is_numeric($date) && !$date instanceof Zend_Date && !is_array($date)) {
         return false;
     }
     if ($format !== null && $format != 'ee' && $format != 'ss' && $format != 'GG' && $format != 'MM' && $format != 'EE' && $format != 'TT' && Zend_Locale::isLocale($format, null, false)) {
         $locale = $format;
         $format = null;
     }
     $locale = Zend_Locale::findLocale($locale);
     if ($format === null) {
         $format = Zend_Locale_Format::getDateFormat($locale);
     } else {
         if (self::$_options['format_type'] == 'php' && !defined($format)) {
             $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
         }
     }
     $format = self::_getLocalizedToken($format, $locale);
     if (!is_array($date)) {
         try {
             $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'date_format' => $format, 'format_type' => 'iso', 'fix_date' => false));
         } catch (Zend_Locale_Exception $e) {
             // Date can not be parsed
             return false;
         }
     } else {
         $parsed = $date;
     }
     if ((strpos($format, 'Y') !== false or strpos($format, 'y') !== false) and !isset($parsed['year'])) {
         // Year expected but not found
         return false;
     }
     if (strpos($format, 'M') !== false and !isset($parsed['month'])) {
         // Month expected but not found
         return false;
     }
     if (strpos($format, 'd') !== false and !isset($parsed['day'])) {
         // Day expected but not found
         return false;
     }
     if ((strpos($format, 'H') !== false or strpos($format, 'h') !== false) and !isset($parsed['hour'])) {
         // Hour expected but not found
         return false;
     }
     if (strpos($format, 'm') !== false and !isset($parsed['minute'])) {
         // Minute expected but not found
         return false;
     }
     if (strpos($format, 's') !== false and !isset($parsed['second'])) {
         // Second expected  but not found
         return false;
     }
     // Set not given dateparts
     if (isset($parsed['hour']) === false) {
         $parsed['hour'] = 12;
     }
     if (isset($parsed['minute']) === false) {
         $parsed['minute'] = 0;
     }
     if (isset($parsed['second']) === false) {
         $parsed['second'] = 0;
     }
     if (isset($parsed['month']) === false) {
         $parsed['month'] = 1;
     }
     if (isset($parsed['day']) === false) {
         $parsed['day'] = 1;
     }
     if (isset($parsed['year']) === false) {
         $parsed['year'] = 1970;
     }
     if (self::isYearLeapYear($parsed['year'])) {
         $parsed['year'] = 1972;
     } else {
         $parsed['year'] = 1971;
     }
     $date = new self($parsed, null, $locale);
     $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $parsed['month'], $parsed['day'], $parsed['year']);
     if ($parsed['year'] != $date->date('Y', $timestamp)) {
         // Given year differs from parsed year
         return false;
     }
     if ($parsed['month'] != $date->date('n', $timestamp)) {
         // Given month differs from parsed month
         return false;
     }
     if ($parsed['day'] != $date->date('j', $timestamp)) {
         // Given day differs from parsed day
         return false;
     }
     if ($parsed['hour'] != $date->date('G', $timestamp)) {
         // Given hour differs from parsed hour
         return false;
     }
     if ($parsed['minute'] != $date->date('i', $timestamp)) {
         // Given minute differs from parsed minute
         return false;
     }
     if ($parsed['second'] != $date->date('s', $timestamp)) {
         // Given second differs from parsed second
         return false;
     }
     return true;
 }
 /**
  * Set the locales which are accepted
  *
  * @param  string|array|Zend_Locale $locale
  * @throws Zend_Filter_Exception
  * @return Zend_Filter_Boolean
  */
 public function setLocale($locale = null)
 {
     if (is_string($locale)) {
         $locale = array($locale);
     } elseif ($locale instanceof Zend_Locale) {
         $locale = array($locale->toString());
     } elseif (!is_array($locale)) {
         require_once 'Zend/Filter/Exception.php';
         throw new Zend_Filter_Exception('Locale has to be string, array or an instance of Zend_Locale');
     }
     require_once 'Zend/Locale.php';
     foreach ($locale as $single) {
         if (!Zend_Locale::isLocale($single)) {
             require_once 'Zend/Filter/Exception.php';
             throw new Zend_Filter_Exception("Unknown locale '{$single}'");
         }
     }
     $this->_locale = $locale;
     return $this;
 }
示例#27
0
 /**
  * Checks if the given date is a real date or datepart.
  * Returns false if a expected datepart is missing or a datepart exceeds its possible border.
  * But the check will only be done for the expected dateparts which are given by format.
  * If no format is given the standard dateformat for the actual locale is used.
  * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
  *
  * @param  string             $date   Date to parse for correctness
  * @param  string             $format (Optional) Format for parsing the date string
  * @param  string|Zend_Locale $locale (Optional) Locale for parsing date parts
  * @return boolean            True when all date parts are correct
  */
 public static function isDate($date, $format = null, $locale = null)
 {
     if (!is_string($date) and !is_numeric($date) and !$date instanceof Zend_Date) {
         return false;
     }
     if ($format !== null and Zend_Locale::isLocale($format, null, false)) {
         $locale = $format;
         $format = null;
     }
     if (empty($locale)) {
         require_once 'Zend/Registry.php';
         if (Zend_Registry::isRegistered('Zend_Locale') === true) {
             $locale = Zend_Registry::get('Zend_Locale');
         }
     }
     if (!Zend_Locale::isLocale($locale, true, false)) {
         if (!Zend_Locale::isLocale($locale, false, false)) {
             require_once 'Zend/Date/Exception.php';
             throw new Zend_Date_Exception("Given locale ({$locale}) does not exist", (string) $locale);
         }
         $locale = new Zend_Locale($locale);
     }
     $locale = (string) $locale;
     if ($format === null) {
         $format = Zend_Locale_Format::getDateFormat($locale);
     } else {
         if (self::$_options['format_type'] == 'php') {
             $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
         }
     }
     try {
         $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'date_format' => $format, 'format_type' => 'iso', 'fix_date' => false));
         if (isset($parsed['year']) and (strpos(strtoupper($format), 'YY') !== false and strpos(strtoupper($format), 'YYYY') === false)) {
             $parsed['year'] = self::getFullYear($parsed['year']);
         }
     } catch (Zend_Locale_Exception $e) {
         // Date can not be parsed
         return false;
     }
     if ((strpos($format, 'Y') !== false or strpos($format, 'y') !== false) and !isset($parsed['year'])) {
         // Year expected but not found
         return false;
     }
     if (strpos($format, 'M') !== false and !isset($parsed['month'])) {
         // Month expected but not found
         return false;
     }
     if (strpos($format, 'd') !== false and !isset($parsed['day'])) {
         // Day expected but not found
         return false;
     }
     if ((strpos($format, 'H') !== false or strpos($format, 'h') !== false) and !isset($parsed['hour'])) {
         // Hour expected but not found
         return false;
     }
     if (strpos($format, 'm') !== false and !isset($parsed['minute'])) {
         // Minute expected but not found
         return false;
     }
     if (strpos($format, 's') !== false and !isset($parsed['second'])) {
         // Second expected  but not found
         return false;
     }
     // Set not given dateparts
     if (isset($parsed['hour']) === false) {
         $parsed['hour'] = 0;
     }
     if (isset($parsed['minute']) === false) {
         $parsed['minute'] = 0;
     }
     if (isset($parsed['second']) === false) {
         $parsed['second'] = 0;
     }
     if (isset($parsed['month']) === false) {
         $parsed['month'] = 1;
     }
     if (isset($parsed['day']) === false) {
         $parsed['day'] = 1;
     }
     if (isset($parsed['year']) === false) {
         $parsed['year'] = 1970;
     }
     $date = new self($parsed, null, $locale);
     $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $parsed['month'], $parsed['day'], $parsed['year']);
     if ($parsed['year'] != $date->date('Y', $timestamp)) {
         // Given year differs from parsed year
         return false;
     }
     if ($parsed['month'] != $date->date('n', $timestamp)) {
         // Given month differs from parsed month
         return false;
     }
     if ($parsed['day'] != $date->date('j', $timestamp)) {
         // Given day differs from parsed day
         return false;
     }
     if ($parsed['hour'] != $date->date('G', $timestamp)) {
         // Given hour differs from parsed hour
         return false;
     }
     if ($parsed['minute'] != $date->date('i', $timestamp)) {
         // Given minute differs from parsed minute
         return false;
     }
     if ($parsed['second'] != $date->date('s', $timestamp)) {
         // Given second differs from parsed second
         return false;
     }
     return true;
 }
示例#28
0
文件: Format.php 项目: lortnus/zf1
 /**
  * Internal function for checking the options array of proper input values
  * See {@link setOptions()} for details.
  *
  * @param  array  $options  Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false,
  *                          locale = Zend_Locale | locale string, precision = whole number between -1 and 30
  * @throws Zend_Locale_Exception
  * @return Options array if no option was given
  */
 private static function checkOptions(array $options = array())
 {
     if (count($options) == 0) {
         return self::$_Options;
     }
     foreach ($options as $name => $value) {
         $name = strtolower($name);
         if ($name !== 'locale') {
             if (gettype($value) === 'string') {
                 $value = strtolower($value);
             }
         }
         switch ($name) {
             case 'number_format':
                 if ($value == 'standard') {
                     $locale = self::$_Options['locale'];
                     if (isset($options['locale'])) {
                         $locale = $options['locale'];
                     }
                     $options['number_format'] = Zend_Locale_Data::getContent($locale, 'decimalnumber');
                 } else {
                     if (gettype($value) !== 'string' and $value !== NULL) {
                         require_once 'Zend/Locale/Exception.php';
                         throw new Zend_Locale_Exception("Unknown number format type '" . gettype($value) . "'. " . "Format '{$value}' must be a valid number format string.");
                     }
                 }
                 break;
             case 'date_format':
                 if ($value == 'standard') {
                     $locale = self::$_Options['locale'];
                     if (isset($options['locale'])) {
                         $locale = $options['locale'];
                     }
                     $options['date_format'] = Zend_Locale_Format::getDateFormat($locale);
                 } else {
                     if (gettype($value) !== 'string' and $value !== NULL) {
                         require_once 'Zend/Locale/Exception.php';
                         throw new Zend_Locale_Exception("Unknown dateformat type '" . gettype($value) . "'. " . "Format '{$value}' must be a valid ISO or PHP date format string.");
                     } else {
                         if (isset($options['format_type']) === true and $options['format_type'] == 'php' or isset($options['format_type']) === false and self::$_Options['format_type'] == 'php') {
                             $options['date_format'] = Zend_Locale_Format::convertPhpToIsoFormat($value);
                         }
                     }
                 }
                 break;
             case 'format_type':
                 if ($value != 'php' && $value != 'iso') {
                     require_once 'Zend/Locale/Exception.php';
                     throw new Zend_Locale_Exception("Unknown date format type '{$value}'. Only 'iso' and 'php'" . " are supported.");
                 }
                 break;
             case 'fix_date':
                 if ($value !== true && $value !== false) {
                     require_once 'Zend/Locale/Exception.php';
                     throw new Zend_Locale_Exception("Enabling correction of dates must be either true or false" . "(fix_date='{$value}').");
                 }
                 break;
             case 'locale':
                 if (gettype($value) === 'string' && strtolower($value) == 'standard') {
                     $options['locale'] = new Zend_Locale();
                 } else {
                     if (!empty($value) && !Zend_Locale::isLocale($value)) {
                         require_once 'Zend/Locale/Exception.php';
                         throw new Zend_Locale_Exception("'" . (gettype($value) === 'object' ? get_class($value) : $value) . "' is not a known locale.");
                     }
                 }
                 break;
             case 'cache':
                 if ($value instanceof Zend_Cache_Core) {
                     Zend_Locale_Data::setCache($value);
                 }
                 break;
             case 'precision':
                 if ($value === NULL) {
                     $value = -1;
                 }
                 if ($value < -1 || $value > 30) {
                     require_once 'Zend/Locale/Exception.php';
                     throw new Zend_Locale_Exception("'{$value}' precision is not a whole number less than 30.");
                 }
                 break;
             default:
                 require_once 'Zend/Locale/Exception.php';
                 throw new Zend_Locale_Exception("Unknown option: '{$name}' = '{$value}'");
                 break;
         }
     }
     return $options;
 }
示例#29
0
 /**
  * Internal function for checking the locale
  *
  * @param string|Zend_Locale $locale Locale to check
  * @return string
  */
 private static function _checkLocale($locale)
 {
     if (empty($locale)) {
         $locale = new Zend_Locale();
     }
     if (!Zend_Locale::isLocale((string) $locale, null, false)) {
         require_once 'Zend/Locale/Exception.php';
         throw new Zend_Locale_Exception("Locale (" . (string) $locale . ") is a unknown locale");
     }
     return (string) $locale;
 }
示例#30
0
 /**
  * test isLocale
  * expected boolean
  */
 public function testIsLocale()
 {
     $locale = new Zend_Locale('ar');
     $this->assertEquals('ar', Zend_Locale::isLocale($locale));
     $this->assertEquals('de', Zend_Locale::isLocale('de'));
     $this->assertEquals('de_AT', Zend_Locale::isLocale('de_AT'));
     $this->assertEquals('de', Zend_Locale::isLocale('de_xx'));
     $this->assertFalse(Zend_Locale::isLocale('yy'));
     $this->assertFalse(Zend_Locale::isLocale(1234));
     $locale = Zend_Locale::isLocale('', true);
     $this->assertTrue(is_string($locale));
     $this->assertTrue(is_string(Zend_Locale::isLocale('auto')));
     $this->assertTrue(is_string(Zend_Locale::isLocale('browser')));
     $this->assertTrue(is_string(Zend_Locale::isLocale('environment')));
 }