Repository navigation
Serve English in place of untranslated locale routes #2214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
David Pine (IEvangelist)
merged 12 commits into
main
from
dapine-microsoft-locale-fallback-served-in-place
Nov 20, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
88aed08
Serve English in place of untranslated locale routes
IEvangelist 0b3a81b
Skip link validation for untranslated locale routes with an English page
IEvangelist 58d80e9
Test link-validation excludes for untranslated locale routes
IEvangelist 9e6fcc1
Don't serve nested locale routes as English fallbacks
IEvangelist 17cf123
Restore generated icon safelist
IEvangelist 65d6e56
Build the static file index on first use
IEvangelist 0a0bbc3
Version locale fallback dictionary requests by content hash
IEvangelist a496f12
Unpack the locale manifest last in E2E so StaticHost never indexes a …
IEvangelist 53058b7
Address locale fallback review feedback
IEvangelist eb969f1
Merge remote-tracking branch 'origin/main' into dapine-microsoft-loca…
IEvangelist 8708bc6
Localize tooltips, icon-only controls, and search on locale fallback …
IEvangelist 86a5673
Pass Pagefind's locale strings explicitly on locale fallback pages
IEvangelist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // @ts-check | ||
| import { existsSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { locales } from './locales.ts'; | ||
|
|
||
| const defaultDocsDir = fileURLToPath(new URL('../src/content/docs/', import.meta.url)); | ||
|
|
||
| /** Custom Astro destinations the validator can't see; checked by browser tests instead. */ | ||
| const customPaths = new Set([ | ||
| '/i18n/', | ||
| '/hub/', | ||
| '/hub/glossary/ats/', | ||
| '/get-started/glossary/#polyglot', | ||
| ]); | ||
|
|
||
| const localePrefixes = Object.keys(locales).filter((locale) => locale !== 'root'); | ||
|
|
||
| /** | ||
| * @param {string} link | ||
| */ | ||
| function isCustomPath(link) { | ||
| const path = link.split('?')[0]; | ||
| return ( | ||
| customPaths.has(path) || | ||
| path === '/reference/api' || | ||
| path.startsWith('/reference/api/') | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Whether a docs page for a route exists as content. | ||
| * | ||
| * @param {string} path Route path without hash or query. | ||
| * @param {string} docsDir | ||
| */ | ||
| function hasDocsPage(path, docsDir) { | ||
| const slug = path.replace(/^\/+|\/+$/g, ''); | ||
| const base = slug === '' ? 'index' : slug; | ||
| return ['.md', '.mdx', '/index.md', '/index.mdx'].some((suffix) => | ||
| existsSync(join(docsDir, `${base}${suffix}`)) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Links to untranslated locale routes (e.g. `/de/docs/`) point at pages that | ||
| * aren't built: StaticHost serves the English page in place. Skip those only | ||
| * when the page isn't translated and the English target exists, so broken | ||
| * links and translated anchors are still reported. | ||
| * | ||
| * @param {string} link | ||
| * @param {string} [docsDir] | ||
| */ | ||
| export function isExcludedFromLinkValidation(link, docsDir = defaultDocsDir) { | ||
| if (isCustomPath(link)) return true; | ||
|
|
||
| const match = /^\/([^/?#]+)(\/[^?#]*)?/.exec(link); | ||
| if (!match || !localePrefixes.includes(match[1])) return false; | ||
|
|
||
| const unlocalized = match[2] ?? '/'; | ||
| const unlocalizedLink = `${unlocalized}${link.slice(match[0].length)}`; | ||
| if (isCustomPath(unlocalizedLink)) return true; | ||
|
|
||
| if (hasDocsPage(`/${match[1]}${unlocalized}`, docsDir)) return false; | ||
| return hasDocsPage(unlocalized, docsDir); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // @ts-check | ||
| import { locales } from './locales.ts'; | ||
| import { redirects } from './redirects.mjs'; | ||
| import { getTranslatedPaths } from './translated-paths.mjs'; | ||
|
|
||
| const localeSegments = Object.keys(locales) | ||
| .filter((key) => key !== 'root') | ||
| .map((key) => key.toLowerCase()); | ||
|
|
||
| /** | ||
| * Path the dev server should render for `pathname`, mirroring StaticHost's | ||
| * `LocaleFallbackMiddleware`: untranslated locale routes (which the build no | ||
| * longer emits) render the English page in place. Returns `undefined` when the | ||
| * request needs no rewrite. | ||
| * | ||
| * @param {string} pathname | ||
| * @param {ReadonlySet<string>} translatedPaths | ||
| * @param {ReadonlySet<string>} [redirectSources] Lowercased configured redirect | ||
| * sources; Astro handles those itself, as StaticHost leaves existing paths alone. | ||
| * @returns {string | undefined} | ||
| */ | ||
| export function getLocaleFallbackDevPath(pathname, translatedPaths, redirectSources = new Set()) { | ||
| const match = pathname.match(/^\/([^/]+)(\/.*)$/); | ||
| if (!match) return undefined; | ||
|
|
||
| const [, segment, unlocalizedPath] = match; | ||
| if (!localeSegments.includes(segment.toLowerCase()) || unlocalizedPath === '/') { | ||
| return undefined; | ||
| } | ||
| // `/de/ja/x/` must not serve the Japanese page as an English fallback for `de`. | ||
| if (localeSegments.includes(unlocalizedPath.split('/')[1]?.toLowerCase())) return undefined; | ||
|
|
||
| const lastSegment = unlocalizedPath.slice(unlocalizedPath.lastIndexOf('/') + 1); | ||
| const isMarkdown = lastSegment.endsWith('.md'); | ||
| // Only pages and their Markdown copies fall back; directory routes without a | ||
| // trailing slash are left for the dev server to redirect. | ||
| if (!unlocalizedPath.endsWith('/') && !isMarkdown) return undefined; | ||
| const pagePath = isMarkdown ? `${pathname.slice(0, -'.md'.length)}/` : pathname; | ||
| const normalizedPagePath = pagePath.toLowerCase(); | ||
| if (translatedPaths.has(normalizedPagePath) || redirectSources.has(normalizedPagePath)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return unlocalizedPath; | ||
| } | ||
|
|
||
| /** | ||
| * Serves the English page in place of untranslated locale routes during | ||
| * `astro dev`, so local development and E2E runs match production. | ||
| * | ||
| * @returns {import('astro').AstroIntegration} | ||
| */ | ||
| export function localeFallbackDevIntegration() { | ||
| const redirectSources = new Set( | ||
| Object.keys(redirects).map((source) => | ||
| (source.endsWith('/') ? source : `${source}/`).toLowerCase() | ||
| ) | ||
| ); | ||
| return { | ||
| name: 'aspire-locale-fallback-dev', | ||
| hooks: { | ||
| 'astro:server:setup': ({ server }) => { | ||
| server.middlewares.use((req, res, next) => { | ||
| const url = new URL(req.url ?? '/', 'http://localhost'); | ||
| // Re-read on each locale request so new translations are picked up without a restart. | ||
| const rewrittenPath = localeSegments.some((locale) => | ||
| url.pathname.toLowerCase().startsWith(`/${locale}/`) | ||
| ) | ||
| ? getLocaleFallbackDevPath(url.pathname, new Set(getTranslatedPaths()), redirectSources) | ||
| : undefined; | ||
| if (rewrittenPath) { | ||
| req.url = `${rewrittenPath}${url.search}`; | ||
| res.setHeader('Content-Language', 'en'); | ||
| } | ||
| next(); | ||
| }); | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // @ts-check | ||
| import { existsSync, readdirSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { locales } from './locales.ts'; | ||
|
|
||
| const defaultDocsDir = fileURLToPath(new URL('../src/content/docs/', import.meta.url)); | ||
|
|
||
| /** | ||
| * Route paths (for example `/ja/get-started/install-cli/`) of every page that | ||
| * has a real translation in a non-default locale. Untranslated locale routes | ||
| * are not built; the static host serves the English page in place. | ||
| * | ||
| * @param {string} [docsDir] | ||
| * @returns {string[]} | ||
| */ | ||
| export function getTranslatedPaths(docsDir = defaultDocsDir) { | ||
| const paths = new Set(); | ||
| for (const locale of Object.keys(locales)) { | ||
| if (locale === 'root') continue; | ||
| const localeDir = join(docsDir, locale); | ||
| if (!existsSync(localeDir)) continue; | ||
|
|
||
| for (const file of readdirSync(localeDir, { recursive: true, encoding: 'utf8' })) { | ||
| if (!/\.mdx?$/i.test(file)) continue; | ||
| const slug = `${locale}/${file.replace(/\\/g, '/').replace(/\.mdx?$/i, '')}` | ||
| .toLowerCase() | ||
| .replace(/\/index$/, ''); | ||
| paths.add(`/${slug}/`); | ||
| } | ||
| } | ||
| return [...paths].sort(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.