DocsTanStack Start
Quickstart - TanStack Start
Server-rendered translations in TanStack Start with loadGenericTranslation, a root loader and GenericTranslationProvider.
On this page
This sets up @generic-translation/tanstack-start so the server picks the visitor’s locale, loads its translations, and the first byte of HTML is already translated. Hydration then matches because the client provider receives the same data. The source is in generic-translation/generic-translation-tanstack-start; see the reference for the full API.
Not tested in a real browser session. The example app was built with vite build and its production server handler was called directly with Request objects; unit tests cover SSR plus hydration and setLocale. Client-side navigation through the loader RPC, the locale-switch POST and the cookie round trip have not been run in a browser. See what was verified.
1. Install
The @generic-translation/* packages are not published to npm yet, so pnpm add @generic-translation/tanstack-start will not work as written. Clone the parent repository, build the packages and link them.
git clone --recurse-submodules https://github.com/generic-translation/generic-translation.git
for p in core react tanstack-start cli; do (cd generic-translation/packages/$p && pnpm install && pnpm build); done
{
"dependencies": {
"@generic-translation/core": "link:../generic-translation/packages/core",
"@generic-translation/react": "link:../generic-translation/packages/react",
"@generic-translation/tanstack-start": "link:../generic-translation/packages/tanstack-start"
},
"devDependencies": {
"@generic-translation/cli": "link:../generic-translation/packages/cli"
}
}
Peer dependencies: react >= 18, @tanstack/react-router and, for server functions, @tanstack/react-start. The package was written against TanStack Start 1.17x (the example builds with Start 1.170 and Vite 8).
2. Extract translations
npx generic-translation init --framework tanstack-start --locales es,fr,ar
npx generic-translation translate
This writes public/_generic-translation/{locale}.json. The CLI treats imports from @generic-translation/tanstack-start like imports from @generic-translation/react. Alternatively set projectId in the server config to read maps from the Generic Translation CDN. Until the packages are on npm, run the built CLI with node /path/to/generic-translation/packages/cli/dist/cli.js.
3. Two server functions
They must live in your own source so Start’s compiler can split them.
// src/generic-translation.ts
import { createServerFn } from "@tanstack/react-start";
import { getRequest, setCookie } from "@tanstack/react-start/server";
import { determineLocale } from "@generic-translation/core";
import { LOCALE_COOKIE, allLocales } from "@generic-translation/tanstack-start";
import { loadGenericTranslation } from "@generic-translation/tanstack-start/server";
export const config = { locales: ["es", "fr", "ar"], defaultLocale: "en" };
export const getGenericTranslationData = createServerFn({ method: "GET" }).handler(async () =>
loadGenericTranslation({ ...config, request: getRequest() }),
);
export const setLocaleFn = createServerFn({ method: "POST" })
.validator((locale: string) => locale)
.handler(async ({ data }) => {
const locale = determineLocale(data, allLocales(config));
if (!locale) throw new Error(`unsupported locale "${data}"`);
setCookie(LOCALE_COOKIE, locale, { path: "/", maxAge: 31_536_000, sameSite: "lax" });
return locale;
});
4. Wire the root route
// src/routes/__root.tsx
import { HeadContent, Outlet, Scripts, createRootRoute } from "@tanstack/react-router";
import { GenericTranslationProvider, getHtmlAttributes } from "@generic-translation/tanstack-start";
import { getGenericTranslationData, setLocaleFn } from "../generic-translation";
export const Route = createRootRoute({
loader: () => getGenericTranslationData(),
component: function Root() {
const genericTranslation = Route.useLoaderData();
const { lang, dir } = getHtmlAttributes(genericTranslation.locale);
return (
<html lang={lang} dir={dir}>
<head><HeadContent /></head>
<body>
<GenericTranslationProvider loaderData={genericTranslation} onSetLocale={(l) => setLocaleFn({ data: l })}>
<Outlet />
</GenericTranslationProvider>
<Scripts />
</body>
</html>
);
},
});
5. Use T anywhere below
import { T, Var, LocaleSelector } from "@generic-translation/tanstack-start";
<T>Hello <b><Var name="user">{name}</Var></b></T>
The repository’s examples/start-app is exactly this setup. Locale-prefixed URLs (/fr/...) are optional; see the reference.
Next steps
Something wrong or unclear on this page? Open an issue.