adapter.php

来自「Bug tracker, and reporter.」· PHP 代码 · 共 497 行 · 第 1/2 页

PHP
497
字号
    }    /**     * Returns the available languages from this adapter     *     * @return array     */    public function getList()    {        $list = array_keys($this->_translate);        $result = null;        foreach($list as $key => $value) {            if (!empty($this->_translate[$value])) {                $result[$value] = $value;            }        }        return $result;    }    /**     * Returns all available message ids from this adapter     * If no locale is given, the actual language will be used     *     * @param  $locale  String|Zend_Locale  Language to return the message ids from     * @return array     */    public function getMessageIds($locale = null)    {        if (empty($locale) or !$this->isAvailable($locale)) {            $locale = $this->_options['locale'];        }        return array_keys($this->_translate[$locale]);    }    /**     * Returns all available translations from this adapter     * If no locale is given, the actual language will be used     * If 'all' is given the complete translation dictionary will be returned     *     * @param  $locale  String|Zend_Locale  Language to return the messages from     * @return array     */    public function getMessages($locale = null)    {        if ($locale == 'all') {            return $this->_translate;        }        if (empty($locale) or !$this->isAvailable($locale)) {            $locale = $this->_options['locale'];        }        return $this->_translate[$locale];    }    /**     * Is the wished language available ?     *     * @param  string|Zend_Locale  $locale  Language to search for, identical with locale identifier,     *                                      see Zend_Locale for more information     * @return boolean     */    public function isAvailable($locale)    {        if ($locale instanceof Zend_Locale) {            $locale = $locale->toString();        }        return array_key_exists($locale, $this->_translate);    }    /**     * Load translation data     *     * @param  mixed               $data     * @param  string|Zend_Locale  $locale     * @param  array               $options     */    abstract protected function _loadTranslationData($data, $locale, array $options = array());    /**     * Internal function for adding translation data     *     * It may be a new language or additional data for existing language     * If $clear parameter is true, then translation data for specified     * language is replaced and added otherwise     *     * @param  array|string          $data    Translation data     * @param  string|Zend_Locale    $locale  Locale/Language to add data for, identical with locale identifier,     *                                        see Zend_Locale for more information     * @param  array                 $options OPTIONAL Option for this Adapter     * @throws Zend_Translate_Exception     * @return Zend_Translate_Adapter     */    private function _addTranslationData($data, $locale, array $options = array())    {        if (!$locale = Zend_Locale::isLocale($locale)) {            require_once 'Zend/Translate/Exception.php';            throw new Zend_Translate_Exception("The given Language ({$locale}) does not exist");        }        if (!array_key_exists($locale, $this->_translate)) {            $this->_translate[$locale] = array();        }        $this->_loadTranslationData($data, $locale, $options);        if ($this->_automatic === true) {            $find = new Zend_Locale($locale);            $browser = $find->getBrowser() + $find->getEnvironment();            arsort($browser);            foreach($browser as $language => $quality) {                if (array_key_exists($language, $this->_translate)) {                    $this->_options['locale'] = $language;                    break;                }            }        }        if (isset(self::$_cache)) {            $id = 'Zend_Translate_' . $this->toString();            self::$_cache->save( serialize($this->_translate), $id);        }        return $this;    }    /**     * Translates the given string     * returns the translation     *     * @param  string              $messageId  Translation string     * @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'];        }        if (!$locale = Zend_Locale::isLocale($locale)) {            // language does not exist, return original string            return $messageId;        }        if ((is_array($this->_translate) and array_key_exists($locale, $this->_translate)) and            (is_array($this->_translate[$locale]) and array_key_exists($messageId, $this->_translate[$locale]))) {            // return original translation            return $this->_translate[$locale][$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 ((is_array($this->_translate) and array_key_exists($locale, $this->_translate)) and                (is_array($this->_translate[$locale]) 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;    }    /**     * Translates the given string     * returns the translation     *     * @param  string              $messageId  Translation string     * @param  string|Zend_Locale  $locale     OPTIONAL Locale/Language to use, identical with locale identifier,     *                                         see Zend_Locale for more information     * @return string     */    public function _($messageId, $locale = null)    {        return $this->translate($messageId, $locale);    }    /**     * 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'];        } else {            if (!$locale = Zend_Locale::isLocale($locale)) {                // language does not exist, return original string                return false;            }        }        if ((is_array($this->_translate) and array_key_exists($locale, $this->_translate)) and            (is_array($this->_translate[$locale]) and array_key_exists($messageId, $this->_translate[$locale]))) {            // 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_array($this->_translate) and array_key_exists($locale, $this->_translate)) and                (is_array($this->_translate[$locale]) and array_key_exists($messageId, $this->_translate[$locale]))) {                // return regionless translation (en_US -> en)                return true;            }        }        // no translation found, return original        return false;    }    /**     * Sets a cache for all Zend_Translate_Adapters     *     * @param Zend_Cache_Core $cache Cache to store to     */    public static function setCache(Zend_Cache_Core $cache)    {        self::$_cache = $cache;    }    /**     * Returns the adapter name     *     * @return string     */    abstract public function toString();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?