惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
Arctic Wolf
U
Unit 42
爱范儿
爱范儿
WordPress大学
WordPress大学
博客园 - 司徒正美
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
美团技术团队
博客园_首页
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
P
Palo Alto Networks Blog
H
Hacker News: Front Page
博客园 - 叶小钗
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
罗磊的独立博客
TaoSecurity Blog
TaoSecurity Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Help Net Security
Help Net Security
雷峰网
雷峰网
S
Security @ Cisco Blogs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Forbes - Security
Forbes - Security
T
Troy Hunt's Blog
V
V2EX
博客园 - 聂微东
Cloudbric
Cloudbric
大猫的无限游戏
大猫的无限游戏
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Commits to openclaw:main
Recent Commits to openclaw:main
IT之家
IT之家
S
SegmentFault 最新的问题
T
Threat Research - Cisco Blogs
J
Java Code Geeks
H
Heimdal Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
小众软件
小众软件
Microsoft Azure Blog
Microsoft Azure Blog
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
The Cloudflare Blog
Simon Willison's Weblog
Simon Willison's Weblog
月光博客
月光博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
SSR-Safe Internationalization in TanStack Start with i18next
Royhan · 2026-06-14 · via DEV Community

SSR can be tricky, mostly because every framework does things its own way. Loaders, lifecycles, hydration, serialization all behave a little differently depending on where you are, so wiring up something like i18next is rarely a copy-paste between frameworks. i18next has solid SSR support, but how you connect it still depends on the framework you're using.

This post focuses on TanStack Start specifically:

  • Detecting the user's preferred language on the server.
  • Initializing separate i18next instances for server and client.
  • Safely passing an i18next instance across the server-to-client boundary without things quietly breaking.

By the end you'll have a working pattern that you can adapt to your own projects.

If you'd rather learn by reading the finished code, the full project is on GitHub: https://github.com/ookamiiixd/tanstack-start-i18next

A quick heads-up before we start: everything here is based on my own testing and experience, not official guidance, so I might have gotten some details wrong. If you spot a mistake or know a better way to do something, I'd genuinely welcome the correction or suggestion.

Table of Contents

  • Understanding the Strategy
  • Prerequisites
  • Project Structure
  • Setting Up i18next
  • Wiring Up TanStack Start
  • Using Translations
  • Gotchas
  • Wrap-up

Understanding the Strategy

This guide follows the strategy below. Feel free to adjust it to your needs.

  • Language detection (in order of priority): optional path param, then cookie, then the Accept-Language header.
  • Translation resources live in public/translations and are served as static JSON files.
  • On the server, resources for all languages are bundled together. This keeps things simple, and bundling everything upfront is cheap on the server.
  • On the client, resources load selectively by namespace using TanStack Router's staticData, and anything else is fetched lazily through i18next-http-backend. Depending on the size of your translations, you might prefer bundling all resources for one language, or all resources for every language.

Prerequisites

Make sure you have a TanStack Start project ready. Use your own, pick one from the examples, or scaffold a new one:

npx @tanstack/cli@latest create

Install the dependencies:

npm i i18next i18next-http-backend react-i18next

For convenience this guide also uses accept-language-parser to parse the Accept-Language header:

npm i accept-language-parser
npm i -D @types/accept-language-parser

Project Structure

src
│   client.tsx        // Client entrypoint, recovers the i18n instance and hydrates
│   router.tsx        // Router factory + type augmentations
│   server.tsx        // Server entrypoint, builds the per-request i18n instance and request context
│   start.ts          // Start config, registers the i18n serialization adapter
│
├───lib
│   └───i18n
│           client.ts      // Client instance (HTTP backend, lazy namespaces) + cookie helpers
│           config.ts      // Shared i18next config + cookie key
│           index.ts       // Isomorphic facade: instance/language helpers, getFixedT, serialization adapter
│           resources.ts   // Bundled resources + type-safe keys
│           server.ts      // Server instance (all resources bundled) + language detection
│
└───routes
    │   __root.tsx    // Language resolution & redirect, document shell, SSR resource serialization
    │
    └───{-$lang}
            route.tsx     // Language layout (validates the lang param, nav + switcher)
            index.tsx     // Home page

public
└───translations    // Translation files
    ├───en
    │       common.json
    │       ...
    │
    └───id
            common.json
            ...

The {-$lang} segment is an optional path param. It matches / (no prefix, used for the fallback language) as well as /en and /id.

Setting Up i18next

1. Translation files

Create translation files for at least two languages:

// public/translations/en/common.json
{ /* ... */ }

// public/translations/id/common.json
{ /* ... */ }

2. Shared config

This config is shared by both instances. Adjust it to your needs. The ns list is the set of namespaces that are always loaded; per-route namespaces come later through staticData, so keep this list small.

// src/lib/i18n/config.ts
import type { FlatNamespace, InitOptions } from "i18next";

export const config = {
  enableSelector: "strict",
  fallbackLng: "en",
  supportedLngs: ["en", "id"],
  ns: ["common", "errors"] satisfies FlatNamespace[],
  defaultNS: "common",
  interpolation: { escapeValue: false },
} satisfies InitOptions;

export const COOKIE_LANGUAGE_KEY = "language";

3. Resources + type-safe keys

Register your translation resources and augment the i18next types so your keys are fully typed:

// src/lib/i18n/resources.ts
import commonEn from "@/../public/translations/en/common.json";
import commonId from "@/../public/translations/id/common.json";
// ...

export const resources = {
  en: {
    common: commonEn,
    // ...
  },
  id: {
    common: commonId,
    // ...
  },
} as const;

declare module "i18next" {
  interface CustomTypeOptions {
    enableSelector: "strict";
    defaultNS: "common";
    resources: (typeof resources)["en"];
  }
}

This file imports every locale JSON, but it's only used by the server utilities, and TanStack Start strips the server code (and this file with it) from the client bundle. So the full resource set never ships to the browser.

4. Client utilities

The client utilities initialize the instance, read the language preference, and write the cookie:

// src/lib/i18n/client.ts
import i18next, { type InitOptions } from "i18next";
import I18NextHttpBackend from "i18next-http-backend";
import { initReactI18next } from "react-i18next";
import pkg from "@/../package.json";
import { COOKIE_LANGUAGE_KEY, config } from "./config";

export function getClientInstance(language: string, overrideConfig?: InitOptions) {
  globalThis.__i18n ??= i18next.createInstance();
  const instance = globalThis.__i18n;
  if (instance.isInitialized) {
    return instance;
  }

  void instance
    .use(I18NextHttpBackend)
    .use(initReactI18next)
    .init({
      ...structuredClone(config),
      debug: import.meta.env.DEV,
      initAsync: false,
      partialBundledLanguages: true, // resources from SSR are bundled; load the rest lazily
      backend: {
        loadPath: `/translations/{{lng}}/{{ns}}.json?v=${pkg.version}`, // cache-bust per release
      },
      lng: language,
      ...overrideConfig,
    });

  instance.isInitialized = true;
  instance.initializedLanguageOnce = true;
  if (overrideConfig?.resources) {
    instance.initializedStoreOnce = true;
  }

  return instance;
}

export function getClientLanguage() {
  const languageFromCookie = document.cookie
    .split(";")
    .map((cookie) => cookie.trim())
    .find((cookie) => cookie.startsWith(`${COOKIE_LANGUAGE_KEY}=`))
    ?.split("=")[1];

  return [languageFromCookie || config.fallbackLng, !!languageFromCookie] as const;
}

export function setClientLanguageCookie(language: string) {
  document.cookie = `${COOKIE_LANGUAGE_KEY}=${language}; path=/; max-age=31536000`;
}

On the client we keep one shared global instance. partialBundledLanguages plus the HTTP backend lets us seed some resources and fetch the rest on demand, and the manual isInitialized flags make react-i18next treat the instance as ready right away so the first render doesn't suspend.

5. Server utilities

The server utilities mirror the client's responsibilities:

// src/lib/i18n/server.ts
import { setCookie } from "@tanstack/react-start/server";
import acceptLanguageParser from "accept-language-parser";
import i18next, { type InitOptions } from "i18next";
import { initReactI18next } from "react-i18next";
import { COOKIE_LANGUAGE_KEY, config } from "./config";
import { resources } from "./resources";

export function getServerInstance(language: string, overrideConfig?: InitOptions) {
  const instance = i18next.createInstance();
  void instance.use(initReactI18next).init({
    ...structuredClone(config),
    initAsync: false,
    resources, // all languages bundled on the server
    lng: language,
    ...overrideConfig,
  });

  instance.isInitialized = true;
  instance.initializedLanguageOnce = true;
  instance.initializedStoreOnce = true;

  return instance;
}

export function getServerLanguage(headers: Headers) {
  let language = config.fallbackLng;
  let shouldUpdateCookie = false;

  const languageFromCookie =
    headers
      .get("cookie")
      ?.split("; ")
      .find((row) => row.startsWith(`${COOKIE_LANGUAGE_KEY}=`))
      ?.split("=")[1] ?? null;

  const acceptLanguage = headers.get("accept-language");
  const languageFromHeader = acceptLanguage
    ? acceptLanguageParser.pick(config.supportedLngs, acceptLanguage)
    : null;

  const detectedLanguage = languageFromCookie ?? languageFromHeader;
  if (detectedLanguage && config.supportedLngs.includes(detectedLanguage)) {
    language = detectedLanguage;
  }

  if (language !== languageFromCookie) {
    shouldUpdateCookie = true;
  }

  return [language, shouldUpdateCookie] as const;
}

export function setServerLanguageCookie(language: string) {
  setCookie(COOKIE_LANGUAGE_KEY, language, { path: "/", maxAge: 365 * 24 * 60 * 60 });
}

On the server we create one instance per request, which gets passed down through context.

Notice the structuredClone in both files. i18next mutates the config while it runs (for example, it pushes loaded namespaces into options.ns). Since we reuse one config object, we clone it so each instance gets its own copy. Without that, namespaces loaded during one request would leak into later requests on the same server. structuredClone is the simplest option, but it throws if your config ever contains functions, so if you add things like a custom formatter, clone just the ns array instead.

6. Isomorphic utilities

Finally, tie the two sides together behind one import:

// src/lib/i18n/index.ts
import { createSerializationAdapter } from "@tanstack/react-router";
import { createIsomorphicFn } from "@tanstack/react-start";
import { getRequestHeaders } from "@tanstack/react-start/server";
import type { DefaultNamespace, FlatNamespace, i18n, TFunction } from "i18next";
import { getClientInstance, getClientLanguage, setClientLanguageCookie } from "./client";
import { getServerInstance, getServerLanguage, setServerLanguageCookie } from "./server";

export { config as i18nConfig } from "./config";

export const getI18nInstance = createIsomorphicFn()
  .client(getClientInstance)
  .server(getServerInstance);

export const getLanguage = createIsomorphicFn()
  .client(getClientLanguage)
  .server(() => {
    const headers = getRequestHeaders();
    return getServerLanguage(headers);
  });

export const setLanguageCookie = createIsomorphicFn()
  .client(setClientLanguageCookie)
  .server(setServerLanguageCookie);

// i18next instances aren't serializable. This adapter is a no-op that
// hands back the global client instance we already have.
export const i18nSerializationAdapter = createSerializationAdapter<i18n, undefined>({
  key: "i18n",
  test: (value): value is i18n =>
    typeof value === "object" && value !== null && "language" in value && "store" in value,
  toSerializable: () => {},
  fromSerializable: () => globalThis.__i18n!,
});

export async function getFixedT<NS extends FlatNamespace[] = [DefaultNamespace]>(
  instance: i18n,
  namespaces: NS,
): Promise<TFunction<NS>>;
export async function getFixedT<NS extends FlatNamespace[] = [DefaultNamespace]>(
  instance: i18n,
  language: string,
  namespaces: NS,
): Promise<TFunction<NS>>;
export async function getFixedT<NS extends FlatNamespace[] = [DefaultNamespace]>(
  instance: i18n,
  languageOrNamespaces: NS | string,
  maybeNamespaces?: NS,
): Promise<TFunction<NS>> {
  const language = maybeNamespaces ? (languageOrNamespaces as string) : instance.language;
  const namespaces = (maybeNamespaces ?? languageOrNamespaces) as NS;

  await instance.loadNamespaces(namespaces);
  return instance.getFixedT(language, namespaces);
}

Two pieces matter here. The serialization adapter exists because TanStack Start won't let you pass a non-serializable value (like an i18next instance) to the client, but we don't actually want to serialize it. So the adapter does nothing on the way out, and on the client it just returns the global instance we already created. getFixedT loads a namespace before returning a t function, which is what you want on the client where namespaces arrive lazily; on the server it's basically free since everything is bundled.

Wiring Up TanStack Start

1. Type augmentations

Augment the router types. We do this in router.tsx for convenience, but it can live anywhere in your project:

// src/router.tsx
import type { FlatNamespace, i18n } from "i18next";

export function getRouter() {
  // ...
}

declare module "@tanstack/react-router" {
  interface Register {
    router: ReturnType<typeof getRouter>;
    server: {
      requestContext: { i18n: i18n; updateLanguageCookie: boolean };
    };
  }

  interface StaticDataRouteOption {
    namespaces?: FlatNamespace[];
  }
}

requestContext types the object you hand to the server entry, and namespaces is what lets each route declare which translation namespaces it needs.

2. Start config

Register the serialization adapter:

// src/start.ts
import { createStart } from "@tanstack/react-start";
import { i18nSerializationAdapter } from "./lib/i18n";

export const startInstance = createStart(() => ({
  defaultSsr: true,
  serializationAdapters: [i18nSerializationAdapter],
}));

3. Server entrypoint

Use a custom server entry to build the per-request instance and put it in the request context:

// src/server.tsx
import type { Register } from "@tanstack/react-router";
import { renderRouterToStream } from "@tanstack/react-router/ssr/server";
import {
  createStartHandler,
  defineHandlerCallback,
  StartServer,
} from "@tanstack/react-start/server";
import { createServerEntry } from "@tanstack/react-start/server-entry";
import { I18nextProvider } from "react-i18next";
import { getServerInstance, getServerLanguage } from "./lib/i18n/server";

const customHandler = defineHandlerCallback(async ({ request, router, responseHeaders }) => {
  const { i18n } = router.options.additionalContext
    .serverContext as Register["server"]["requestContext"];

  return renderRouterToStream({
    request,
    router,
    responseHeaders,
    children: (
      <I18nextProvider i18n={i18n}>
        <StartServer router={router} />
      </I18nextProvider>
    ),
  });
});
const fetch = createStartHandler(customHandler);

export default createServerEntry({
  async fetch(request) {
    const [language, shouldUpdateCookie] = getServerLanguage(request.headers);
    const i18n = getServerInstance(language);

    return fetch(request, {
      context: { i18n, updateLanguageCookie: shouldUpdateCookie },
    });
  },
});

The fetch handler runs for every request: it detects the language, builds the instance, and passes it down as serverContext. The custom handler then wraps StartServer in I18nextProvider so server-rendered components translate against that instance. We put the provider here, at the entry level rather than inside a route component, so error and not-found pages can translate too, since those render in place of your normal component tree.

4. Client entrypoint

Recover the instance on the client and hydrate:

// src/client.tsx
import { StartClient } from "@tanstack/react-start/client";
import { StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
import { I18nextProvider } from "react-i18next";
import { getClientInstance } from "./lib/i18n/client";

const i18nData = globalThis.__i18nData;
const i18n = getClientInstance(i18nData.language, {
  ns: i18nData.namespaces,
  resources: i18nData.resources,
});

hydrateRoot(
  document,
  <StrictMode>
    <I18nextProvider i18n={i18n}>
      <StartClient />
    </I18nextProvider>
  </StrictMode>,
);

We read what the server injected into window.__i18nData (set up in the next step) and build the client instance from it.

Passing ns here is important. Seeding resources puts the data into i18next, but i18next only treats namespaces you list in ns as already loaded. If you skip a seeded namespace, i18next fetches it again right after hydration and you get a flash. So list all of them.

5. The root route

The root route handles language resolution, redirects, and SSR serialization.

First, language resolution in beforeLoad:

beforeLoad: async ({ serverContext, location, preload }) => {
  let i18n: i18n;
  let language: string;
  let shouldUpdateCookie = false;

  if (serverContext) {
    // On the server, the instance comes from request context.
    i18n = serverContext.i18n;
    language = i18n.language;
    shouldUpdateCookie = serverContext.updateLanguageCookie;
  } else {
    // serverContext is absent on the client, so detect and recover the instance.
    const [_language, _shouldUpdateCookie] = getLanguage();
    language = _language;
    shouldUpdateCookie = _shouldUpdateCookie;
    i18n = getI18nInstance(language);
  }

  let pathname = location.pathname;
  const firstSegment = pathname.split("/").filter(Boolean)[0];

  // Redirect to a language-prefixed URL when the resolved language isn't the
  // fallback and the first path segment is missing or unsupported.
  if (
    (!firstSegment || !i18nConfig.supportedLngs.includes(firstSegment)) &&
    language !== i18nConfig.fallbackLng
  ) {
    pathname = `/${language}${pathname === "/" ? "" : pathname}`;
    const search = location.searchStr === "?" ? "" : location.searchStr;
    throw redirect({ to: pathname + search, replace: true });
  }

  // A supported language in the URL wins.
  if (firstSegment && i18nConfig.supportedLngs.includes(firstSegment)) {
    language = firstSegment;
    shouldUpdateCookie = true;
  }

  // Only run side effects on a real navigation, never during a preload.
  if (!preload) {
    if (i18n.language !== language) {
      await i18n.changeLanguage(language);
    }
    if (shouldUpdateCookie) {
      setLanguageCookie(language);
    }
  }

  return { i18n };
},

serverContext is the signal for which side you're on: it's there on the server and absent on the client. And the !preload guard keeps us from changing the language or writing cookies just because a link was hovered.

Next, the SSR serialization helper:

function getI18nDataScript(i18n: i18n, matches: AnyRouteMatch[]) {
  const language = i18n.language;
  const namespacesFromMatches = matches
    .filter((match) => !!match.staticData?.namespaces?.length)
    .flatMap((match) => match.staticData.namespaces!);
  const namespaces = new Set<FlatNamespace>([...i18nConfig.ns, ...namespacesFromMatches]);

  const i18nData: typeof globalThis.__i18nData = {
    language,
    namespaces: [...namespaces],
    resources: { [language]: {} },
  };

  for (const namespace of namespaces) {
    const resource = i18n.store.data?.[language]?.[namespace];
    if (resource) {
      i18nData.resources[language]![namespace] = resource;
    }
  }

  return `window.__i18nData = ${JSON.stringify(i18nData)}`;
}

declare global {
  var __i18n: i18n | undefined;
  var __i18nData: {
    language: string;
    namespaces: string[];
    resources: Resource;
  };
}

This collects only what the current page needs, the always-loaded i18nConfig.ns plus each matched route's staticData.namespaces, and serializes those for the active language. That's the selective loading in action: the home page ships home's namespaces and nothing else.

Finally, the shell component:

function RootDocument({ children }: { children: React.ReactNode }) {
  const { i18n } = useTranslation();
  const matches = useMatches();

  return (
    <html lang={i18n.language} dir={i18n.dir()}>
      <head>
        <HeadContent />
      </head>
      <body>
        {children}
        {import.meta.env.SSR && (
          <ScriptOnce>{getI18nDataScript(i18n, matches)}</ScriptOnce>
        )}
        <Scripts />
      </body>
    </html>
  );
}

// ...
export const Route = createRootRoute({
  shellComponent: RootDocument,
  // ...
});

ScriptOnce renders a server-only inline script that removes itself after running, so it's gone before React hydrates and doesn't cause a mismatch. The import.meta.env.SSR guard is false in the client build, so the whole thing is dropped from the browser bundle. It's a server-only concern, so there's no reason to ship or run it on the client.

Using Translations

Declaring namespaces per route

A route declares the namespaces it needs through staticData. That's the only thing the SSR serializer reads to decide what to ship:

// src/routes/{-$lang}/index.tsx
export const Route = createFileRoute("/{-$lang}/")({
  staticData: { namespaces: ["home"] },
  component: Home,
});

function Home() {
  const { t } = useTranslation("home");
  return <h1>{t(($) => $.home.title)}</h1>;
}

Setting the page title

You can set the title in head and read the instance from match.context.i18n. Since head runs during the load phase, this sets the title and loads the namespace in one go, and you don't need to thread it through a loader:

head: async ({ match }) => {
  const t = await getFixedT(match.context.i18n, ["home"]);
  return { meta: [{ title: t(($) => $.home.title) }] };
},

Keep the route's staticData.namespaces either way, since that's what feeds the SSR payload.

A language switcher

When switching languages, load the target language before changing the URL:

// src/components/language-switcher.tsx
async function handleChangeLanguage(newLanguage: string) {
  // ...build the new pathname...
  await i18n.changeLanguage(newLanguage); // load the target language first
  setClientLanguageCookie(newLanguage);
  await router.navigate({ to: pathname + search, replace: true });
  await router.invalidate();
}

Error and not-found pages

Error and not-found pages need titles too, but head only runs during loading, so it can't title a page that throws while rendering. The simple, reliable approach is to render <title> inside the error component itself. React 19 moves it into <head>, and it works whether the failure happened at load time or render time:

// src/components/errors.tsx
function BaseError({ title, description }: { title: string; description: string }) {
  const { t, i18n } = useTranslation("common");
  return (
    <>
      <title>{title}</title>
      <div>
        <h1>{title}</h1>
        <p>{description}</p>
        <Link to="/{-$lang}" params={{ lang: i18n.language }}>
          {t(($) => $.common.links.home)}
        </Link>
      </div>
    </>
  );
}

Just don't set the error title in both head and a component <title>, or you'll end up with two of them.

Gotchas

A quick recap of the things that tend to trip people up:

  • Clone the config per instance. i18next mutates it as it runs, so a shared config leaks state between requests on the server.
  • List every seeded namespace in ns on the client. Otherwise i18next re-fetches them after hydration and you get a flash.
  • Put the provider at the entry level, not inside a component, so error and not-found pages can translate.

Wrap-up

You now have a TanStack Start i18n setup that detects language on the server, renders translated HTML, hydrates without a flash, loads namespaces selectively, and passes the instance across the boundary cleanly. Adapt the detection order, bundling granularity, and routing to taste. The full example is on GitHub if you want a starting point: https://github.com/ookamiiixd/tanstack-start-i18next

Good luck!