/** * postDispatch - auto render a view * * Override Zend_Controller_Action_Helper_ViewRenderer::postDispatch() * @return void */ public function postDispatch() { /** * Get module name, controller name, action name */ $request = $this->getRequest(); $module = $request->getModuleName(); if (null === $module) { $module = $this->getFrontController()->getDispatcher()->getDefaultModule(); } $controller = $request->getControllerName(); if (null === $controller) { $controller = $this->getFrontController()->getDispatcher()->getDefaultControllerName(); } $action = $request->getActionName(); if (null == $action) { $action = $this->getFrontController()->getDispatcher()->getDefaultAction(); } /** * Set cacheId for Smarty's caching */ $langCode = Vi_Registry::get('langCode'); $this->view->cacheId = Vi_Registry::getAppName() . '|' . $langCode . '|module|' . $module . '_' . $controller . '_' . $action . ($this->view->cacheId ? '_' . $this->view->cacheId : ''); /** * Call parent's postDispatch() function */ $result = parent::postDispatch(); /** * Revive Vi_Language::$currentType and Vi_Language::$currentName */ Vi_Language::$currentType = Vi_Registry::get('controllerCurrentType'); Vi_Language::$currentName = Vi_Registry::get('controllerCurrentName'); return $result; }
/** * Init configuration */ public function init() { /** * Notify module type to translator */ $this->_currentType = Vi_Language::$currentType; $this->_currentName = Vi_Language::$currentName; Vi_Language::$currentType = Vi_Language::TYPE_MODULE; Vi_Language::$currentName = $this->_request->getModuleName(); /** * Add include path for controller */ set_include_path(get_include_path() . PATH_SEPARATOR . 'modules/' . Vi_Registry::getModuleName()); /** * start session for system */ $this->session = new Zend_Session_Namespace(Vi_Constant::SESSION_NAMESPACE . "_" . Vi_Registry::getAppName()); Vi_Registry::set('session', $this->session); /** * Load module config */ $this->_config = Vi_Registry::get('config'); $configFile = 'modules/' . $this->_request->getModuleName() . '/config.php'; if (is_file($configFile)) { $moduleConfig = (include $configFile); /** * Unset config item have values as default system */ $this->_unsetConfig($moduleConfig); /** * Merge with system config */ $this->_config = array_merge($this->_config, $moduleConfig); Vi_Registry::set('config', $this->_config); } /** * Load module constant */ $constantFile = 'modules/' . $this->_request->getModuleName() . '/Constant.php'; if (is_file($constantFile)) { include_once $constantFile; } /** * start up the auth */ $this->auth = Vi_Auth::getInstance(); $authStorage = new Vi_Auth_Storage(); $this->auth->setStorage($authStorage); Vi_Registry::set('auth', $this->auth); /** * Only ADMIN application need following function * * Don't remove comment if you need change. * In Vi_Controller_Action_Admin, this function is always loaded */ // $this->_initAcl();//Don't load permission as default }
/** * Translate a phrase of words, or array of phrases * * @param string|array $phrase * @param array $params Params are used to replace in $phrase * @param string $langCode Default is null. If $langCode == null, it will use Vi_Registry::get('langCode') * * @return string|array * @throws Exception if language file is not readable, or $phrase is not string/array * * @example * $this->translate2('Version {{0}} is good', * array('1.0'), 'en'); * //Result: 'Version 1.0 is good'; * * * $this->translate2(array('Version {{0}} is good', * 'But version {{0}} is better than version {{1}}'), * array( * array( * '1.0' * ), * array( * '2.0', * '1.0' * ) * ), * 'en'); * * //Result: array('Version 1.0 is good', 'But version 2.0 is better than version 1.0') */ public function translate2($phrase, $params = array(), $langCode = null) { if (null == $langCode) { $langCode = Vi_Registry::get('langCode'); } else { /** * @TODO Check $langCode to sure it has in system's languages */ } if (null == $phrase) { return ''; } if (!isset($this->_name[self::$currentType])) { $this->_name[self::$currentType] = ''; } if (!isset($this->_translatedPhrases[self::$currentType])) { $this->_translatedPhrases[self::$currentType] = array(); } if (!isset($this->_translatedPhrases[self::$currentType][$langCode])) { $this->_translatedPhrases[self::$currentType][$langCode] = array(); } /** * Detect module, or layout, or sticker */ if (self::$currentType == self::TYPE_MODULE) { $path = 'languages/module.' . self::$currentName; } elseif (self::$currentType == self::TYPE_LAYOUT) { $config = Vi_Registry::get('config'); self::$currentName = $config['layoutCollection']; $path = 'languages/layout.' . self::$currentName; } else { $path = 'languages/sticker.' . self::$currentName; } /** * Load language file */ $this->_filePath = "{$path}.{$langCode}.php"; if (!is_writable('languages')) { throw new Exception('Language folder languages' . '" is not writable. Please change it to 777 mode.'); } if ($this->_name[self::$currentType] != self::$currentName || empty($this->_translatedPhrases[self::$currentType][$langCode])) { if (is_file($this->_filePath)) { if (is_readable($this->_filePath)) { $this->_translatedPhrases[self::$currentType][$langCode] = (require_once "{$this->_filePath}"); } else { throw new Exception("Permission denied. Language file ({$this->_filePath}) is not readable."); } $this->isTranslated = true; } else { $this->isTranslated = false; } $this->_name[self::$currentType] = self::$currentName; } /** * Begin translation */ $return = ''; if (is_string($phrase)) { if (array_key_exists($phrase, $this->_translatedPhrases[self::$currentType][$langCode])) { $return = $this->_translatedPhrases[self::$currentType][$langCode][$phrase]; if (null == $return) { return "[{$phrase}]"; } else { return $return; } } else { /** * Not is translated yet */ $this->isTranslated = false; $this->_translatedPhrases[self::$currentType][$langCode][$phrase] = ''; $return = "[{$phrase}]"; } } elseif (is_array($phrase)) { foreach ($phrase as $item) { if (array_key_exists($item, $this->_translatedPhrases[self::$currentType][$langCode])) { $return[$item] = $this->_translatedPhrases[self::$currentType][$langCode][$item]; if (null == $return[$item]) { $return[$item] = "[{$item}]"; } } else { /** * Not is translated yet */ $this->isTranslated = false; $this->_translatedPhrases[self::$currentType][$langCode][$item] = ''; $return[$item] = "[{$item}]"; } } } else { throw new Exception("The param of Vi_Language::translate() is not correct. Params have to be string or array"); } /** * Write language file */ if (!$this->isTranslated) { $this->_writer->write($this->_filePath, new Zend_Config($this->_translatedPhrases[self::$currentType][$langCode])); } /** * Translate with params */ if (is_array($params) && !empty($params)) { if (is_string($phrase)) { $find = array(); $replace = array(); for ($i = 0; $i < count($params); $i++) { $find[] = "{{$i}}"; $replace[] = $params[$i]; } $return = str_replace($find, $replace, $return); } elseif (is_array($phrase)) { foreach ($phrase as $index => $item) { if (is_array($params[$index]) && !empty($params[$index])) { $find = array(); $replace = array(); for ($i = 0; $i < count($params[$index]); $i++) { $find[] = "{{$i}}"; $replace[] = $params[$index][$i]; } $return[$item] = str_replace($find, $replace, $return[$item]); } } } } return $return; }
public function __destruct() { Vi_Language::$currentType = $this->_saveInfo['currentType']; Vi_Language::$currentName = $this->_saveInfo['currentName']; }