Skip to content

Fix i18n language switching under the /wiki deploy subpath - #8

Merged
clouths merged 1 commit into
mainfrom
fix/i18n-subpath
Oct 15, 2026
Merged

clouths merged 1 commit into
mainfrom
fix/i18n-subpath

Conversation

@clouths

@clouths clouths commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Problem

On the live site (GitHub Pages project site, openperpetuum.github.io/wiki/) the language switcher is broken: switching languages never navigates to the translated page, translated pages don't activate their language from the URL, and rewritten nav links 404. Locally (root path) everything works, which is why CI never caught it.

Root cause: the runtime i18n code assumed the site lives at the domain root.

  • getLangFromUrl() anchored its regex at the pathname start → /wiki/fr/... never detected French
  • translatedUrl() computed wrong targets (/fr/wiki/...) → returned null → no navigation
  • rewriteNavLinks() wrote bare /fr/... hrefs → 404 under the subpath
  • the pre-paint head script in base.html had the same anchored-regex bug (flash of English, wrong <html lang>)

Fix

Mirrors the pattern search.js/zone-map.js already use for runtime URLs: read the deploy base from body[data-root] (config.base_url), strip its path part for the site-absolute language math, re-add it to results. No CI workflow changes needed — the existing sed base_url + rewrite_site_base.py steps already produce correct data-root and static hrefs.

  • tools/gen_i18n.py + regenerated static/i18n.js: new rootPath()/sitePath() helpers; getLangFromUrl, translatedUrl, rewriteNavLinks work on site-absolute paths
  • templates/base.html: head script strips config.base_url (inlined at build) before matching; sidenav stripLang() now only strips known language codes (window.WIKI_LANGS) instead of any 2–3-char segment

Tests

New Playwright subpath project that mirrors the deploy exactly:

  • tests/setup-subpath.js: copies public/, runs the same rewrite_site_base.py as CI, points data-root/wikiRoot at the Pages URL
  • tests/serve.js: serves the fixture under /wiki/ (also normalizes duplicate slashes)
  • tests/i18n-subpath.spec.js: 8 tests — URL language detection, switcher navigation both directions, chrome-only swap on untranslated pages, rewritten nav links (asserts no bare /<lang>/ links exist), search under subpath, no-flash head detection

Full suite locally: 179 passed, 0 failed (77 platform skips).

On the GitHub Pages project site the wiki is served under /wiki/, but the
runtime i18n code assumed the site lives at the domain root:

- getLangFromUrl() anchored its regex at the pathname start, so /wiki/fr/
  never detected French
- translatedUrl() computed wrong targets (/fr/wiki/...), so the language
  switcher never navigated on translated pages
- rewriteNavLinks() wrote bare /fr/... hrefs that 404 under the subpath
- the pre-paint head script (base.html) had the same anchored-regex bug
- sidenav stripLang() could eat a short content segment matching a
  2-3-letter code

The runtime now mirrors the established search.js/zone-map.js pattern:
read the deploy base from body[data-root] (config.base_url), strip its
path part for the site-absolute language math, re-add it to results.
The head script strips config.base_url (inlined at build time) before
matching; stripLang() now only strips known language codes.

Tested via a new Playwright 'subpath' project: tests/setup-subpath.js
builds a copy of public/ exactly as the deploy pipeline serves it
(rewrite_site_base.py + Pages base_url), served under /wiki/ by
tests/serve.js; tests/i18n-subpath.spec.js mirrors the key i18n.spec.js
behaviors against it. Full suite: 179 passed, 0 failed.
@clouths clouths self-assigned this Oct 15, 2026
@clouths

clouths commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

The failing tests are unrelated to the changes made here.

I'll fix it in a follow-up PR.

@clouths
clouths merged commit c80d943 into main Oct 15, 2026
11 of 13 checks passed
@clouths
clouths deleted the fix/i18n-subpath branch October 15, 2026 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants