From 9d5154afa6002358ae79291986a95df58c6bdd16 Mon Sep 17 00:00:00 2001 From: "chengbo.chu" <13951848647@163.com> Date: Fri, 28 Feb 2025 14:02:28 +0800 Subject: [PATCH 1/2] Feat: update community url --- themes/fibos/layout/partials/main_menu.ejs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/fibos/layout/partials/main_menu.ejs b/themes/fibos/layout/partials/main_menu.ejs index f323b26..c7f19fb 100755 --- a/themes/fibos/layout/partials/main_menu.ejs +++ b/themes/fibos/layout/partials/main_menu.ejs @@ -12,7 +12,7 @@ " class="nav-link team<%- page.path.match(/\/(technology)(\/|$)/) ? ' current' : '' %>">技术手册 -->
  • - <%= __('menu.community') %> + <%= __('menu.community') %>
  • FIBOS From 1316520df949fab22c6101b26035239b0d1cfaef Mon Sep 17 00:00:00 2001 From: "chengbo.chu" <13951848647@163.com> Date: Fri, 28 Feb 2025 14:03:24 +0800 Subject: [PATCH 2/2] Feat: update Language Identification --- scripts/middleware.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 scripts/middleware.js diff --git a/scripts/middleware.js b/scripts/middleware.js new file mode 100644 index 0000000..886c4d9 --- /dev/null +++ b/scripts/middleware.js @@ -0,0 +1,34 @@ +hexo.extend.filter.register('server_middleware', function(app) { + const languageMap = { + 'zh': 'zh-cn', + 'zh-tw': 'zh-cn', + 'zh-hk': 'zh-cn', + 'en-us': 'en-us', + 'en': 'en-us' + }; + + app.use(function(req, res, next) { + // 分解路径并获取第一个部分作为语言代码 + const urlParts = req.url.split('/'); + const pathLang = urlParts[1]; + + // 如果路径已经是 `/en-us` 或 `/zh-cn`,不需要重定向 + if (pathLang === 'en-us' || pathLang === 'zh-cn') { + return next(); + } + + // 如果路径开头不是有效的语言代码,或需要标准化为 `/en-us` + const acceptLanguage = req.headers['accept-language']; + const preferredLang = acceptLanguage ? acceptLanguage.split(',')[0].split('-')[0] : 'en'; + + // 根据映射获取目标语言,默认 `en-us` + const targetLang = languageMap[preferredLang] || 'en-us'; + + if (targetLang === 'zh-cn') { + res.writeHead(302, { 'Location': `/zh-cn${req.url.replace(`/${pathLang}`, '')}` }); + } else { + res.writeHead(302, { 'Location': `/en-us${req.url.replace(`/${pathLang}`, '')}` }); + } + res.end(); + }); +});