DocsQuickstarts
Quickstart - Vite and React SPA
Add Generic Translation to a client-side React app built with Vite, with translations loaded from static files or the CDN.
This adds Spanish, French and Japanese to a Vite + React single-page app.
1. Install
The Generic Translation packages are not published to npm yet. Clone the parent repository, build the packages, and link them. The vite-app example in packages/react/examples/vite-app is set up exactly this way.
git clone --recurse-submodules https://github.com/generic-translation/generic-translation.git
for p in core react cli; do (cd generic-translation/packages/$p && pnpm install && pnpm build); done
In your app’s package.json:
{
"dependencies": {
"@generic-translation/core": "link:../generic-translation/packages/core",
"@generic-translation/react": "link:../generic-translation/packages/react"
},
"devDependencies": {
"@generic-translation/cli": "link:../generic-translation/packages/cli"
}
}
Because the packages are linked from outside your project, tell Vite to share one copy of React:
// vite.config.ts
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [react()],
resolve: { dedupe: ["react", "react-dom"] },
});
2. Configure
pnpm generic-translation init --locales es,fr,ja
generic-translation init detects Vite and writes generic-translation.config.json with the output template public/_generic-translation/[locale].json. Vite serves everything in public/ as static files, so the browser can fetch the maps at /_generic-translation/es.json. Credentials for the CLI go in .env.local (see the CLI reference).
3. Mount the provider
// src/main.tsx
import { createRoot } from "react-dom/client";
import { GenericTranslationProvider } from "@generic-translation/react";
import { App } from "./App";
async function loadTranslations(locale: string): Promise<Record<string, string>> {
const response = await fetch(`${import.meta.env.BASE_URL}_generic-translation/${locale}.json`);
return response.ok ? response.json() : {};
}
createRoot(document.getElementById("root")!).render(
<GenericTranslationProvider defaultLocale="en" locales={["es", "fr", "ja"]} loadTranslations={loadTranslations}>
<App />
</GenericTranslationProvider>,
);
After mount, the provider picks the visitor’s saved locale (cookie or localStorage), then the browser’s language, and fetches that map. Nothing is read during render, so server rendering and the first client render always agree. See the provider reference.
Prefer the CDN? Drop loadTranslations and pass projectId="prj_...". The provider then fetches {baseUrl}/cdn/{projectId}/{locale}.json. That requires publishing (see Publishing and the CDN).
4. Mark up text
// src/App.tsx
import { T, Var, Plural, LocaleSelector, useGenericTranslation } from "@generic-translation/react";
export function App() {
const t = useGenericTranslation();
const user = "Ada";
const count = 2;
return (
<main>
<LocaleSelector aria-label={t("Language")} />
<h1>
<T>Ship in <em>every</em> language</T>
</h1>
<p>
<T>
Welcome back, <strong><Var name="user">{user}</Var></strong>. You have{" "}
<Plural n={count} one="one message" other={<><Var name="count">{count}</Var> messages</>} />.
</T>
</p>
<button title={t("Sign out", { $context: "button" })}>
<T context="button">Sign out</T>
</button>
</main>
);
}
5. Translate
pnpm generic-translation translate
The CLI scans src/**/*.{ts,tsx}, extracts the messages, translates what is new through the API, and writes the three maps. Reload the page and pick a language, or open the page with a browser set to Spanish.
During development you can skip the CLI round trip: pass devApiKey to the provider and missing strings are translated on demand. This is for local work only; the key is ignored in production builds with a console error, and API keys must never be shipped to browsers. See devApiKey.
6. Ship
Commit public/_generic-translation/*.json (or generate it in CI with generic-translation translate), and run vite build. See CI.
Something wrong or unclear on this page? Open an issue.