_options['logMessage'] = "%message%"; parent::__construct($options); } public function setSection($section) { $this->_options['section'] = $section; return $this; } public function isSection($section, $locale) { return (isset($this->_translate[$locale]) && isset($this->_translate[$locale][$section])); } protected function _loadTranslationData($filename, $locale, array $options = array()) { $this->_bigEndian = false; $options = $options + $this->_options; $section = str_replace('.mo', '', basename($filename)); if ($options['clear'] || !isset($this->_translate[$locale])) { $this->_translate[$locale] = array($section => array()); } $this->_file = @fopen($filename, 'rb'); if (!$this->_file) { // require_once 'Zend/Translate/Exception.php'; throw new Zend_Translate_Exception('Error opening translation file \'' . $filename . '\'.'); } if (@filesize($filename) < 10) { // require_once 'Zend/Translate/Exception.php'; throw new Zend_Translate_Exception('\'' . $filename . '\' is not a gettext file'); } // get Endian $input = $this->_readMOData(1); if (strtolower(substr(dechex($input[1]), -8)) == "950412de") { $this->_bigEndian = false; } else if (strtolower(substr(dechex($input[1]), -8)) == "de120495") { $this->_bigEndian = true; } else { // require_once 'Zend/Translate/Exception.php'; throw new Zend_Translate_Exception('\'' . $filename . '\' is not a gettext file'); } // read revision - not supported for now $input = $this->_readMOData(1); // number of bytes $input = $this->_readMOData(1); $total = $input[1]; // number of original strings $input = $this->_readMOData(1); $OOffset = $input[1]; // number of translation strings $input = $this->_readMOData(1); $TOffset = $input[1]; // fill the original table fseek($this->_file, $OOffset); $origtemp = $this->_readMOData(2 * $total); fseek($this->_file, $TOffset); $transtemp = $this->_readMOData(2 * $total); for($count = 0; $count < $total; ++$count) { if ($origtemp[$count * 2 + 1] != 0) { fseek($this->_file, $origtemp[$count * 2 + 2]); $original = @fread($this->_file, $origtemp[$count * 2 + 1]); } else { $original = ''; } if ($transtemp[$count * 2 + 1] != 0) { fseek($this->_file, $transtemp[$count * 2 + 2]); $this->_translate[$locale][$section][$original] = fread($this->_file, $transtemp[$count * 2 + 1]); } } $this->_translate[$locale][$section][''] = trim($this->_translate[$locale][$section]['']); if (empty($this->_translate[$locale][$section][''])) { $this->_adapterInfo[$filename] = 'No adapter information available'; } else { $this->_adapterInfo[$filename] = $this->_translate[$locale][$section]['']; } unset($this->_translate[$locale][$section]['']); } protected function _readMOData($bytes) { if ($this->_bigEndian === false) { return unpack('V' . $bytes, fread($this->_file, 4 * $bytes)); } else { return unpack('N' . $bytes, fread($this->_file, 4 * $bytes)); } } public function translate($messageId, $locale = null) { if ($locale === null) { $locale = $this->_options['locale']; } $section = $this->_options['section']; if (!Zend_Locale::isLocale($locale, true, false)) { if (!Zend_Locale::isLocale($locale, false, false)) { // language does not exist, return original string return $messageId; } $locale = new Zend_Locale($locale); } $locale = (string) $locale; if (isset($this->_translate[$locale][$section][$messageId]) === true) { // return original translation return $this->_translate[$locale][$section][$messageId]; } else if ($section != 'common' && isset($this->_translate[$locale]['common'][$messageId]) === true) { return $this->_translate[$locale]['common'][$messageId]; } else if (strlen($locale) != 2) { // faster than creating a new locale and separate the leading part $locale = substr($locale, 0, -strlen(strrchr($locale, '_'))); if (isset($this->_translate[$locale][$section][$messageId]) === true) { // return regionless translation (en_US -> en) return $this->_translate[$locale][$section][$messageId]; } else if ($section != 'common' && isset($this->_translate[$locale]['common'][$messageId]) === true) { return $this->_translate[$locale]['common'][$messageId]; } } $this->_log($messageId, $locale); // no translation found, return original return $messageId; } public function isTranslated($messageId, $original = false, $locale = null) { $section = $this->_options['section']; 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 $this->_log($messageId, $locale); return false; } $locale = new Zend_Locale(); } $locale = (string) $locale; if (isset($this->_translate[$locale][$section][$messageId]) === true) { // 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 (isset($this->_translate[$locale][$section][$messageId]) === true) { // return regionless translation (en_US -> en) return true; } } // No translation found, return original $this->_log($messageId, $locale); return false; } }