Quick Start
React
This page demonstrates the minimal setup of Intor in a React project.
Installation
In this example, we use Vite to create a React project.
See the official documentation for details: Vite
Install Intor:
npm install intoryarn add intorpnpm add intorbun add intor
Project Structure
index.jsonUindex.jsonUApp.tsxMmain.tsxMintor-config.tsUintor-client-provider.tsxU
Integration Steps
♯1 Messages
Create a messages directory in your project, and add a subdirectory for each locale.
Each locale provides an index.json file.
index.jsonUindex.jsonU
{ "hello": "Hello, {name}", "rich": "A <tag>text</tag>." }
{ "hello": "Bonjour, {name}", "rich": "Un <tag>texte</tag>." }
♯2 Config
Create an i18n/ directory under src/, and add an intor-config.ts file inside it.
This example uses the most basic translation loading approach by defining messages directly in the config.
For more details, see: Message Loading
intor-config.tsU
import { defineIntorConfig } from "intor"; import EN from "../../messages/en/index.json"; import FR from "../../messages/fr/index.json"; export const intorConfig = defineIntorConfig({ defaultLocale: "en", supportedLocales: ["en", "fr"], messages: { en: EN, fr: FR, }, });
♯3 Initialization
Create intor-client-provider.tsx inside the i18n/ directory.
intor-client-provider.tsxU
import type { ReactNode } from "react"; import { getClientLocale } from "intor"; import { IntorProvider } from "intor/react"; import { intorConfig } from "./intor-config"; export function IntorClientProvider({ children }: { children: ReactNode }) { const locale = getClientLocale(intorConfig); return ( <IntorProvider value={{ config: intorConfig, locale }}> {children} </IntorProvider> ); }
Then, wrap your application with this provider in main.tsx.
main.tsxM
// ... import { IntorClientProvider } from "./i18n/intor-client-provider.tsx"; createRoot(document.getElementById("root")!).render( <StrictMode> <IntorClientProvider> <App /> </IntorClientProvider> </StrictMode>, );
Usage Example
Use useTranslator to access translation capabilities:
t: returns the resolved texttRich: renders structured messages with semantic tagssetLocale: switches the active locale
App.tsxM
// ... import { useTranslator } from "intor/react"; function App() { const { t, tRich, setLocale } = useTranslator(); return ( <> <p>{t("hello", { name: "Intor" })}</p> <p>{tRich("rich", { tag: (children) => <b>{children}</b> })}</p> <button onClick={() => setLocale("en")}>English</button> <button onClick={() => setLocale("fr")}>French</button> </> ); } export default App;