// ====================== // DedeCMS 多语言支持(性能优化版) // ====================== $allowLang = array('fr','de','ar','jp','zh'); // 可用语言 $defaultLang = 'en'; // 默认英文 // 【优化1】缓存语言检测结果,避免重复执行正则 $currentLangCache = null; function getCurrentLang() { global $allowLang, $defaultLang, $currentLangCache; // 如果已经检测过,直接返回缓存结果 if ($currentLangCache !== null) { return $currentLangCache; } if(isset($_SERVER['REQUEST_URI'])){ if(preg_match('#^/([a-z]{2})(/|$)#i', $_SERVER['REQUEST_URI'], $m)){ $lang = strtolower($m[1]); if(in_array($lang, $allowLang)){ $currentLangCache = $lang; return $lang; } } } $currentLangCache = $defaultLang; return $defaultLang; } // 【优化2】缓存语言字典,避免重复加载和排序 $langDictCache = array(); function loadLangDict($lang){ global $langDictCache; // 如果已加载,直接返回缓存 if (isset($langDictCache[$lang])) { return $langDictCache[$lang]; } $file = dirname(__FILE__)."/../lang/".$lang.".php"; if(!file_exists($file)){ $langDictCache[$lang] = array(); return array(); } $dict = include($file); if(!is_array($dict)){ $langDictCache[$lang] = array(); return array(); } // 关键:长句优先(仅在首次加载时排序) uksort($dict, function($a, $b){ return strlen($b) - strlen($a); }); $langDictCache[$lang] = $dict; return $dict; } // 【优化3】核心优化:如果是英文,完全跳过所有处理 function translateOutput($html){ global $allowLang, $defaultLang; $lang = getCurrentLang(); // ⚡ 关键优化:英文页面直接返回,不做任何处理 if ($lang == $defaultLang) { return $html; } // ------------------- // 1️⃣ 翻译页面文字(优化:只处理正文内容,不处理整个HTML) // ------------------- $dict = loadLangDict($lang); if(!empty($dict)){ // 【优化4】使用更高效的替换方式 // 如果字典很大,用 strtr 比循环 str_replace 快很多 $search = array_keys($dict); $replace = array_values($dict); $html = str_replace($search, $replace, $html); } // ------------------- // 2️⃣ 给站内链接加语言前缀(排除资源文件) // ------------------- $html = preg_replace_callback( '#(href|src|action)=["\'](/(?!fr/|de/|ar/|jp/|zh/)[^"\']*)["\']#i', function($m) use($lang){ $attr = $m[1]; $path = $m[2]; // 排除资源文件 if(preg_match('#\.(ico|png|jpg|jpeg|gif|svg|css|js|woff2?|ttf|eot)$#i', $path)){ return $m[0]; } // 英文不加前缀(但这里已经排除了英文,保留安全) if($lang == 'en') return $m[0]; if($path === '/') { return $attr.'="/'.$lang.'/"'; } $path = preg_replace('#^/(fr|de|ar|jp|zh)/#i', '/', $path); return $attr.'="/'.$lang.$path.'"'; }, $html ); // ------------------- // 3️⃣ hreflang 标签 + Canonical 标签 // ------------------- $curPath = $_SERVER['REQUEST_URI']; $seoHtml = ''; // 先添加 Canonical(始终指向英文版本) $canonicalUrl = ''; if($curPath=='/' || preg_match('#^/index(\.html|\.php)?$#i', $curPath)){ $canonicalUrl = 'https://'.$_SERVER['HTTP_HOST'].'/'; } else { $urlPath = preg_replace('#^/(fr|de|ar|jp|zh)/#i', '/', $curPath); $canonicalUrl = 'https://'.$_SERVER['HTTP_HOST'].$urlPath; } $seoHtml .= ''."\n"; // 【优化5】缓存 URL 路径处理结果 $urlPath = preg_replace('#^/(fr|de|ar|jp|zh)/#i', '/', $curPath); $host = $_SERVER['HTTP_HOST']; // 再添加各语言版本 hreflang foreach(array_merge(array($defaultLang), $allowLang) as $l){ if($curPath=='/' || preg_match('#^/index(\.html|\.php)?$#i', $curPath)){ $url = $l==$defaultLang ? 'https://'.$host.'/' : 'https://'.$host.'/'.$l.'/'; } else { $url = $l==$defaultLang ? 'https://'.$host.$urlPath : 'https://'.$host.'/'.$l.$urlPath; } $seoHtml .= ''."\n"; } // 最后添加 x-default $defaultUrl = $curPath=='/' || preg_match('#^/index(\.html|\.php)?$#i', $curPath) ? 'https://'.$host.'/' : 'https://'.$host.$urlPath; $seoHtml .= ''."\n"; // 插入到 前面 $html = preg_replace('##i', $seoHtml.'', $html, 1); // ------------------- // 4️⃣ 生成下拉菜单多语言链接 // ------------------- $cleanPath = $urlPath; // 复用前面已经处理好的路径 $langMenuHtml = ''; $langMenuHtml .= '
  • English
  • '; foreach($allowLang as $l){ $url = 'https://'.$host.'/'.$l.$cleanPath; switch($l){ case 'fr': $label='Français'; break; case 'de': $label='Deutsch'; break; case 'ar': $label='العربية'; break; case 'jp': $label='日本語'; break; case 'zh': $label='中文'; break; default: $label=strtoupper($l); } $langMenuHtml .= '
  • '.$label.'
  • '; } // 替换下拉菜单 $html = preg_replace_callback( '#(]*dropdown-menu dropdown-menu-end[^>]*>)(.*?)()#is', function($m) use($langMenuHtml){ return $m[1].$langMenuHtml.$m[3]; }, $html ); return $html; } // --------------------------- // 开启输出缓冲 // --------------------------- ob_start("translateOutput"); Warning: require_once(DEDEINC/arc.archives.class.php): failed to open stream: No such file or directory in /www/wwwroot/www.vipplc.com/plus/view.php on line 17 Fatal error: require_once(): Failed opening required 'DEDEINC/arc.archives.class.php' (include_path='.:/www/server/php/53/lib/php') in /www/wwwroot/www.vipplc.com/plus/view.php on line 17