ZBlogPHP::LoadLanguage PHP Method

LoadLanguage() public method

载入指定应用语言包
public LoadLanguage ( string $type, string $id, $default = '' ) : null
$type string 应用类型(system|theme|plugin)
$id string 应用ID
return null
    public function LoadLanguage($type, $id, $default = '')
    {
        $languagePath = $this->path;
        $languageRegEx = '/^([0-9A-Z\\-_]*)\\.php$/ui';
        $languageList = array();
        $language = '';
        $default = str_replace(array('/', '\\'), '', $default);
        $languagePtr =& $this->lang;
        if ($default == '') {
            $default = $this->option['ZC_BLOG_LANGUAGEPACK'];
        }
        $defaultLanguageList = array($default, 'zh-cn', 'zh-tw', 'en');
        switch ($type) {
            case 'system':
                $languagePath .= 'zb_users/language/';
                break;
            case 'plugin':
            case 'theme':
                $languagePath .= 'zb_users/' . $type . '/' . $id . '/language/';
                $languagePtr =& $this->lang[$id];
                break;
            default:
                $languagePath .= $type . '/language/';
                $languagePtr =& $this->lang[$id];
                break;
        }
        $handle = opendir($languagePath);
        $match = null;
        if ($handle) {
            while (false !== ($file = readdir($handle))) {
                if (preg_match($languageRegEx, $file, $match)) {
                    $languageList[] = $match[1];
                }
            }
            closedir($handle);
        } else {
            // 这里不会执行到,在opendir时就已经抛出E_WARNING
            throw new Exception('Cannot opendir(' . $languagePath . ')');
            return false;
        }
        if (count($languageList) === 0) {
            throw new Exception('No language in ' . $languagePath);
            return false;
        }
        for ($i = 0; $i < count($defaultLanguageList); $i++) {
            // 在效率上,array_search和命名数组没有本质区别,至少在这里如此。
            if (false !== array_search($defaultLanguageList[$i], $languageList)) {
                $language = $defaultLanguageList[$i];
                break;
            }
        }
        if ($language === '') {
            throw new Exception('Language ' . $default . ' is not found in ' . $languagePath);
            return false;
        }
        $languagePath .= $language . '.php';
        $languagePtr = (require $languagePath);
        $this->langpacklist[] = array($type, $id, $language);
        return true;
    }
ZBlogPHP