Capítulo 33 de 57
TanStack Router is library-agnostic for i18n: it provides composable primitives (optional path parameters, URL rewrites, type-safe params) that let you build locale-aware routing yourself, or integrate a dedicated i18n library like Paraglide for translations, locale detection, and URL localization, in both client-only and TanStack Start (SSR) setups.
/{-$locale}/about matches /about (default), /en/about, /fr/about, /es/about etc. in a single route definition, avoiding duplicated route trees per locale (see Ch 26 for the underlying {-$param} mechanism).beforeLoad.Locale union type plus a type-guard function (isLocale/validateLocale) used in beforeLoad to reject or redirect on invalid locale segments.<Link params={(prev) => ({ ...prev, locale: ... })}>, commonly setting locale: undefined for the default language to keep URLs clean (e.g. no /en/ prefix).rewrite Router Option for Locale URLs: When using a client-side i18n library (e.g. Paraglide), createRouter({ rewrite: { input, output } }) deLocalizes incoming URLs before matching and re-localizes outgoing URLs, this is the same input/output rewrite mechanism from Ch 22, applied specifically to locale prefixes.@inlang/paraglide-js provides type-safe translations and URL localization helpers (localizeUrl, deLocalizeUrl, localizeHref, getLocale, shouldRedirect) that plug into the router's rewrite option (client-only) or server middleware (paraglideMiddleware, TanStack Start SSR).shouldRedirect({ url }) checked in beforeLoad, throwing redirect({ href: decision.redirectUrl.href }) when a locale redirect decision is needed, works even in client-only/offline environments.FileRoutesByTo route tree type ensures no route is missing a translation, with compiler feedback.localizeHref to generate one prerendered output per locale.// Route: /{-$locale}/about
export const Route = createFileRoute('/{-$locale}/about')({
component: AboutComponent,
})
function AboutComponent() {
const { locale } = Route.useParams()
const currentLocale = locale || 'en'
const content = {
en: { title: 'About Us' },
fr: { title: 'À Propos' },
es: { title: 'Acerca de' },
}
return <h1>{content[currentLocale].title}</h1>
}
const router = createRouter({
routeTree,
rewrite: {
input: ({ url }) => deLocalizeUrl(url),
output: ({ url }) => localizeUrl(url),
},
})
rewrite config delegating locale URL transformation to Paraglide's runtime helpers.{-$locale}) alone are enough, no external i18n library required.rewrite option (client-side) or server middleware (TanStack Start) with a dedicated library like Paraglide rather than reimplementing locale URL logic by hand.locale param (type guard + beforeLoad check) since it originates from user-controllable URL input, same discipline as any other path/search param (Ch 26/27).input/output rewrite mechanism reused here for locale prefixes.{-$param} optional parameter syntax this chapter's core pattern depends on.Link params function-style updates used for language switchers.