Quick Start
Express
This page demonstrates the minimal setup of Intor in an Express project.
Installation
This guide assumes that a Express project environment is already set up.
For initial project setup, see: Express
Install Intor:
npm install intoryarn add intorpnpm add intorbun add intor
Project Structure
index.jsonUindex.jsonUindex.tsUintor-config.tsU
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 intor-config.ts under src/.
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 index.ts under src/.
Initialize the Express application and register Intor’s request handler.
index.tsU
import express from "express"; import { createIntorHandler } from "intor/express"; import { intorConfig } from "./intor-config"; const app = express(); app.use(createIntorHandler(intorConfig)); app.listen(3000, () => { console.log(`Server is running on http://localhost:3000`); });
Usage Example
Use getTranslator to access translation capabilities:
t: returns the resolved texttRich: renders structured messages with semantic tags
index.tsM
import { getTranslator } from "intor/express"; // ... app.get("/", async function (req, res) { const { t, tRich } = await getTranslator(intorConfig, req); res.send({ hello: t("hello", { name: "Intor" }), rich: tRich("rich", { tag: (children) => `<b>${children}</b>` }), }); }); // ...
As a convenience option, translation helpers are also available directly on
req, though type inference is limited in this mode.app.get("/", async function (req, res) { res.send({ hello: req.t("hello", { name: "Intor" }), rich: req.tRich("rich", { tag: (children) => `<b>${children}</b>` }), }); });