Date: 2026-01-11 15:30 Summary: Unified language handling and introduced a tool route registrar so prefixed locales no longer double-redirect.
[question]
- 访问
http://localhost:8787/ja/下点击http://localhost:8787/ja/tools/website-headers会被重定向到http://localhost:8787/zh/ja/tools/website-headers并出现 404。 http://localhost:8787/ja/tools/website-headers页面显示为英文而非日语。markdown to html工具没有支持目前的 10 国语言切换功能。- 是否可以将目前两个工具的语言控制合并,以便统一语言管理并便于后续扩展所有工具?
- 要求实现“工具路由注册器”,把现有工具改用统一注册方式。
[try to solve]
Root cause: Accept-Language negotiation treated a path as non-explicit when the first segment was a supported language but not in SITE_LANGS. The middleware then prepended the negotiated language, producing paths like /zh/ja/... and 404s.
Fixes and refactor:
- Explicit language prefix detection — Updated
getExplicitLangFromPathinsrc/index.tsso it only checksisSupportedLang(seg)(no longer requires the segment to be inSITE_LANGS). Explicit URLs such as/ja/...are no longer wrongly redirected to/zh/ja/.... - Render language from the path — When the path has a supported language prefix, tool routes render in that language even if it is not listed in
SITE_LANGS. - Shared language helpers — Extracted negotiation/helpers into
src/site/lang.ts(DEFAULT_LANGS,getEnabledLangs,getFallbackLang,getDefaultLang,parseAcceptLanguage,pickLang,getExplicitLangFromPath,withLangPrefix,getLangFromPath,stripLangPrefix,langAlternatesForPath). - Markdown tool language switcher — Updated
src/pages/markdownToHtmlPage.tsso the language dropdown and alternate links use fullsupportedLangs(all 10 locales). - Tool route registrar — Added
src/site/toolRegistrar.tsexportingregisterToolPage(app, toolName, renderFn), which registers/tools/<name>and/:lang/tools/<name>with shared language logic. Migratedwebsite-headersandmarkdown-to-htmlto this registrar.
Local verification:
npm run dev
# or
wrangler dev
http://localhost:8787/ja/tools/website-headersshould render in Japanese whensrc/site/i18n/ja.tshas entries.http://localhost:8787/tools/website-headersmay redirect based onAccept-Language.http://localhost:8787/tools/markdown-to-htmlshould show all 10 languages in the header switcher.
Files touched: src/index.ts, src/site/lang.ts, src/site/toolRegistrar.ts, src/pages/markdownToHtmlPage.ts, and related note in dev-logs/2026-01-11-fix-lang-redirect.md.
[actions]
- Updated:
src/index.ts - Added:
src/site/lang.ts - Added:
src/site/toolRegistrar.ts - Updated:
src/pages/markdownToHtmlPage.ts - Related:
dev-logs/2026-01-11-fix-lang-redirect.md